Php multi-dimensional array from mysql result Php multi-dimensional array from mysql result arrays arrays

Php multi-dimensional array from mysql result


$query = mysql_query("SELECT * FROM table WHERE uid = '1' ORDER BY id DESC");$results = array();while($line = mysql_fetch_array($query, MYSQL_ASSOC)){    $results[] = $line;}


This does not include the sql functions for gathering the data but I made a table for my issue (was the same exact problem) This is my table also by the way. So its more of example than an explanation.

This is not a full on explanation but is a example code.


TABLE:
[ ID | Parent | name |href | title ]


CODE:

            foreach ($query->result_array() as $row){    if ($row['parent'] === null)        $data[$row['id']][0]= '<a href="'.$row['href'].'"'.($row['title']!==null)?'title="'.$row['title'].'"':''.'>'.$row['name'].'</a>';    else     {        $temp = '<a href="'.$row['href'].'"'.($row['title']!==null)?'title="'.$row['title'].'"':''.'>'.$row['name'].'</a>';        array_push($data[$row['parent']],$temp);    }}

I used this to generate my links for my navbar. Then I used a function to make a multi level list out of them. For my issue. Its very similar issue but this was my solution.

If you would like. Instead of making your own version. I can make a similar code using your database scheme for the data instead..

Warning: It seems this might only work on a two level layout at moment. Ill improvise code some more and post the next version of my snippet.


$query = mysql_query("SELECT * FROM table WHERE uid = '1' ORDER BY id DESC");$results = array();$num_fields=mysql_num_fields($query);$num_rows=mysql_num_rows($query);while($line = mysql_fetch_array($query)){for($i=0;$i<$num_rows;$i++{  for($j=0;$j<$num_fields;$j++  {  $results[$i][$j]=$line[$i][$j];  }}}