PHP for循环 - 在表格中显示数据 [英] PHP for loop - display data in table

查看:134
本文介绍了PHP for循环 - 在表格中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解为什么我的代码不工作;它只显示1个结果,而表中有多个条目。

 <?php 

$ search = $ _POST ['search'];
print($ search);
$ query =选择*从学生的地方='苏塞克斯';
$ 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 =1id =table1>
< th> Id< / th>
< th>类型< / th>
< th>说明< / th>
< th>操作员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>


解决方案

您忘记关闭FOR循环及其封闭的IF ,我在下面做了:

 <? 
if($ counter> 0)
{
for($ z = 0; $ z <$ counter; $ z ++)
{
?>
< tr>
<?
if($ z <$ counter)
{
?>
< td>
<?php echo $ id [$ z]?>
< / td>
<?
}
// $ z = $ z + 1; <! - 删除或注释掉此行 - >
?>
< / tr>
<? }
}?> <! - 这里我们关闭for循环,IF - >
< / table>

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

i can't understand why my code is not working; it is only displaying 1 result, whereas there are multiple entries in table..

<?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:

<?
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

这篇关于PHP for循环 - 在表格中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆