简体   繁体   中英

PHP: Echo Result Number

I am fetching a number of results from my database using a mysql_query . What I need to do is echo the result number, along with the result itself.

In other words, if my query fetches 3 results, I would like the first result to have a 1 beside it, and the second result a 2 and so on. I need the numbers to start at 1, not 0.

note: I do not mean mysql_num_rows as that only tells me how many results, not the result number itself.

Here is my query information:

$primary_img_query = "SELECT imgURL, imgTitle FROM primary_images WHERE primaryId=16";
$primary_img_data = mysql_query($primary_img_query) or die('MySql Error' . mysql_error());

while($row = mysql_fetch_assoc($primary_img_data)) { 
    echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'>";//this is where I want the result number echoed
  }

Make a counter:

$count = 1;

while(...){
   echo $count++;
}

In your case:

$count = 1;

while($row = mysql_fetch_assoc($primary_img_data)) { 
   echo $count++;
   echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'>";
}

One other solution I don't see mentioned here is using an ordered list:

$result = mysql_query(...);

echo "<ol>\n";
while ($row = mysql_fetch_array($result))
{
    echo "<li><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></li>";
}
echo "</ol>\n";

This gives you more "correct" HTML, if you don't particularly care about the result number in your PHP code. (Nothing stops you from using both, either: use an ordered list to output the number, but keep a counter for other things, like rel attributes or alternating CSS classes.)

Just do:

$counter = 1;
while($row = mysql_fetch_assoc($primary_img_data)) { 
    echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'>";//this is where I want the result number echoed
    echo $counter++;
  }
$i = 1;
while($row = mysql_fetch_assoc($primary_img_data)) { 
    echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'>" . $i;
    $i ++;
}
$i = 1;
while($row = mysql_fetch_assoc($primary_img_data)) {
      echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'>" . $i;//this is where I want the result number echoed
      $i++;
} 

A little bit brutish, but why not just keep a counter?

$primary_img_query = "SELECT imgURL, imgTitle FROM primary_images WHERE primaryId=16";
$primary_img_data = mysql_query($primary_img_query) or die('MySql Error' . mysql_error());
$n=0;

while($row = mysql_fetch_assoc($primary_img_data)) { 
    $n++;
    echo "<img src='new_arrivals_img/thumbnails/".$row['imgURL']."'>" . $n;
}

Inline counters, for the win.

$i = 0;
while($row = mysql_fetch_assoc($primary_img_data))
{ 
    echo ++$i . "<img src='new_arrivals_img/thumbnails/" . $row['imgURL'] . "'><br>";
}

This is not materially different than any other answer here, other than it's 1 line shorter and actually leverages the fact that you can use incrementors inline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM