poleposters Posted July 22, 2009 Share Posted July 22, 2009 Hi, I'm selecting records from my database using a while loop.I would like to print a number that corresponds with the order in which the records are retrieved. ie The first record displayed would print the number 1 next to it, the second the number 2 etc.I've read up on counters, but I can't seem to get them to work for this particular example. $query="SELECT * FROM photos WHERE album='$album'"; $result=mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); while($photos=mysql_fetch_array($result)) { $url=$photos['photo_url']; $thumb=$photos['thumb_url']; $caption=$photos['caption']; $number=??????????????????????????????????? print "$url $thumb $caption $number"; } Link to comment https://forums.phpfreaks.com/topic/166988-solved-how-do-i-add-a-counter-to-a-while-loop/ Share on other sites More sharing options...
Batosi Posted July 22, 2009 Share Posted July 22, 2009 <?php $query="SELECT * FROM photos WHERE album='$album'"; $number = 0; $result=mysql_query($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error()); while($photos=mysql_fetch_array($result)) { $number++; $url=$photos['photo_url']; $thumb=$photos['thumb_url']; $caption=$photos['caption']; $number=??????????????????????????????????? print "$url $thumb $caption $number"; } ?> Link to comment https://forums.phpfreaks.com/topic/166988-solved-how-do-i-add-a-counter-to-a-while-loop/#findComment-880413 Share on other sites More sharing options...
poleposters Posted July 22, 2009 Author Share Posted July 22, 2009 Thank you! I tried something similar however I declared $number = 0; inside the while loop. No wonder it didn't work. Thanks again. Link to comment https://forums.phpfreaks.com/topic/166988-solved-how-do-i-add-a-counter-to-a-while-loop/#findComment-880417 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.