0

我想在两列中显示数据,如下所示

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 中它很容易,只需获取一个数据列表进行一些设置并为其提供数据源,它就会为您呈现它。

但是在PHP中如何显示这种列表,谁能给我一些代码?

谢谢

4

2 回答 2

1

我假设您想要绘制一个 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
于 2009-07-04T13:00:53.080 回答
0

我想您是在问如何构建 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 没有。

这是否回答你的问题?

于 2009-07-04T12:38:46.277 回答