Php代码显示类别下的子类别

时间:2017-02-10 18:50:16

标签: php loops mysqli

我从数据库中获取类别和子标记信息,如下所示:

$cols=array("id","name","description","tags","subcat","img");
$result=$conn->get("category",null,$cols);`

请注意,结果是一个多维数组,如$result[0]['name']将显示类别名称,$result[0]['subcat']将分别显示第一个类别和子类别。 以下是片段的其余部分:

$subcat="Notset";
$cat=array();
$sub=array();
$j=0;
for ($i=0;$i<$conn->count; $i++)
{ 
    if($result[$i]['subcat'] != $subcat)
    {
        $subcat=$result[$i]['subcat'];
        $sub[$j]=$subcat;
        $j++;
    }
$cat[$i]=$result[$i]['name'];
}

但我得到的结果如下: Snapshot of web page

enter image description here

但是我想在桌面类别下显示sony,在笔记本电脑类别下显示macbook请帮助。在上面的图像中,红色按钮是绿色类别,用于子类别。

2 个答案:

答案 0 :(得分:0)

我使用以下php代码完成此操作:

# categories with a parent id of 0 are parent categories 
$selectparents = $db->prepare("
SELECT category_id, category_name 
FROM categories 
WHERE  parent_category_id = 0 
ORDER BY category_name
"); 

$selectparents->execute();

while($row = $selectparents->fetch(PDO::FETCH_ASSOC)){
        $p_category_id = $row['category_id'];
        $category_name = $row['category_name'];

        echo '<b>'.$category_name.'</b><br>';

            # get sub categories for this parent
            $selectchilds = $db->prepare("
            SELECT category_id, category_name 
            FROM categories 
            WHERE  parent_category_id = ".$p_category_id." 
            ORDER BY category_name
            ");
            $selectchilds->execute();

            while($row = $selectchilds->fetch(PDO::FETCH_ASSOC)){
            $category_id = $row['category_id'];
            $category_name = $row['category_name'];

            echo '<b>'.$category_name.'</b><br>';

            }   
}  

基本上所有父类别都将parent_id设置为0,按字母顺序查询,然后在父循环的while循环中查询每个类别的子节点,并在父节点下按字母顺序呈现。

答案 1 :(得分:0)

您可以使用以下代码:(您需要在示例中设置$fetched_categories = $result。)

function get_subcategories($array, $category_name){
    $return = array();

    foreach ($array as $key => $category){
        if ($category['subcat'] == $category_name){
            $category['sub_categories'] = get_subcategories($array, $category['name']);            
            $return[] = $category;
        }
    }

    return $return;
}

function print_categories($array, $level = 0){
    echo ($level > 0 ? '<ol>' : '<ul>');

    foreach ($array as $category){
        echo '<li>' . $category['name'] . '</li>';
        print_categories($category['sub_categories'], ++$level);
    }

    echo ($level > 0 ? '</ol>' : '</ul>');
}

$fetched_categories = array();
$fetched_categories[] = ['name' => 'Camera', 'subcat' => ''];
$fetched_categories[] = ['name' => 'Macbook', 'subcat' => 'Laptops'];
$fetched_categories[] = ['name' => 'Laptops', 'subcat' => ''];
$fetched_categories[] = ['name' => 'Desktops', 'subcat' => ''];
$fetched_categories[] = ['name' => 'Sony', 'subcat' => 'Desktops'];
$fetched_categories[] = ['name' => 'DSLR', 'subcat' => 'Camera'];

$categories = get_subcategories($fetched_categories, '');

print_categories($categories);

这是输出:

https://eval.in/734955