在一个循环中的PHP循环

时间:2014-06-13 05:31:29

标签: php mysql

如何在while循环中执行for循环,如下面的编码。该表需要显示

中columm数据的数量列表
       <?php

        $server = mysql_connect("localhost","root", "");
        $db =  mysql_select_db("registration",$server);
        $query = mysql_query("select * from user");
    ?>
    <table class="striped">
        <tr class="header">
            <td>No</td>
            <td>Id</td>
            <td>Name</td>
            <td>Title</td>
        </tr>
        <?php
           while ($row = mysql_fetch_array($query)) {
                for($x =1 ; $x <= $row ; $x++);
               echo "<tr>";
               echo "<td>"'$x'"</td>";
               echo "<td>".$row['id']."</td>";
               echo "<td>".$row['password']."</td>";
               echo "<td>".$row['position']."</td>";
               echo "</tr>";
           }

        ?>## Heading ##

2 个答案:

答案 0 :(得分:1)

$count = 1;
while ($row = mysql_fetch_array($query)) {
    echo "<tr>";
    echo "<td>" . $count . "</td>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['password'] . "</td>";
    echo "<td>" . $row['position'] . "</td>";
    echo "</tr>";

    ++$count;
}

OR

$count = 1;
while ($row = mysql_fetch_array($query)) {
    echo "<tr>";
    echo "<td>" . $count++ . "</td>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td>" . $row['password'] . "</td>";
    echo "<td>" . $row['position'] . "</td>";
    echo "</tr>";
}

答案 1 :(得分:0)

     <?php
               $num_rows = mysql_num_rows($query);
               echo "<tr>";
                   echo "<td> Total Rows".$num_rows."</td>";
               echo "</tr>";
               $i = 1;
               while ($row = mysql_fetch_array($query)) {
                    echo "<tr>";              
                   echo "<td>".$i."</td>";
                   echo "<td>".$row['id']."</td>";
                   echo "<td>".$row['password']."</td>";
                   echo "<td>".$row['position']."</td>";
                   echo "</tr>";
                   $i++;
               }
?>