基于字段的PHP SQL循环结果

基于字段的PHP SQL循环结果,php,sql,loops,Php,Sql,Loops,我正在创建一个动态记录集,在该记录集中,我向用于选择所有墨迹记录的查询填充一对值 接下来,我需要循环和分组结果,但不确定如何根据查询中使用的值进行分组 参考查询: SELECT fldItem, fldDescription, fldRank,fldType FROM tableName WHERE fldType IN (33, 34, 36) ORDER BY fldRank 值33、34和36将动态地传递给查询。 我的目标是创建一个循环/重复区域,允许分组显示共享同一fldType的所有

我正在创建一个动态记录集,在该记录集中,我向用于选择所有墨迹记录的查询填充一对值

接下来,我需要循环和分组结果,但不确定如何根据查询中使用的值进行分组

参考查询:

SELECT fldItem, fldDescription, fldRank,fldType FROM tableName
WHERE fldType IN (33, 34, 36) ORDER BY fldRank
值33、34和36将动态地传递给查询。 我的目标是创建一个循环/重复区域,允许分组显示共享同一fldType的所有结果

结果应如下所示:

item1, description1, rank1, type33
item2, description2, rank2, type33
item3, description3, rank5, type33
item4, description4, rank6, type33
item5, description5, rank8, type33

--------

item6, descriptionv, rank1, type34
item7, descriptionw, rank2, type34
item8, descriptionx, rank3, type34
item9, descriptiony, rank4, type34
item10, descriptionz, rank5, type34

--------

item11, descriptionA, rank2, type36
item12, descriptionB, rank3, type36
$last_type = null;
foreach ($records as $record) {
    if ($last_type !== null && $last_type != $record->fldType) {
        echo '<whatever your visual cue is>';
        $last_type = $record->fldType;
    }
}
使用do/while循环会显示所有要显示的项目,但不会显示任何分组。ForEach循环是解决方案吗

SELECT fldItem, fldDescription, fldRank,fldType FROM tableName
WHERE fldType IN (33, 34, 36) ORDER BY fldType, fldRank
这将首先按fldType分组,然后按fldType分组。在PHP代码中,只需记住最后一个fldType,并在它发生更改时添加一个新组。所有FLD类型现在都将分组在一起,因此您可以按顺序循环查看结果


这将首先按fldType分组,然后按fldType分组。在PHP代码中,只需记住最后一个fldType,并在它发生更改时添加一个新组。现在所有FLD类型都将分组在一起,因此您可以按顺序循环结果。

如果您在报告输出中谈论的是可视化分组,那么您需要在每次循环迭代中跟踪该值,然后在检测到更改时发出可视化中断。大概是这样的:

item1, description1, rank1, type33
item2, description2, rank2, type33
item3, description3, rank5, type33
item4, description4, rank6, type33
item5, description5, rank8, type33

--------

item6, descriptionv, rank1, type34
item7, descriptionw, rank2, type34
item8, descriptionx, rank3, type34
item9, descriptiony, rank4, type34
item10, descriptionz, rank5, type34

--------

item11, descriptionA, rank2, type36
item12, descriptionB, rank3, type36
$last_type = null;
foreach ($records as $record) {
    if ($last_type !== null && $last_type != $record->fldType) {
        echo '<whatever your visual cue is>';
        $last_type = $record->fldType;
    }
}

如果您正在谈论报表输出中的可视分组,那么您需要为每个循环迭代跟踪该值,然后在检测到更改时发出可视中断。大概是这样的:

item1, description1, rank1, type33
item2, description2, rank2, type33
item3, description3, rank5, type33
item4, description4, rank6, type33
item5, description5, rank8, type33

--------

item6, descriptionv, rank1, type34
item7, descriptionw, rank2, type34
item8, descriptionx, rank3, type34
item9, descriptiony, rank4, type34
item10, descriptionz, rank5, type34

--------

item11, descriptionA, rank2, type36
item12, descriptionB, rank3, type36
$last_type = null;
foreach ($records as $record) {
    if ($last_type !== null && $last_type != $record->fldType) {
        echo '<whatever your visual cue is>';
        $last_type = $record->fldType;
    }
}

您是否使用GROUP BY尝试过不同的SQL语句?这将分组返回结果。然后,您可以使用循环,当类型发生更改时,让它退出换行符或任何您想要区分不同类型的内容。GROUP BY不以组为单位返回结果,而是为组返回单个结果。例如,在此处使用GROUP BY只会返回三行。是的,最初我尝试使用GROUP BY,但在()中与选择一起使用时发现它不起作用。您是否对GROUP BY尝试过不同的SQL语句?这将分组返回结果。然后,您可以使用循环,当类型发生更改时,让它退出换行符或任何您想要区分不同类型的内容。GROUP BY不以组为单位返回结果,而是为组返回单个结果。也就是说,在这里使用GROUP BY只会返回三行。是的,最初我尝试使用GROUP BY,但在()中与选择一起使用时发现它不起作用。是的,这将以上面引用的方式返回结果,但我正在尝试获取由“----------”分隔符显示的组。在实际情况下,这是因为结果(每个组)将被设置为一个格式化的表。是的,这将以上面引用的方式返回结果,但我正在尝试获取由“----------”分隔符显示的组。在实际情况下,这是因为结果(每组)将被设置为一个格式化的表格。是的,我相信这就是我要寻找的。谢谢是的,我相信这就是我要找的。谢谢