Here is the markup of what I have so far:
<?php
$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.categories_image, c.parent_id, c.sort_order, c.date_added, c.last_modified from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id order by c.sort_order ASC");
while ($categories = tep_db_fetch_array($categories_query)){
echo '<tr class="dataTableRow">
<td class="dataTableContent">' . $categories ['categories_name'] . '</td>';
?>
<td class="dataTableContent"></td>
<?php
echo '</tr>';
}
?>
This displays all the categories and subcategories all on a page, but in no order, what i need is for the subcategories to be displayed under the main category. I have some clue as to how i would do it, a link between the sort_order
field and the parent_id
field. The main categories use the sort_order
field as id, and the subccat's use the parent_id
field to sort the category they belong to. Any ideas?
I'm not sure how to do display category tree with single MySQL query, so I'm suggesting to use recursive PHP function:
<?php
function showChildCategories($parent) {
$parent = intval($parent);
// Select only categories with parent_id = $parent
$categories_query = tep_db_query("select
cd.categories_name,
c.categories_id
from " . TABLE_CATEGORIES . " c, " .
TABLE_CATEGORIES_DESCRIPTION . " cd
where c.categories_id = cd.categories_id
and c.parent_id = $parent
order by c.sort_order ASC");
// Go through all query result rows
while ($categories = tep_db_fetch_array($categories_query)){
// Show category name
echo '<tr class="dataTableRow"><td class="dataTableContent">' .
$categories ['categories_name'] . '</td></tr>';
// Show child categories for current row
showChildCategories($categories ['categories_id']);
} // while
} // function showChildCategories
// Show top-level categories and all their children recursively
showChildCategories(0);
?>