PHP:回显结果编号
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
做一个计数器:
在你的情况下:
Make a counter:
In your case:
我在这里没有看到提到的另一种解决方案是使用有序列表:
如果您不特别关心 PHP 代码中的结果编号,这将为您提供更“正确”的 HTML。 (没有什么可以阻止您同时使用两者:使用有序列表来输出数字,但为其他事物保留一个计数器,例如
rel
属性或交替的 CSS 类。)One other solution I don't see mentioned here is using an ordered list:
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:
有点野蛮,但为什么不保留一个柜台呢?
A little bit brutish, but why not just keep a counter?
内联计数器,为了胜利。
这与这里的任何其他答案没有本质上的不同,只是它短了 1 行,并且实际上利用了您可以内联使用增量器的事实。
Inline counters, for the win.
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.