Gregory Loginow Gregory Loginow - 3 months ago 9
MySQL Question

how to stop a ++$i auto increment after a certaint number php

I´m making this table for a contest, as you can see i'm fetching data from a database as from a array, the thing is there's only 3 prizes and like 40 participants, i want to show every participant and the prizes he's winning so far, but after the third position i want to show the "No Aplica" status next to the name, the thing is i can't stop the counter from going up nor i can make it a set number so it stays on the "No Aplica" option

<table style="display:inline;" class='table-personal table-striped'>
<thead>
<tr>
<th class="line-header">Posición</th>
<th>Premio</th>
<th>Nombre</th>
<th>Puntos Jul</th>
<th>Puntos Ago</th>
<th>Puntos Sep</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
$e = -1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
echo "<tr class='posicion'>";
echo "<td class='line-rows'>" . $premio[++$i] . "</td>";
echo "<td>" . $row["nombre"] . "</td>";
echo "<td>" . $row["puntos_julio"] . "</td>";
echo "<td>" . $row["puntos_agosto"] . "</td>";
echo "<td>" . $row["puntos_septiembre"] . "</td>";
echo "</tr>";
if ($i >= 3) {
$i = -1;
}
}
?>
</tbody>
</table>

Answer

You need to increase the value $i if the value of $i is less than 3. Otherwise no need to increase the value of $i.

Do like this:

<?php
  $i = 0;
  $e = -1;
  if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {
        $premio=array("$10.000", "$5.000", "$2.000", "No Aplica");
        echo "<tr class='posicion'>";
        echo "<td class='line-rows'>" . $premio[$i] . "</td>";
        echo "<td>" . $row["nombre"] . "</td>";
        echo "<td>" . $row["puntos_julio"] . "</td>";
        echo "<td>" . $row["puntos_agosto"] . "</td>";
        echo "<td>" . $row["puntos_septiembre"] . "</td>";
        echo "</tr>";
        if ($i < 3) {
           $i++;
        }
     }
  ?>
Comments