Answer the question
In order to leave comments, you need to log in
Nested SQL query, optimization solutions?
Good time of the day.
I'm working on a small php project, separating code and design, I came up with the following:
<? foreach($categories as $cat): ?>
<li><p class="categories"><?=$cat['title']?></p>
<foreach(getSubCat($cat['cat_id']) as $subcat): ?>
<p class="subcats"><?=$subcat['title']?></p>
<? endforeach ?>
</li>
<? endforeach ?>
Answer the question
In order to leave comments, you need to log in
SELECT `cat`.`id` AS `catId`, `cat`.`title` AS `catTitle`, `subcat`.`title` AS `subcatTitle`
FROM `categories` AS `cat`
LEFT JOIN `subcategories` AS `subcat` ON `cat`.`id` = `subcat`.`cat_id`
ORDER BY `cat`.`id`
<?php
$cat = -1;
foreach ($result as $row) {
if ($row['catId'] != $cat) {
echo ($cat == -1 ? "" : "<\li>")."<li><p class='categories'>{$row['catTitle']}</p>";
$cat = $row['catId'];
}
echo "<p class='subcats'>{$row['subcatTitle']}</p>";
}
if ($cat != -1)
echo "</li>";
?>
keep comrade
phpforum.ru/txt/index.php/t56444.html
www.sideralis.org/baobab
Mike77 , thanks for the theory, but indeed SELECT with union can achieve the same results "on the fly" (and I didn't read the union options before and in vain ...), only then you have to divide the received data into several arrays. Not a frail volume can work out, for my particular case it is suitable, but it is not true for everyone.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question