Good evening friends! I am a beginner in php, but I passionately dream to master this path tightly. Please help me understand the situation and understand how such things are done:

enter image description here

While stuck here on this (Bad):

$mass_pro = [$composition]; $sql = "SELECT * FROM products"; $result = $con->query($sql); foreach($result as $row) { if (in_array($row['id'], $mass_pro)) { echo $row['name']; } } 

But there is a suspicion that somewhere has gone astray. Please help. Thank!

  • one
    And why do we keep the composition data in such a way, a perversion IMHO ... And if you need to find all the dishes that contain Pork? Why not use the iDProduct-IdProduct Id intermediate table. Yes, and categories also need to be taken out separately (for example, what will you do if you want to rename a category?). In general, the face is completely wrong organization of data in the database. - Ella Svetlaya
  • @EllaSvetlaya Thank you! The situation is that I still do not really understand PDO and I don’t know how to perform intersection of data from one table with another in the course of executing the code, therefore, I use this option as a temporary solution. I will try, of course, as you suggested, but it will confuse me even more. Could you, based on your option, jot down a code, how will it work approximately? - Incognito
  • @Incognito PDO has nothing to do with your question at all. No need to "cross" any table using PHP. First you need to make a SQL query that selects the data in the desired form. And then execute this request and literally display your example code. So you need to learn SQL, not PHP. If you make such a structure as suggested by Ella, then the request will be of the form select t1.name, t1.description, group_concat(t2.name) from t1,t2,t3 where t1.id=t2.blud_id and t3.id=t2.prod_id group by t1.name, t1.description . - Mike
  • @EllaSvetlaya is all right, but not so sharp. This is not a perversion, but ignorance. An answer that describes the basic concepts of relational databases using this example would be very useful for the site. - Ipatiev

1 answer 1

"Such things" are in no way stored in this form in the database. Such tables are built using grouping. For example:

 select id ,group_CONCAT(id_of_composition) as composition from table group by type; 

This is the case if you need to group identifiers. In the case of names, the same function will be used, but the request will be a little more complicated.


More complicated example:

There are tables

 composition (составляющие) | id_comp | name | -------------------- |1 | молоко | |2 | мясо | |3 | кофе | |4 | масло | |5 | хлеб | dish (блюда) | id_dish | name | -------------------------------- |1 | кофе с молоком | |2 | бутерброд с мясом | |3 | бутерброд с маслом | entry (вхождения) | id_entry | id_dish | id_comp| -------------------------------- |1 | 1 | 3 | |2 | 1 | 1 | |3 | 2 | 5 | |4 | 2 | 2 | |5 | 3 | 5 | |6 | 3 | 2 | 

Request:

 Select d.name ,group_CONCAT(c.name) as composition From composition c ,dish d ,entry e Where d.id_comp = e.id_comp and e.id_dish = d.id_dish Group by d.name, 
  • Well, the author just asks how to build a query that will be "a little more difficult" - Ipatiev
  • @Ipatiev, I gave the author a function to get several results in one line, which the author actually needed. The complete scheme would be available, the request would be written a little more difficult in response - Sanek Zhitnik
  • and it uses a query to a single table without a join. To be honest, this posting is much more suitable for comment than for an answer. - Ipatiev
  • @ Ipatiev in the request did not specifically use complex structures for the beginner. - Sanek Zhitnik