1

我不明白为什么我的代码不起作用;它只显示 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>
4

1 回答 1

4

您忘记关闭 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>

更新:注意到,感谢 VinceBurn,$z 增加了两次

于 2012-11-24T16:15:59.450 回答