2 回答
TA贡献1827条经验 获得超4个赞
您可以像这样解析子类别,但是每次检查父类别具有哪些类别时,它确实需要进行 sql 连接:
function cats(){
$sql = "SELECT * FROM `cats";
$cats;
$con = mysqli_connect($this->vars["host"], $this->vars["user"], $this->vars["pass"], $this->vars["db"]);
if(mysqli_connect_errno()){
print "START MYSQLI ERROR<br/>".
mysqli_connect_error() .
"<br/>ENDMYSQLI ERROR";
}else{
$res = mysqli_query($con, $sql);
if(mysqli_num_rows($res) > 0){
while($cat = $res->fetch_array(MYSQLI_ASSOC)){
if($cat["parent-id"] == "0"){
$cats[$key] = $cat["cat-name"];
$sql_subCategory = "SELECT * FROM cats where parent_id=". $cat["id"];
$res_sub = mysqli_query($con, $sql_subCategory);
if(mysqli_num_rows($res_sub) > 0){
while($cat_sub = $res_sub->fetch_array(MYSQLI_ASSOC)){
$cats[$cat["cat-name"]] = $cat_sub["cat-name"];
}
}
}
}
}
mysqli_free_result($res_sub);
mysqli_free_result($res);
mysqli_close($con);
return $cats;
}
那应该给你数组:
array(
'cat1' => 'Some Category',
'cat2' => array(
'sub1' => 'name',
),
);
- 2 回答
- 0 关注
- 142 浏览
添加回答
举报