Php 从MySQL数据库为购物车创建分类形式的列表?

Php 从MySQL数据库为购物车创建分类形式的列表?,php,mysql,Php,Mysql,我已经创建了如下所示的数据库 cat_id cat_parent_id cat_name cat_decs 11 0 Men for men 12 0 Women for women 13 11 Men clothing 123 14 12 Women cloth 434 Men Women Men clot

我已经创建了如下所示的数据库

cat_id   cat_parent_id cat_name  cat_decs
11         0             Men       for men
12         0             Women     for women
13         11            Men clothing  123
14         12            Women cloth   434
Men
Women
Men clothing
Women cloth

function buildCategoryOptions($catId = 0)
{

    //die();
    $sql = "SELECT cat_id, cat_parent_id, cat_name
            FROM tbl_category
            ORDER BY cat_id";
    $result = mysql_query($sql) or die('Cannot get Product. ' . mysql_error());

    $categories = array();
    while($row = mysql_fetch_array($result)) {
        list($id, $parentId, $name) = $row;
        //echo $id, $parentId, $name;


        if ($parentId == 0) {
            // we create a new array for each top level categories
            $categories[$id] = array('name' => $name, 'children' => array());

        } 

            else {
            // the child categories are put int the parent category's array
            $categories[$parentId]['children'][] = array('id'=> $id, 'name'=> $name);   

        }


    }   
      echo ($categories[28]['children'][0]['name']);
    // build combo box options

    $list = '';
    foreach ($categories as $key => $value) {
        $name= @$value['name'];
        //print_r($value['name']);

        $children = $value['children'];     
        $list .= "<optgroup label=\"$name\">"; 

        foreach ($children as $child) {
            $list .= "<option value=\"{$child['id']}\"";
            if ($child['id'] == $catId) {
                $list.= " selected";
            }

            $list .= ">{$child['name']}</option>\r\n";

        }

        $list .= "</optgroup>";
        //die();
    }
    //die();
    return $list;
}
我想用这种方式打印

Men
  Men clothing
Women
  Women cloth
无论是在我的菜单栏中还是在组合框中,我都试图找到解决方案,但都毫无进展 我用过这个功能,但它是这样打印的

cat_id   cat_parent_id cat_name  cat_decs
11         0             Men       for men
12         0             Women     for women
13         11            Men clothing  123
14         12            Women cloth   434
Men
Women
Men clothing
Women cloth

function buildCategoryOptions($catId = 0)
{

    //die();
    $sql = "SELECT cat_id, cat_parent_id, cat_name
            FROM tbl_category
            ORDER BY cat_id";
    $result = mysql_query($sql) or die('Cannot get Product. ' . mysql_error());

    $categories = array();
    while($row = mysql_fetch_array($result)) {
        list($id, $parentId, $name) = $row;
        //echo $id, $parentId, $name;


        if ($parentId == 0) {
            // we create a new array for each top level categories
            $categories[$id] = array('name' => $name, 'children' => array());

        } 

            else {
            // the child categories are put int the parent category's array
            $categories[$parentId]['children'][] = array('id'=> $id, 'name'=> $name);   

        }


    }   
      echo ($categories[28]['children'][0]['name']);
    // build combo box options

    $list = '';
    foreach ($categories as $key => $value) {
        $name= @$value['name'];
        //print_r($value['name']);

        $children = $value['children'];     
        $list .= "<optgroup label=\"$name\">"; 

        foreach ($children as $child) {
            $list .= "<option value=\"{$child['id']}\"";
            if ($child['id'] == $catId) {
                $list.= " selected";
            }

            $list .= ">{$child['name']}</option>\r\n";

        }

        $list .= "</optgroup>";
        //die();
    }
    //die();
    return $list;
}
请不要使用mysql函数来编写新代码。它们不再得到维护,社区已经开始。看到了吗?相反,你应该学习并使用或。如果你不能决定哪一个,我会帮你的。如果您选择PDO。