Jump to content

[SOLVED] How do I add a counter to a while loop?


poleposters

Recommended Posts

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";

   
}

 

<?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";
   
   
   }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.