我正在努力学习,所以我不会伸出援手,直到我用尽了所有其他途径。我希望在这里得到一些帮助/指导,因为我在Stack Overflow(靠近页面底部)上找到的解决方案之一似乎不适合我。几个小时后,我准备扔掉毛巾了!我发现一些参考资料告诉我,随着时间的推移,各种扩展已经贬值和/或完全删除,所以我现在想知道这个代码片段是否更旧,并且由于其他原因没有运行(尽管我知道我可能也错过了一些东西)。作为参考,我在共享服务器上,所以我不能更改PHP版本(5.6.40)或mySQL版本(5.6.46)。
我的表被命名为(用于测试)。我只有三列:, , 。邻接数据的最大深度为 4。我正在尝试构建一个数组,然后可以转换(我在想)。根据我复制/粘贴/修改的代码片段,我认为它可以工作,但即使在使用和之后,我在屏幕上也没有得到任何东西。在 Chrome 开发工具中,没有连接错误。test_jsonidparentnamejson_encode($arr)print_rvar_dump
这是我正在使用的代码:
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;
}
}
探索选项几个小时后的更新:
克里斯托斯和达尔曼是对的,我试图使用的代码有许多贬值(删除?)的扩展名。现在我已经修复了这些问题并且没有错误(使用error_reporting(E_ALL); ini_set(“display_errors”,1); ini_set(“display_startup_errors”,1);)我仍然感到困惑,为什么我只得到这个输出:数组( )
从下面的代码。关于我缺少什么的任何其他建议?感谢您提供的任何帮助。我可能正看着它,但我的眼睛开始交叉。
胡说叔叔