I am trying to make a category tree system to display endless categories. The database is setup like:
ID PARENT_ID CATEGORY_NAME
PHP Code:
$cat_array = array();
$subcat_array = array();
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con, "SELECT * FROM `documents_category` WHERE ISNULL(parent_id) ");
while($row = mysqli_fetch_array($sql))
{ $cat_array[] = $row;
//echo $row['category_name'];
}
// print_r($cat_array);
$sql2 = mysqli_query($con, "SELECT * FROM `documents_category`
WHERE parent_id IS NOT NULL ");
while($row2 = mysqli_fetch_array($sql2))
{ $subcat_array[] = $row2;
}
foreach ($cat_array as $value)
{
echo "{$value['category_name']}<br/>";
foreach ($subcat_array as $value2)
{
if($value2['parent_id'] == $value['id'])
{
echo "{$value2['category_name']}<br/>";
}
}
}
The sub categories would just use the parents id. I can get this working upto to one parent and a sub but after than nothing shows up. I have tried this a few different ways but no luck. Any advice?