php - Codeigniter multi-dimensional array return only the first row -
php - Codeigniter multi-dimensional array return only the first row -
one day i'm on problem , cant find solution. goal homecoming list of links id (the father). want childs. in view, have on result (the 1st one...).
i have in controller :
$data['list_link'] = $this->menumanager->list_link();
in model :
function list_link($fatherid=0){ $r = array(); $ssql = 'select * categories fatherid = ' . $fatherid . ' , siteid = ' . my_siteid_default . ' order name'; $query = $this->db->query($ssql); // stock results in array $r[] = $query->result_array(); foreach ($query->result() $row) { // let's find childs $this->list_link($row->id,$loop); } homecoming $r; }
if "for each" $r here, looks good. so, $data['list_link'] shoud have rows.
in view :
foreach ($list_link $link){ foreach ($link $row){ echo $row['name']; } }
but, have first links (first childs), not other one. help appreciated i'm on problem days...
you're not storing values in recursive calls (though i'm still not sure you'd expect). you'd need populate $r
each function call:
$r[] = $this->list_link($row->id,$loop);
however, either missed or you're overcomplicating things, think homecoming result array , utilize it:
function list_link($fatherid=0,$loop=0){ $ssql = 'select * categories fatherid = ' . $fatherid . ' , siteid = ' . my_siteid_default . ' order name'; $query = $this->db->query($ssql); homecoming $query->result_array(); }
update
your latest version still doesn't collect info recursive calls, here total function, see if works:
function list_link($fatherid=0){ $r = array(); $ssql = 'select * categories fatherid = ' . $fatherid . ' , siteid = ' . my_siteid_default . ' order name'; $query = $this->db->query($ssql); $result = $query->result_array(); // stock results in array $r[$fatherid] = $result; foreach ($result $row) { // let's find children $r[$fatherid][$row['id']] = $this->list_link($row['id']); } homecoming $r; }
note i've added $r[$fatherid][$row['id']]
end result should array branching structure. if don't want that, $r[]
instead.
php arrays codeigniter recursion
Comments
Post a Comment