问题描述
我正在尝试按记录的优先级对记录进行分组,例如
I'm trying to group down records by their priority levels, e.g.
记录...
---优先级:中--
记录...
---优先级:低---
记录...
类似的事情,如何在PHP中做到这一点? while循环通过具有int值的优先级列对记录进行排序(高= 3,中= 2,低= 1).例如 WHERE priority ='1'
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
<?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.推荐答案
如果您确定结果是按优先级排序的,那么琐碎的事情就这样:
If you're sure the results are ordered by priority then something as trivial as this:
$priority = null;
while($row = mysql_fetch_array($result))
{
if( $row['priority'] != $priority )
{
echo '<h1>Priority Level: ' . $row['priority'] . '</h1>';
$priority = $row['priority'];
}
echo $row['name'];
}
换句话说,您在$priority
变量中跟踪当前优先级.然后测试在if
条件下优先级是否已更改.如果是这样,请echo
优先级并将当前优先级设置为在当前行中找到的优先级.
In other words, you keep track of the current priority level in the $priority
variable. Then test whether the priority has changed in the if
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.
这篇关于从while循环对记录进行分组|的PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!