Hello i want a solutions for categories off subcategories in array n level .
- 1 like
- 1 comment
Mukund verma |08 Sep at 11:09
Hello i want a solutions for categories off subcategories in array n level .
Sanjana Kumari08 Sep at 12:09
<?php
$hostname = "localhost";
$db_username = "root";
$db_password = "";
$db_name = "test";
$db = new mysqli($hostname, $db_username, $db_password, $db_name);
class categoryss {
public function get_categories($id){
global $db;
$querycat='';
$gcategories=array();
if(is_null($id)){
$querycat = "SELECT cat_id,cat_name,parent_id FROM categories WHERE parent_id='0'";
}else{
$querycat = "SELECT cat_id,cat_name,parent_id FROM categories WHERE parent_id='".$id."'";
}
$result = $db->query($querycat);
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
if(!empty($row['cat_id']) && $row!=''){
$gcategories[]=$row;
if(!empty($this->get_categories($row['cat_id']))){
$gcategories[]['child']=$this->get_categories($row['cat_id']);
}
}
}
}
return $gcategories;
}
////////////////////////////////////////
}
$obj=new categoryss();
$getNlevelCategory=$obj->get_categories('0');
/*echo "<pre>";
print_r($getNlevelCategory);*/
function traverseArray($array) {
if(is_array($array)){
foreach($array as $vals) {
echo @$vals['cat_name'] . "<br>";
traverseArray(@$vals['child']). "\n";
}
}
}
echo traverseArray($getNlevelCategory);
?>