while 循环中的记录分组 | PHP
我正在尝试按优先级对记录进行分组,例如
--- 优先级:高 ---
记录...
--- 优先级:中 ---
记录...
--- 优先级:低 ---
记录...
类似这样的事情,我该如何在 PHP 中做到这一点? while 循环按具有 int 值的优先级列对记录进行排序(高= 3,中= 2,低= 1)。例如WHERE优先级='1'
标签:“优先级:[优先级]”必须设置在有关其级别的分组记录之上
编辑:
<?php
while($row = mysql_fetch_array($result))
{
echo '<h1>Priority Level: ' . $row['priority'] . '</h1>';
echo $row['name'];
}
?>
就像那段代码——
tags is the label which seperates records regarding their priority level.I'm trying to group down records by their priority levels, e.g.
--- Priority: High ---
Records...
--- Priority: Medium ---
Records...
--- Priority: Low ---
Records...
Something like that, how do I do that in PHP? The while loop orders records by the priority column which has int value (high = 3, medium = 2, low = 1). e.g. WHERE priority = '1'
The label: "Priority: [priority level]" has to be set above the grouped records regarding their level
EDIT:
<?php
while($row = mysql_fetch_array($result))
{
echo '<h1>Priority Level: ' . $row['priority'] . '</h1>';
echo $row['name'];
}
?>
Like that piece of code - the
tags is the label which seperates records regarding their priority level.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确定结果是按优先级排序的,那么就像这样微不足道的事情:
换句话说,您可以在
$priority
变量中跟踪当前的优先级。然后在if
条件下测试优先级是否发生变化。如果是,则回显
优先级并将当前优先级设置为当前行中找到的优先级。请注意,只有当行按优先级排序时,这才按预期工作(真正分组一次)。换句话说,当不同的优先级没有分散在结果集中时。
If you're sure the results are ordered by priority then something as trivial as this:
In other words, you keep track of the current priority level in the
$priority
variable. Then test whether the priority has changed in theif
condition. If so,echo
the priority and set the current priority to the priority found in the current row.Mind you, this only works as expected (truly grouped once) if the rows are ordered by priority. In other words, when different priorities are not scattered across the resultset.
如果您使用 mysql 为什么不使用数据库排序功能?
If you use mysql why would not you use DB ordering capabilities?