使用 php 按组/批次显示结果
我正在创建一个应用程序,其中结果将显示在组/批次中
示例输出应该是: 第 1 批: 显示->数据库中的所有实体 第 2 批: 显示->数据库中的所有实体 第 3 批: 显示->来自数据库的所有实体
$qry = mysql_query("SELECT *,sum(counter) FROM table_name GROUP BY batch") or die("ERROR: ".mysql_error());
if(!empty($qry))
{
while($row = mysql_fetch_array($qry))
{
for($b=1;$b<=$row['sum(counter)'];$b++){
echo $b;
}
}
echo "<hr>";
echo "<div><input type='submit' name='submitBtn' value='submit'></div>";
}else{
echo "No data";
}
?>
我能够获取注册用户的数量,但问题出在我的 while 循环上(我认为).. 我得到的结果是 第 1 批: 1,2(正确) 第 2 批: 1(应该是3) 第 3 批: 1,2(应该是4,5)
任何帮助将不胜感激。
I'm creating an application wherein the results would display in a group/batch
Sample Output Should be:
Batch 1:
Display->All Entity From DB
Batch 2:
Display->All Entity From DB
Batch 3:
Display->All Entity From DB
$qry = mysql_query("SELECT *,sum(counter) FROM table_name GROUP BY batch") or die("ERROR: ".mysql_error());
if(!empty($qry))
{
while($row = mysql_fetch_array($qry))
{
for($b=1;$b<=$row['sum(counter)'];$b++){
echo $b;
}
}
echo "<hr>";
echo "<div><input type='submit' name='submitBtn' value='submit'></div>";
}else{
echo "No data";
}
?>
I was able to get the number[s] of registered users but the problem is on my while loop(I think)..
The result I am getting is
Batch 1:
1,2(Correct)
Batch 2:
1(should be 3)
Batch 3:
1,2(should be 4,5)
any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在循环中,您不断地重置 $b = 1。
尝试在 while 循环之前定义 $b=1 。
In your loop, you are constantly resetting $b = 1.
Try defining $b=1 before the while loop.
您的
for
循环没有意义。它只是从 1 计数到从sum(counter)
检索到的数字。您想要的是实际用户,因此您需要从结果中获取他们的 ID 或其他内容。另外,我强烈建议为sum(counter)
列使用别名。Your
for
loop doesn't make sense. It simply counts from 1 to the number retrieved fromsum(counter)
. What you want is the actual users, so you need to get their IDs or whatever from your result. Also I strongly recommend using an alias for thesum(counter)
column.