I don't even have an idea of where to start for this. I need to display 100 numbers in a table and would like to use a while loop to do so. Is there a "shortcut" to doing this?
Asked
Active
Viewed 1,272 times
-3
-
2show your code. and what do you mean by `shortcut`? – urfusion May 11 '16 at 13:08
-
Im really not 100% on your issue, you just need to run a while loop and echo the number in a table cell. You can use the `modulus operator (%)` to wrap the cells in a row every `x` results to build your table – Andy Holmes May 11 '16 at 13:10
-
Start by building an HTML table with one row and then include a loop that echoes that row in every iteration with the data you want. – Jordi Nebot May 11 '16 at 13:11
-
1Read, Do, Read, Do Read, Do. The only way to learn. Or hire a programmer. – digitai May 11 '16 at 13:12
-
@urfusion, there will be no code, if some code than that will be the answer. Cause no complexity arise there. – Murad Hasan May 11 '16 at 13:19
-
@safetybrick, Your answer is ready. [Answer](http://stackoverflow.com/questions/37163669/how-do-i-display-the-output-of-a-while-loop-in-a-table/37163800#37163739) – Murad Hasan May 11 '16 at 13:19
-
check the @FrayneKonok answer in that case. – urfusion May 11 '16 at 13:20
4 Answers
2
For a table you need some tags table
, tr
and td
. The tr
and td
are in while loop and the value $i
will print inside the td
.
<table>
<?php
$i = 1;
while($i != 101){?>
<tr><td><?php echo $i++;?></td></tr>
<?php }?>
</table>

Murad Hasan
- 9,565
- 2
- 21
- 42
1
You can use while, for, foreach for your convenience, like below code
<table>
<thead>
<tr>
<th class="header">Number</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
while($i != 101){
?>
<tr>
<td><?php echo $i; ?></td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>

Deepak Dholiyan
- 1,774
- 1
- 20
- 35
0
You can dipslay 100 numbers simply in tbale like this:
<?php
// Using For loop
echo "<table>";
for ($i = 1;$i <= 100; $i++) { // use for loop
echo "<tr><td>".$i."</td></tr>";
}
echo "</table>";
// OR
// Using while loop
$i = 1;
echo "<table>";
while($i <= 100) {
echo "<tr><td>".$i."</td></tr>";
$i++;
}
echo "</table>";
?>

Jayesh Chitroda
- 4,987
- 13
- 18
0
You can use a while
loop:
while ($i <= 100) {
echo "<td><tr>" . $i++ . "</tr></td>";
}

Panda
- 6,955
- 6
- 40
- 55
-
-
@FrayneKonok Yup, didn't realise that. Updated post, thanks for pointing out – Panda May 11 '16 at 13:19