PHP,MySQL:只需要显示一次父名称..几乎已经完成,但仍然有问题
我有一个父类别,其中包含所有汽车名称,由表“parent”中的parent_name 表示。对于每个父母来说,可能有任意数量的汽车型号和车辆。它们都放在名为“模型”的表中。这些模型中的每一个都可以有任意数量的图像和模型。通过 model_id 作为外键引用。我的任务是仅显示所有父级名称一次(如在一组中),同时列出该父级下的所有模型以及仅 1 个相应的图像。 Parent_name 不应显示多次。
我的实验:
我基本上尝试编写 2 个查询。一种是在“模型”和“模型”上左连接“父”表。使用 GROUP BY Parent_id ,然后在 while 循环中编写另一个查询,通过连接模型和模型来仅获取 1 个图像。使用 model_id 字段的图像表。但这样做仅列出 1 个模型,即使有多个模型。所以我尝试使用 GROUP BY Parent_id, model_id。使用此功能确实会显示所有模型,但同时也会重复显示parent_name &我需要parent_name 在整个页面中只显示一次。你可以说我正在尝试将 model_name 分组到父级和父级下。显示单亲下的所有模型,我只显示模型的 1 张图像。如果我可以避免多次显示parent_name,问题就可以解决。
以下是我的表架构:
//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: 抱歉,我忘了添加这个。我是非面向对象程序员。因此,如果您能够在解决方案中避免面向对象的代码并以非oops 的方式向我展示相同的代码,我将非常感激。谢谢。
I have a parent category that holds all Cars names, denoted by parent_name in table "parent". For each of these parents, there could be any number of car models & they all go in table called "model". Each of these models can have any number of images & refereced via the model_id as the Foreign Key. My task is to show all the Parent Name only once (as in a group) and at the same time, list all the models under that Parent with just 1 corresponding image. The parent_name should not be shown more than once.
MY EXPERIMENTS:
I basically tried to write 2 queries. One was to left join "parent" table on "models" & use GROUP BY parent_id and then in the while loop, write another query to fetch only 1 image using by joining the models & images tables by using model_id field. But doing this lists only 1 Model, even though there are multiple models. So I tried to use GROUP BY parent_id, model_id. Using this does show all the models but at the same time, also repeats showing the parent_name & I need the parent_name to show only once throughout the page. You can say that I am trying to GROUP the model_name under the parent & show all the models under a single parent and I am showing only 1 image of the model. If somehow I can avoid showing the parent_name multiple times, the issue would be solved.
Following are my table schemas:
//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
DESIRED OUTPUT:
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.
I need the PHP & mySQL code to achieve the above. All help in resolving the issue is appreciated.
Thank you very much.
EDIT 1:
Sorry, I forgot to add this. I am non-object oriented programmer. So I would really be thankful if you can avoid object oriented code in your solution and show me the same in a non-oops way. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在一个查询中执行此操作,然后将其组合到一个关联数组中:
您将拥有一个关联数组,其中父级名称作为数组的键。然后,您可以使用 for 循环仅打印一次键($i = 0),但按值打印其余值。
这够清楚了吗?
编辑:您的数组可能如下所示:
因此要打印出来,您需要两个循环。一张给父母(还有名字),一张给他们的孩子(在本例中是模特)。
现在知道它是如何工作的了吗?当然,正如 Artefacto 所说,如果您不喜欢 OOP 函数,您可以使用过程函数。
You might do it in one query and than combine it to an associative array:
You will than have an associative array with the parent name as the key of the array. You can then use a for loop to print the key only once (with $i = 0) but the rest value by value.
Is that clear enough?
EDIT: Your array than might look like this:
So to print out you need two loops. One for the parents (and there names) and one for their childs (models in this case).
Now an idea of how it works? An sure as Artefacto said, you can use procedural functions if you don't like OOP functions.