Why is this not counting correctly?
Hi guys, I am trying to make it count for each indiviual item and display the number next to it.
Code:
<?$query = mysql_query ( "SELECT * FROM `Stations` LIMIT 6" ); while ( $fea = mysql_fetch_object ( $query ) ){
$num = 1;
?>
<li>
<table border="0" style="background-color:#FFFFFF" width="100%" cellpadding="3" cellspacing="3">
<tr>
<td>
<?echo $num;?> </td>
</table>
</li><?$num++; } ?>
how ever it just echos 1 6 times, rather than 1 - 6
any ideas?
Re: Why is this not counting correctly?
because this is inside your loop
$num = 1;
so on each iteration, it's setting $num to 1 ...
Try initializing it to 1 outside of the while loop.
-tg
Re: Why is this not counting correctly?
What a mess...
PHP Code:
<?php
$query = mysql_query ( "SELECT * FROM `Stations` LIMIT 6" );
$num = 1;
while ($fea = mysql_fetch_object($query)):
?>
<li><?php echo $num++ ?></td></li>
<?php endwhile; ?>
Formatting your code isn't just something you do when you're bored; it's essential for spotting mistakes like the one you just made.