PHP:回显结果编号

发布于 2024-12-02 07:19:44 字数 719 浏览 1 评论 0原文

我正在使用 mysql_query 从数据库中获取大量结果。我需要做的是回显结果编号以及结果本身。

换句话说,如果我的查询获取 3 个结果,我希望第一个结果旁边有一个 1,第二个结果旁边有一个 2,依此类推。 我需要数字从 1 开始,而不是 0。

注意: 我的意思不是 mysql_num_rows 因为它只告诉我有多少结果,而不是结果编号本身。

这是我的查询信息:

$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
  }

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
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧话新听 2024-12-09 07:19:44

做一个计数器:

$count = 1;

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

在你的情况下:

$count = 1;

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

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']."'>";
}
蓝戈者 2024-12-09 07:19:44

我在这里没有看到提到的另一种解决方案是使用有序列表:

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

如果您不特别关心 PHP 代码中的结果编号,这将为您提供更“正确”的 HTML。 (没有什么可以阻止您同时使用两者:使用有序列表来输出数字,但为其他事物保留一个计数器,例如 rel 属性或交替的 CSS 类。)

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.)

灯角 2024-12-09 07:19:44

只要这样做:

$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++;
  }

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++;
  }
安静 2024-12-09 07:19:44
$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;
    $i ++;
}
手心的温暖 2024-12-09 07:19:44
$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++;
} 
$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++;
} 
老娘不死你永远是小三 2024-12-09 07:19:44

有点野蛮,但为什么不保留一个柜台呢?

$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;
}

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;
}
瀞厅☆埖开 2024-12-09 07:19:44

内联计数器,为了胜利。

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

这与这里的任何其他答案没有本质上的不同,只是它短了 1 行,并且实际上利用了您可以内联使用增量器的事实。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文