How to correctly number rows in the indicated column in the table

How to correctly number rows in the indicated column in the table

<?php
    require_once('conect.php');
    $result = $conn->prepare("SELECT * FROM tabelatestowa ORDER BY pkt DESC");
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++);
    $numbers_rows = 1; {
?>              
    <tr>
      <td><label><?php echo $row['id']; ?></label></td>
      <td><label><?php echo ++$number_rows;?>  </label></td>
      <td><label><?php echo $row['name']; ?></label></td>
      <td><label><?php echo $row['pkt']; ?></label></td>
    </tr>
<?php } ?>
</tbody>
</table>

4 answers

  • answered 2018-05-16 05:58 Kishen Nagaraju

    Try Removing the ';' at the end of for loop and shifting the $numbers_rows = 1; before the for loop.

  • answered 2018-05-16 06:02 Rp9

    try this,seems issue with for loop format and numbers_rows should before loop

    <?php
                require_once('conect.php');
                $result = $conn->prepare("SELECT * FROM tabelatestowa ORDER BY pkt DESC");
                $result->execute();
                $numbers_rows = 1;
                for($i=0; $row = $result->fetch(); $i++)
                 {
            ?>              
                <tr>
                      <td><label><?php echo $row['id']; ?></label></td>
                      <td><label><?php echo ++$number_rows;?>  </label></td>
                      <td><label><?php echo $row['name']; ?></label></td>
                      <td><label><?php echo $row['pkt']; ?></label></td>
                </tr>
                <?php } ?>
            </tbody>
         </table>
    

  • answered 2018-05-16 06:03 Mike

    This can be easily accomplished by doing PDOStatement::fetchAll() and looping over it with foreach. This way you don't have to worry about manually setting or incrementing a counter:

    <?php
                require_once('conect.php');
                $result = $conn->prepare("SELECT * FROM tabelatestowa ORDER BY pkt DESC");
                $result->execute();
                $results = $result->fetchAll();
                foreach ($results as $index => $row) {
            ?>
                <tr>
                      <td><label><?php echo $row['id']; ?></label></td>
                      <td><label><?php echo ($index + 1);?>  </label></td>
                      <td><label><?php echo $row['name']; ?></label></td>
                      <td><label><?php echo $row['pkt']; ?></label></td>
                </tr>
                <?php } ?>
            </tbody>
         </table>
    

  • answered 2018-05-16 06:09 Nigel Ren

    The general point of the other answers is correct about moving the initialisation of the variable outside the loop and correcting where the { goes.

    Generally it's written like Alive to Die pointed out with a while() loop...

    $numbers_rows = 1;
    while($row = $result->fetch()){
    

    The one thing I wanted to add was your echo of the value is using a pre increment (add 1 then use the value), so setting the value to 1 before the loop will mean the first value printed will be 2.

    Change the echo to...

    echo $number_rows++;
    

    to ensure it starts with 1.