I'm trying to develop a function that will manage product categories in a simple e-commerce app.
I've created a categories table and each category has a parent category that identifies its origin. I developed a category class with a function whose aim is to collect all categories below a particular category and put the id values in an array, then return the array. Below is the syntax
function getAllSubCategories($category_group_id)
{
//compose query
$query01 = "SELECT categories_id ".
"FROM ".TABLE_CATEGORIES." ".
"WHERE (categories_id > 0) AND ".
"(parent_id = '".$category_group_id."') ".
"ORDER BY categories_id";
//execute query
$query_result = cj_db_query($query01, $link = 'db_link');
//$notice = $this->checkIfSubCategoryExists($category_group_id);
//get the category ids of sub categories
while ($row = mysql_fetch_array($query_result, MYSQL_ASSOC))
{
$category_id = $row["categories_id"];
//check if a subcategory exists for the category
$notice = $this->checkIfSubCategoryExists($category_id);
if ($notice = "yes")
{
$this->getAllSubCategories($category_id);
}
else if ($notice = "no")
{
$this->category_tree_arr[] = $category_id;
}
}
return $this->category_tree_arr;
}
It's a recursive function but I haven't been able to get it working properly.
I would really appreciate any tips on how to develop recursive functions since that's the only way I think it will work without overbloating the code.
Thanks a lot.