query($sql); $data['manufacturer'] = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { //categories //echo "
";print_r($row); $cat_sql = "SELECT term_id as id, name, slug FROM wp_terms where term_id in (SELECT term_id FROM wp_term_taxonomy where parent = '$row[id]' and taxonomy = 'product_cat') order by name asc"; $cat_result = $conn->query($cat_sql); if ($cat_result->num_rows > 0) { while($cat_row = $cat_result->fetch_assoc()) { //sub categories $subcat_sql = "SELECT term_id as id, name, slug FROM wp_terms where term_id in (SELECT term_id FROM wp_term_taxonomy where parent = '$cat_row[id]' and taxonomy = 'product_cat') order by name asc"; $subcat_result = $conn->query($subcat_sql); if ($subcat_result->num_rows > 0) { while($subcat_row = $subcat_result->fetch_assoc()) { $sub_category[] = array( 'cat_id' => $subcat_row['id'], 'cat_name' => $subcat_row['name'], 'cat_slug' => $subcat_row['slug'], ); } } else { $sub_category = array(); } $category[] = array( 'cat_id' => $cat_row['id'], 'cat_name' => $cat_row['name'], 'cat_slug' => $cat_row['slug'], 'sub_category' => $sub_category, ); unset($sub_category); } } else { $category = array(); } $data['manufacturer'][] = array( 'cat_id' => $row['id'], 'cat_name' => $row['name'], 'cat_slug' => $row['slug'], 'category' => $category ); unset($category); } } return $data; } $categories = get_categories($cat_id=9); header('Content-Type: application/json'); echo json_encode($categories);