I am trying to learn as I go so I don't reach out for help until I've exhausted all other avenues. I would appreciate some help/direction here because one of the solutions I found on Stack Overflow (near the bottom of the page) does not seem to work for me. After several hours, I'm ready to throw in the towel! I've found references informing me that various extensions have been depreciated and/or removed altogether over time so I'm now wondering if this code snippet is older and doesn't run for a different reason (although I'm aware that I might also be missing something). As reference I am on a shared server so I can't change the PHP version (5.6.40) or the mySQL version (5.6.46).
My table is named (for testing) test_json
. I only have three columns: id
, parent
, name
. The adjacency data is max 4-deep. I am trying to build an array that I can then convert (I'm thinking json_encode($arr)
). Based on the code snippet I copied/paste/modified, I thought it would work but I get nothing on the screen even after using print_r
and var_dump
. In Chrome dev tools there are no connection errors.
Here is the code I'm using:
echo '<pre>';
$categories = OrgChart::getTopCategories();
print_r($categories);
echo '</pre>';
class OrgChart
{
public static function getTopCategories()
{
return self::getCategories('parent = 0');
}
public static function getCategories($where = '')
{
if ($where) $where = " WHERE $where";
$result = mysql_query("SELECT * FROM test_json $where");
$categories = array();
while ($category = mysql_fetch_array($result)){
$my_id = $category['id'];
$category['children'] = OrgChart::getCategories("parent = $my_id");
$categories[] = $category;
}
mysql_free_result($result);
return $categories;
}
}
Updates after several hours of exploring options:
Christos and Dharman were right and the code I attempted to use had a number of depreciated (removed?) extensions. Now that I've fixed those and have zero errors (using error_reporting(E_ALL); ini_set("display_errors", 1); ini_set('display_startup_errors', 1);) I'm still stymied why I only get this output: Array ( )
from the code below. Any other suggestions on what I'm missing? Thank you for any help that can be provided. I could be looking right at it but my eyes are starting to cross.
echo '<pre>';
$categories = OrgChart::getTopCategories();
print_r($categories);
echo '</pre>';
class OrgChart
{
public static function getTopCategories()
{
return self::getCategories('parent = 0');
}
public static function getCategories($where = '')
{
if ($where) $where = " WHERE $where";
// $result = mysqli_query(&db, "SELECT * FROM test_json $where");
Database::initialize();
$result = mysqli_query (Database::$conn, "SELECT * FROM test_json $where");
$categories = array();
while ($category = mysqli_fetch_array($result)){
$my_id = $category['id'];
$category['children'] = OrgChart::getCategories("parent = $my_id");
$categories[] = $category;
}
// mysql_free_result($result);
return $categories;
}
}