Php 按类别拆分sql结果
我从我正在返回的数据库中得到了一些数据,但目前它返回了一个大列表中的所有数据 对于每两个项目,数据都有一个categoryname。我想分开每一组的项目,有一个类别分配给它Php 按类别拆分sql结果,php,mysql,split,Php,Mysql,Split,我从我正在返回的数据库中得到了一些数据,但目前它返回了一个大列表中的所有数据 对于每两个项目,数据都有一个categoryname。我想分开每一组的项目,有一个类别分配给它 Example: test1 (has category testing) test2 (has category testing) test3 (has category testing123) 这些结果显示在一个大列表中,我希望结果如下: testing: test1 test2 testing123: test3
Example:
test1 (has category testing)
test2 (has category testing)
test3 (has category testing123)
这些结果显示在一个大列表中,我希望结果如下:
testing:
test1
test2
testing123:
test3
我该怎么做
我现在的代码(仅相关部分):
while($row\u items=mysqli\u fetch\u assoc($res\u items)){
$清单='';
if($row_项目['checkpoints']!=''){
$check=分解(“,”,$row_项['checkpoints');
foreach($check as$list){
$checklist.='
“.$list”。 ”;
}
}
回声'
';
}
每个结果在数据库中都有一个
cat
行,该行连接到producten_cats(一个id为cat
的表,与cat
相同,其中的类别名称以及表格行名称:naam
;很难从代码中分辨出类别名称的位置,因此我无法给您一个准确的答案
要按显示的方式拆分数据库结果集,请执行以下操作,以检测何时在数据库结果集中获得具有新category
值的第一行
$previous_category = "---nothing---";
while ($row_items = mysqli_fetch_assoc($res_items)) {
$category = $row_items['category'];
if ( $category != $previous_category ) {
$previous_category = $category;
/* show category header, for example */
echo '<h2>$category:</h2>';
}
$checkpoints = $row_items['checkpoints'];
/* show details, for example */
echo '<p>$checkpoints</p>';
}
$previous_category=“----无----”;
while($row\u items=mysqli\u fetch\u assoc($res\u items)){
$category=$row_项目['category'];
如果($category!=$previous\u category){
$previous_category=$category;
/*例如,显示类别标题*/
回显“$category:”;
}
$checkpoints=$row_项目['checkpoints'];
/*例如,显示详细信息*/
回显“$checkpoints”;
}
要使这类事情正常工作,您需要在查询中使用orderbycategory
这种技术通常被称为表示层格式化。它将MySQL提供的数据表转换为用户友好的布局。很难从代码中分辨出类别名称,因此我无法给出准确的答案 要按显示的方式拆分数据库结果集,请执行以下操作,以检测何时在数据库结果集中获得具有新
category
值的第一行
$previous_category = "---nothing---";
while ($row_items = mysqli_fetch_assoc($res_items)) {
$category = $row_items['category'];
if ( $category != $previous_category ) {
$previous_category = $category;
/* show category header, for example */
echo '<h2>$category:</h2>';
}
$checkpoints = $row_items['checkpoints'];
/* show details, for example */
echo '<p>$checkpoints</p>';
}
$previous_category=“----无----”;
while($row\u items=mysqli\u fetch\u assoc($res\u items)){
$category=$row_项目['category'];
如果($category!=$previous\u category){
$previous_category=$category;
/*例如,显示类别标题*/
回显“$category:”;
}
$checkpoints=$row_项目['checkpoints'];
/*例如,显示详细信息*/
回显“$checkpoints”;
}
要使这类事情正常工作,您需要在查询中使用orderbycategory
这种技术通常称为表示层格式化。它将MySQL表示的数据表转换为用户友好的布局