PHP、MySQL:只需显示一次父名称。。差不多了,但还是有问题
我有一个包含所有汽车名称的父类别,由表“parent”中的parent_name表示。对于这些父母中的每一位来说,可能有任意数量的汽车模型&它们都放在一张名为“模型”的表格中。每个模型都可以有任意数量的图像&通过模型id作为外键引用。我的任务是只显示一次所有父级名称(如在一个组中),同时列出该父级下的所有模型,仅显示一个对应的图像。父\u名称不应显示多次 我的实验:PHP、MySQL:只需显示一次父名称。。差不多了,但还是有问题,php,mysql,group-by,grouping,Php,Mysql,Group By,Grouping,我有一个包含所有汽车名称的父类别,由表“parent”中的parent_name表示。对于这些父母中的每一位来说,可能有任意数量的汽车模型&它们都放在一张名为“模型”的表格中。每个模型都可以有任意数量的图像&通过模型id作为外键引用。我的任务是只显示一次所有父级名称(如在一个组中),同时列出该父级下的所有模型,仅显示一个对应的图像。父\u名称不应显示多次 我的实验: //Table parent parent_id parent_name 1 Par1 2
//Table parent
parent_id parent_name
1 Par1
2 Par2
//Table model
model_id parent_id model_name
1 1 Model1
2 2 Model2
3 1 Model3
4 1 Model4
5 2 Model5
//Table model_images
image_id model_id
1 1
2 1
3 1
4 2
5 3
6 3
Par1 ---> This is the parent. Needs to be shown only once.
Model1 --> This is a model. List all models that belong to this parent.
image_id 1 -> Show only 1 image of the model (model may have multiple images but I need just one)
Model3 --> This is a model.
image_id 5 -> Show only 1 image of the model
Model4 --> This is a model.
No Image -> Note that no image exists for this model. So we show "No Image" text.
------------------------------------------------------------
Par2 ---> This is the parent. Needs to be shown only once.
Model2 --> This is a model.
image_id 4 -> Show only 1 image of the model
Model5 --> This is a model.
No Image -> Note that no image exists for this model. So we show "No Image" text.
我基本上试着写两个查询。一种方法是左键联接“models”上的“parent”表并按parent\u id使用GROUP,然后在while循环中,通过使用model\u id字段联接models和images表,编写另一个查询以仅获取1个图像。但这样做只列出一个模型,即使有多个模型。因此,我尝试使用GROUP BY parent_id,model_id。使用它确实会显示所有模型,但同时也会重复显示parent_名称&我需要在整个页面中仅显示一次parent_名称。您可以说,我正在尝试将模型名称分组到父项下&在单个父项下显示所有模型,并且仅显示模型的一个图像。如果我能避免多次显示家长的名字,问题就会解决
以下是我的表格模式:
//Table parent
parent_id parent_name
1 Par1
2 Par2
//Table model
model_id parent_id model_name
1 1 Model1
2 2 Model2
3 1 Model3
4 1 Model4
5 2 Model5
//Table model_images
image_id model_id
1 1
2 1
3 1
4 2
5 3
6 3
Par1 ---> This is the parent. Needs to be shown only once.
Model1 --> This is a model. List all models that belong to this parent.
image_id 1 -> Show only 1 image of the model (model may have multiple images but I need just one)
Model3 --> This is a model.
image_id 5 -> Show only 1 image of the model
Model4 --> This is a model.
No Image -> Note that no image exists for this model. So we show "No Image" text.
------------------------------------------------------------
Par2 ---> This is the parent. Needs to be shown only once.
Model2 --> This is a model.
image_id 4 -> Show only 1 image of the model
Model5 --> This is a model.
No Image -> Note that no image exists for this model. So we show "No Image" text.
所需输出:
//Table parent
parent_id parent_name
1 Par1
2 Par2
//Table model
model_id parent_id model_name
1 1 Model1
2 2 Model2
3 1 Model3
4 1 Model4
5 2 Model5
//Table model_images
image_id model_id
1 1
2 1
3 1
4 2
5 3
6 3
Par1 ---> This is the parent. Needs to be shown only once.
Model1 --> This is a model. List all models that belong to this parent.
image_id 1 -> Show only 1 image of the model (model may have multiple images but I need just one)
Model3 --> This is a model.
image_id 5 -> Show only 1 image of the model
Model4 --> This is a model.
No Image -> Note that no image exists for this model. So we show "No Image" text.
------------------------------------------------------------
Par2 ---> This is the parent. Needs to be shown only once.
Model2 --> This is a model.
image_id 4 -> Show only 1 image of the model
Model5 --> This is a model.
No Image -> Note that no image exists for this model. So we show "No Image" text.
我需要PHP和mySQL代码来实现上述功能。感谢您对解决此问题的所有帮助
多谢各位
编辑1:
对不起,我忘了加这个。我是非面向对象的程序员。因此,如果您能在解决方案中避免使用面向对象代码,并以非面向对象的方式向我展示这些代码,我将非常感激。谢谢。您可以在一个查询中执行此操作,然后将其合并到关联数组中:
$query = ' SELECT *
FROM parent AS p
LEFT JOIN model AS m
ON p.id = m.parent_id
LEFT JOIN model_images AS m_i
ON m.model_id = m_i.model_id';
$array = array();
if($mysli->query($quer)){
while($row = $result->fetch_assoc()){
$array[$row['parent_name']][$row['model_id']] = $row;
}
}
然后将有一个关联数组,其父名称作为数组的键。然后,您可以使用for循环只打印一次键(使用$i=0),其余的值按值打印
这够清楚吗
编辑:您的数组可能如下所示:
Array(
'Par 1' =>
Array(
[0] => Array(
'parent_id' => 1,
'parent_name' => 'Par 1',
'model_id' => 1,
'model_name' => 'Model 1',
'image_id',
),
[1] => Array(...)
),
'Par 2' => Array(...)
)
所以要打印出来,需要两个循环。一个给父母(还有他们的名字),一个给他们的孩子(本例中的模特)
foreach($par\u name=>$models的数组){
回显“父项名称:”.$par_名称。“
”;
echo“Model ID:”.$models[0]['Model_ID']。,Model Name:“.$models[0]['Name'];//替换为所需的输出
}
现在知道它是如何工作的了吗?当然,正如Artefactor所说,如果你不喜欢OOP函数,你可以使用过程函数。从我在第一段中读到的内容来看,我不明白为什么只想显示一次父项名称(我解释为只应显示一行)。我相信您可以使用mysql的array_accum aggregate(或其他任何名称)将父级的所有模型分组到一列中,并将每个模型的一个图像分组到另一列中,但这并没有什么优势。至于其余的——tl;dr,但如果您想要SQL查询,则应以表格形式显示所需的输出。@谢谢您的回复。我之所以只想展示一次,是因为我试图将所有模型分组到一个父模型下。因此,如果100个模型以10个模型/父对象的速率共享10个父对象,那么我不想显示父对象的名称100次(因为我们有100个模型),我只想显示每个父对象的名称一次&显示与该父对象对应的10个模型。我真的不在乎这是否需要在表或基于div的布局中显示。我只需要输出。除了父名称不断重复之外,我几乎做到了这一点&我需要避免这种情况。好的,我现在明白了,您不一定只想在SQL中执行这种冗余削减。考仔的答案看起来不错。它仍然会获取100行,但是它会将它们分组到一个PHP数组下。谢谢你的回复。很抱歉,我不理解您提供的代码(这是因为我不了解基于OOPS的编程)。我也编辑了我的问题以反映同样的情况。你能用非面向对象的方式给我看一下吗?@Denver如果你不想使用面向对象,只需使用
mysqli\u query
和mysqli\u fetch\u assoc
而不是方法调用。@artifact我在下面的评论中发布了回复。请检查它。请参阅我的编辑1下面。谢谢