php while loop with $i++

18,976

just move the $i= 1; outside the loop

like this for instance:

...
$i= 1;
while($row = mysql_fetch_assoc($query)) {
    ...
    echo '<input type="radio" name="';echo $i; echo'" /> '; echo $row['a'];
    ...
    ...
    $i++;
}
Share:
18,976
Ricky
Author by

Ricky

Coding all day. Trapping at night.

Updated on June 13, 2022

Comments

  • Ricky
    Ricky almost 2 years

    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>';