recursion - Recursive PHP function gets only first values -


i'm having issue below code. i'm trying time now, , believe must blind , not see obvious. can point @ obvious thing?

function ought categories , it's names. takes ids correctly, names first category , first subcategory.

function collect($id, $current = null) {     global $db_1;      $s = "select category.id id, category_i18n.name name category,category_i18n category.parent_id= '$id' , category.visible = '1' , category_i18n.culture='en' , category.id = category_i18n.id order category.order_asc asc";     $r = mysql_query($s, $db_1);     $row = mysql_fetch_array($r);     $children = array();     if (mysql_num_rows($r) > 0) {         while ($row) {             $children['name'] = $row['name'];             $children[$row['id']] = collect($row['id']);         }     } else     {             $children['name'] = $row['name'];     }      return $children; } 

edit:

edit2:

after changes i'm getting array this:

array(3) {   ["name"]=>   string(9) "cat"   ["id"]=>   string(5) "10404"   ["children"]=>   array(3) {     ["name"]=>     string(10) "subcat"     ["id"]=>     string(5) "10410"     ["children"]=>     bool(false)   } } 

i think not problem mysql query, in phpmyadmin works , shows data properly...

updated :

it seems have 1 level of subdirectory. function :

1) first fetches name , id of given category. 2) uses obtained id fetch name of subcategory has parent_id = fetched id. 3) here comes problem. due recursion again calls collect() id of subcategory. there no subcategory this, returns null.

that is,

1st parent (id1) -> subcategory(id2) -> subcategory(id2) , on.

you can use many levels of categories want :

function collect($id, $current = null) {     global $db_1;      $s = "select category.id id, category_i18n.name name category,category_i18n category.parent_id= '$id' , category.visible = '1' , category_i18n.culture='en' , category.id = category_i18n.id order category.order_asc asc";     $r = mysql_query($s, $db_1);     $temp = array();     if(mysql_num_rows($r) > 0)     {         while($row = mysql_fetch_array($r))         {              $temp[$row['id']]['name'] = $row['name'];             $temp[$row['id']]['children'] = collect($row['id']); //get further children (recursive)             if(!$temp[$row['id']]['children']) {continue;} // if false returned, means category has no sub categories. move onto next one.         }     }     else {return false;} //no rows present, return false      return $temp; } 

this code not tested. array in following format

$parent[parent_id] -> [name]                     -> [children] -> false (if subdirectories not present)                                   -> [children_id] #1 -> [name]                                                       -> [children]                                   -> [children_id] #2 , on. 

update : getting last result, because forgot 1 thing. $temp[] array getting overwritten every time in loop.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -