Actually the problem is this: there is a table, there are 3 fields in it (id, main, sub), id is just an index, main is the user id, who invited, sub - who invited.

Ie this is a module for DLE for referrals. And I need to display a list in the admin panel (nameplate), for example, User1 invited user2,3,4,5, and User2 invited user 6,7,8.

It seems to me that a cycle in a cycle is not the best option, because if there are 10 people, each invited 10 friends, then these are 110 requests. And given that I still get the ID for the nickname and the number of his orders, it is already 310. A lot of truth.

$db->query("SELECT distinct main FROM `" . PREFIX . "_refferal` LIMIT 10"); $db2 = new db; $db3 = new db; while ($row = $db->get_row()) { if ($bgcolor == "lightgrey") { $bgcolor = "white"; } else { $bgcolor = "lightgrey"; } $db3->query("SELECT name,skidka FROM `" . PREFIX . "_users` WHERE user_id='$row[main]'"); $unf = $db3->get_row(); $db3->query("SELECT id FROM `" . PREFIX . "_zakaz` WHERE user='$row[main]' AND (status='input' OR status='input')"); $zk = $db3->num_rows(); echo "<table border=0 width=600 align=center> <tr ><td width=257 align=center bgcolor=$bgcolor><a href='?do=refferals&act=full&id=$row[main]'>$unf[name]</a> ($unf[skidka]%) Заказов : $zk</td> <td><table>"; $db2->query("SELECT * FROM `" . PREFIX . "_refferal` WHERE main='$row[main]'"); while ($row2 = $db2->get_row()) { $db3->query("SELECT name,skidka FROM `" . PREFIX . "_users` WHERE user_id='$row2[sub]'"); $unf = $db3->get_row(); $db3->query("SELECT id FROM `" . PREFIX . "_zakaz` WHERE user='$row2[sub]' AND (status='input' OR status='input')"); $zk = $db3->num_rows(); if ($bgcolor2 == "lightgreen") { $bgcolor2 = "white"; } else { $bgcolor2 = "lightgreen"; } echo "<tr bgcolor=$bgcolor2><td width=300><a href='?do=refferals&act=full&id=$row[main]'>$unf[name]<a/> ($unf[skidka]%) Заказов : $zk</td></tr>"; } echo "</table></td></tr></table>"; } 

    2 answers 2

    First, as I understand it, you work through PDO or some other ORM system. Secondly, why so many forms of connection to the database are created, I don’t understand, you can work in the context of one connection. Thirdly, there are a lot of unnecessary requests, look towards JOIN-requests.

    • Class for the database Native DLE Unfortunately even in the context of one is impossible. Because if in a large cycle we run another subquery, the previous one disappears. So I also thought about join, I need to study their structure - lnart
    • Then be sure to look in the direction of JOIN because there you can resolve everything with a pair (or even one in general, if you can build correctly) once made requests and then work with the results without touching the base anymore. Significantly affect the speed of the whole. For example, in one application we now execute JOIN through 18 tables and it turns out a huge array of data that the rest of the code works with, without touching the base at all. Prior to that, a bunch of intermediate SELECTs and loops were executed there. - Shamanis

    I agree with the answer Shamanis. I want to clarify. Suppose we have a table of your invited refferal with the fields id, main, sub. And the zakaz order table with the main and zakaz_id fields. Make a request to the database:

     SELECT `refferal`.*, count(`zakaz`.`zakaz_id`) as `zakaz_count` FROM `refferal` INNER JOIN `zakaz` ON (`zakaz`.`main` = `refferal`.`main`) LIMIT 10 

    In response, we just see all the information that I HAVE UNDERSTOOD you need to know:

     id main sub zakaz_count 1 1 3 3 2 1 2 3 3 2 4 2 4 3 5 1 и т.д. 

    This is for you simply as an aid. Of course, you can do better ...