通过数组值php获取子类别的主类别

时间:2018-01-20 05:14:44

标签: php mysql arrays

我想按类别和子类别进行分类。我从用户表中选择了子类别ID。我得到了这个:

Sports Cricket
Sports Basketball
Sports Volley Ball
News Political
News Business
News Entertainment

但我需要它是这样的:

Sports 
    Cricket
    Basketball
    Volley Ball
News 
    Political
    Business
    Entertainment

这是我的代码:

<?      
$row = mysqli_fetch_array(mysqli_query($con,"select * from user where UserId ='3' and Status='act'"));
$show_str = $row["SubcatId"];

$fas = mysqli_query($con, "select * from subcategory where Status='act' ORDER BY catId Asc");
while($ad = mysqli_fetch_array($fas)){
    $SubCategory = $ad["SubCategory"]; //Sub Category Name
    $SubCatId = $ad["SubCatId"]; //Sub Category Id
    $catId = $ad["catId"]; //Category Id
    $array = explode(",", $show_str);
    foreach($array as $item){
        if($item == $SubCatId){
            $fac = mysqli_fetch_array(mysqli_query($con, "select distinct Category from category where catId='$catId'"));
            echo $Category = $fac["Category"];
?>
<?=$SubCategory;?>              
<br>
<?php
        }
    }
}
?>

0 个答案:

没有答案