Jump to content

Create array from db results


samerhannoun

Recommended Posts

Hi guys ,, I want a help to create an array like this array('XXX'=>array(name=>' yyyy''VVV'=>array(name=>'uuuu'))I want this to implement nested check boxes so the values (XXX,VVV) just header not check box, so I make a field in the db table call selected as the following:IDname parent_idselectedbest wishes

Link to comment
Share on other sites

You need to use a recursive function to get the children of each box and add it to the array. e.g.:

function get_checkboxes($parent = 0){  $retval = array();  $result = mysql_query('SELECT * FROM table WHERE parent_id=' . $parent);  while ($row = mysql_fetch_assoc($result))  {	$cur = array(	  'name' => $row['name'],	  'selected' => $row['selected'],	  'children' => get_checkboxes($row['id'])	);	$retval[] = $cur;  }  return $retval;}

Link to comment
Share on other sites

You need to use a recursive function to get the children of each box and add it to the array. e.g.:
function get_checkboxes($parent = 0){  $retval = array();  $result = mysql_query('SELECT * FROM table WHERE parent_id=' . $parent);  while ($row = mysql_fetch_assoc($result))  {	$cur = array(	  'name' => $row['name'],	  'selected' => $row['selected'],	  'children' => get_checkboxes($row['id'])	);	$retval[] = $cur;  }  return $retval;}

thx alot Justsomeguy!!!I got this point but how Can I now create the checkboxes ??Sorry seems stupid question??thx again
Link to comment
Share on other sites

You'll need another recursive function to loop through the array and print everything, or the function above can print instead of store in an array.
best regards friend... thank you very much for your help and adviceBTW did you work on Zend FrameWork ?
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...