1

このようなデータベースを作成しました

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
  Men clothing
Women
  Women cloth

メニューバーまたはコンボボックスで解決策を見つけようとしましたが、すべてがうまくいきませんでしたこの関数を使用しましたが、このように印刷されています

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;
}
4

0 に答える 0