简体   繁体   English

PHP for循环 - 在表格中显示数据

[英]PHP for loop - display data in table

i can't understand why my code is not working; 我无法理解为什么我的代码不起作用; it is only displaying 1 result, whereas there are multiple entries in table.. 它只显示1个结果,而表中有多个条目..

<?php  

    $search = $_POST['search'];
    print ($search);
    $query  = "SELECT * FROM student WHERE county= 'sussex'";
    $result = mysql_query($query,$conn);
    $counter = 0;
    $z=0;
    while($row = mysql_fetch_assoc($result)){
    $id[$counter] = $row['roll_number'];
    $counter = $counter + 1;
    }
?>
<table border="1" id="table1" >
    <th>Id</th>
    <th>Type</th>
    <th>Description</th>
    <th>Operator Id</th>

<?
    if($counter>0)
    {
        for($z=0;$z<$counter;$z++)
        {
?> 
            <tr>
            <?
                if($z<$counter)
                {
            ?>
                <td >
                    <?php echo $id[$z]?>                            
                </td> 
            <?
                }
                $z = $z + 1;
            ?>   
            </tr>
</table>

You forgot to close the FOR loop and its enclosing IF, I've done it below: 你忘了关闭FOR循环及其封闭的IF,我在下面做了:

<?
if($counter>0)
{
    for($z=0;$z<$counter;$z++)
    {
?> 
        <tr>
            <?
                if($z<$counter)
                {
            ?>
                <td >
                    <?php echo $id[$z]?>                            
                </td> 
            <?
                }
                //$z = $z + 1;  <!-- REMOVE or comment out this line -->
            ?>   
            </tr>
     <? }
     }  ?> <!-- HERE we close the for loop and the IF -->
</table>

UPDATE : noted, thanks to VinceBurn, that $z was incremented twice 更新 :注意,感谢VinceBurn,$ z增加了两倍

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM