$sql = $db->query_read("SELECT * FROM `" . TABLE_PREFIX . "xsdl_cats` WHERE 1"); while ($row = $db->fetch_array($sql)) { if($row['sub_cat']) { $cats[$row['par_cat']] = $row; } else { $cats['0'] = $row; } } foreach($cats as $key => $value) { if($key > 0) continue; echo $value['cat_name'].'<br />'; foreach($cats as $k => $v) { if($k == 0 OR $k != $key) continue; echo '- '.$v['cat_name'].' | '; } }