php while 循环与 $i++ [英] php while loop with $i++

查看:27
本文介绍了php while 循环与 $i++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在 while 循环内有一个单选按钮,并且每次运行循环时单选按钮的名称都会增加 1.
现在代码不起作用,因为它没有递增.任何建议都会很棒.

What I want to do is have a radio button inside a while loop and the name of the radio to be increased by 1 every time the loop is ran.
Right now the code is not working because it is not incrementally increasing. Any suggestions would be great.

$query = mysql_query("SELECT * FROM table WHERE id = '1' ORDER BY time ASC");
echo '<table> <th> A </th> <th> time </th> <th> B </th>'; 

while($row = mysql_fetch_assoc($query)) {

    $i= 1;
    echo '<tr><td>';
    echo '<input type="radio" name="';echo $i++; echo'" /> '; echo $row['a'];
    echo '</td>';
    echo '<td>';
    echo $row['time'];
    echo '</td>';
    echo '<td>';
    echo '<input type="radio" name="';echo $i++; echo '" />'; echo $row['b'];
    echo '</td> </tr> ';
}


echo '</tr></table>';

推荐答案

您每次都在重置计数器.

You're resetting your counter each time.

$i = 1;

while($row = mysql_fetch_assoc($query)) {
    // Your code

    $i++;
}

.. 并将 echo $i++; 替换为 echo $i;.

.. and replace echo $i++; with echo $i;.

这篇关于php while 循环与 $i++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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