如何实现DataList控件(asp.net)这样的功能在PHP?
题
欲如下
,以显示在两列中的数据Entry 1 Entry 2
entry 1 description entry 2 description
Entry 3 Entry 4
entry 3 description entry 4 description
Entry 5
entry 5 description
现在在asp.net其prestty容易,只得到一个数据列表进行一些设置,并给数据源,它就会呈现给你。
但在PHP中如何显示这种列表,任何人都可以给我一些代码吗?
由于
解决方案
我认为你要画一个HTML表格.. 这是很容易处理的HTML布局&可变列数。 这里是一个示例代码,会做详细的你需要什么..
/* populate sample data */
for($i=1; $i<10; $i++) { $data_array[]=array('Entry '.$i,'entry '.$i.' description'); }
/* how many columns */
$column_number='3';
/* html table start */
?><table border="1" cellspacing="5" cellpadding="5"><?php
$recordcounter=1; /* counts the records while they are in loop */
foreach($data_array as $record) {
/* decide if there will be new Table row (<TR>) or not ... left of division by column number is the key */
if($recordcounter%$column_number==1){ echo "<tr>"; }
?>
<td>
<?=$record[0]?>
<br />
<?=$record[1]?>
</td>
<?php
/* decide if there will be end of table row */
if($recordcounter%$column_number==0){ echo "</tr>"; }
$recordcounter++; /* increment the counter */
}
if(($recordcounter%$column_number)!=1){ echo "</tr>"; }
?></table><?php
其他提示
我想你问如何构建HTML输出?
<?php $array = array('Item 1' => 'Description 1', 'Item 2' => 'Description 2'); ?>
<dl>
<?php foreach ($array as $item => $description) : ?>
<dt><?php echo $item; ?></dt>
<dd><?php echo $description; ?></dd>
<?php endforeach; ?>
</dl>
您可以轻松的作风,使用CSS,浮他们做出列或你有什么。
如果您使用的是框架可能包括某种做到这一点的帮手,但标准的PHP没有。
这是否回答你的问题?
不隶属于 StackOverflow