<?php
// I have a table `categoreis` to store products' categories information
// it use categories_id and parent_id to identify the level relationship between those categories
// now I want a function(tep_get_leaf_categories() below) to get all leaf categories for a passed in category_id
// say tep_get_leaf_categories(360) will get all leaf categories id as an array of category 360
// this function get all subcategories, or child-categories for $category_id
function tep_get_subcategories($category_id) {
$subcategories_raw = tep_db_query('SELECT ca.categories_id
FROM categories ca
LEFT JOIN categoreis cb
ON ca.parent_id = cb.categories_id
WHERE cb.categoreis_id = ' . (int) $category_id);
$subcategories = array();
if (mysql_num_rows($subcategories_raw) >= 1) {
while ($row = mysql_fetch_assoc($subcategories_raw)) {
$category_id = $row['categories_id'];
$subcategories[] = $category_id;
}
}
return $subcategories;
}
// this is the function that I'm working on, to get the leaf category of $category_id
function tep_get_leaf_categories($category_id) {
$sub_categories = tep_get_subcategories($category_id);
// no sub categories found:return self as result
if (count($sub_categories) === 0) {
return array($category_id);
} else {
foreach ($sub_categories as $category_id) {
tep_get_leaf_categories($category_id);
// what next?
}
}
}