recursive function category database

后端 未结 4 1934
春和景丽
春和景丽 2021-01-06 16:57

i hoping to create a recursive function which i don\'t have an idea yet

this is my code to fetch category from database

  

        
4条回答
  •  猫巷女王i
    2021-01-06 17:35

    Something like this?

    function getCategories($mid, $categoryParent = 1)
    {
        $return = '';
        $results = mysql_query("SELECT * FROM categories WHERE category_parent = '$categoryParent' ORDER BY lft ASC", $mid);
        while($row = mysql_fetch_array($results)) {
            $return .= "
  • {$row['category_name']}
  • "; $subs = getCategories($mid, $row['category_id']); if (!empty($subs)) { $return .= '
      '; $return .= $subs; $return .= '
        '; } } return $return; } $mid = mysql_connect($host, $user, $pass); echo getCategories($mid);

    Would print out all your categories, of course fix it to exactly how you want, but that should give you an idea of how recursive functions could work

提交回复
热议问题