0

Hello for 5 days i really been searching and trying, but i think i cant array the tree or i cant really understand how to echo a select box with tree format like

<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>

My database estructure is

id | parent_id | name

I have been really close for example here im just missing a 3er level symbol, and so on.

https://stackoverflow.com/questions/16474767/asign-symbol-to-subcategory-inside-subcategory-in-selectbox-php

EDIT

how do you build an array like this from my database estructure, im learning, i havent learn arrays

$datas = array(
    array('id' => 1, 'parent' => 0, 'name' => 'Page 1'),
    array('id' => 2, 'parent' => 1, 'name' => 'Page 1.1'),
    array('id' => 3, 'parent' => 2, 'name' => 'Page 1.1.1'),
    array('id' => 4, 'parent' => 3, 'name' => 'Page 1.1.1.1'),
    array('id' => 5, 'parent' => 3, 'name' => 'Page 1.1.1.2'),
    array('id' => 6, 'parent' => 1, 'name' => 'Page 1.2'),
    array('id' => 7, 'parent' => 6, 'name' => 'Page 1.2.1'),
    array('id' => 8, 'parent' => 0, 'name' => 'Page 2'),
    array('id' => 9, 'parent' => 0, 'name' => 'Page 3'),
    array('id' => 10, 'parent' => 9, 'name' => 'Page 3.1'),
    array('id' => 11, 'parent' => 9, 'name' => 'Page 3.2'),
    array('id' => 12, 'parent' => 11, 'name' => 'Page 3.2.1'),
    );

This array would make this code works, =( , i think

<?PHP 

$datas = mysql_query("SELECT id, parent_id, name FROM category");

function generatePageTree($datas, $depth = 0, $parent = 0){
    if($depth > 1000) return ''; // Make sure not to have an endless recursion
    $tree = '';
    for($i=0, $ni=count($datas); $i < $ni; $i++){
        if($datas[$i]['parent'] == $parent){
            $tree .= str_repeat('-', $depth);
            $tree .= $datas[$i]['name'] . '<br/>';
            $tree .= generatePageTree($datas, $depth+1, $datas[$i]['id']);
        }
    }
    return $tree;
}

echo(generatePageTree($datas));


?>
aynber
  • 22,380
  • 8
  • 50
  • 63

1 Answers1

0

I would use a few for loops and recursive function to do this. Pretty much what it'll do is take each result from your query, and search for children in the results. Use a query that gets the id and parent_id for all the rows. You'll also want a function that turns the results into a 2-d array with the first layer is the row, and then the column i.e. $result[0]['ID']; would be the id for the first row.

You could use a query like

SELECT id, parent_id, name from YOUR_TABLE_HERE;

  $num=count($results);
  for($i=0;$i<$num;$i++) {
       $id=$results[$i]['id'];
       if($results[$i]['parent_id']===null) {
             echo "<option>".$results[$i]['name']."</option>";
        }
       for($j=0;$j<$num;$j++) {
           if($results[$j]['parent_id']==$id) {
                search($results[$j][$id],$results,"-");
            }
       }
    }
 //the level_delim is just the string that signifies the level i.e. ---
  function search($id, $results,$level_delim) {
     $num=count($results);
     for($i=0;$i<$num;$i++) {
           if($results[$i]['parent_id']==$id) {
                echo "<option>".$level_delim."-".$results[$i]['name']."</option>"
                search($results[$i]['id],$results,$level_delim."-");
            }
      }
 }
  function allResults(mysqli_result $result) {
   $array=array();
   for($row=0;$row<mysqli_num_rows($result);$row++) {       //get all the rows and gett array
       $array[$row]=  mysqli_fetch_assoc($result);
   }
   return $array;
}
Micah
  • 119
  • 4
  • Thanks man! , as i said, i have troubles setting the query cause maybe im doing something wrong. thats why i put my database structure, as you can see.. if you can give an example of a query for the code you give me , it would be great. – DreaminMedia Queretaro May 11 '13 at 18:21
  • really??? with that simple query ?? jaja maybe that was my prob in other posts i see very complex querys so i did not understan a bit of that.. This is very very clear, actually.. i will test right away – DreaminMedia Queretaro May 11 '13 at 19:50
  • Parse error: syntax error, unexpected 'search' (T_STRING), expecting ',' in this part search($results[$i]['id'] – DreaminMedia Queretaro May 11 '13 at 19:55
  • 1
    There is something fishy about this, no offence. Say id 7 has parent_id 5. when $id=5 it comes to a call to search(...) you put $results[$j][$id], which does not exist. You probably mean $results[$j]['id'], so you call search(7,result...). The if statement in search will never be true, because there is no other element that has parent_id =7 and thus nothing will ever happen. Or am I mistaken? – bouscher May 11 '13 at 19:59
  • Give us your an answer!! :D it would be nice to see what you will do. $results[$j]['id'] yes send Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 7205 bytes) in /Applications/MAMP/htdocs/Catalog/admin/regproducto.php on line 32 – DreaminMedia Queretaro May 11 '13 at 20:02
  • after 2 tries and fixed all syntax erros .. He is right this code produces an infinite loop , thanks @bouscher – DreaminMedia Queretaro May 11 '13 at 20:04
  • Either infinite loop or nothing. – bouscher May 11 '13 at 20:05
  • Well at least he has an idea. 5 days, and i cant make a single selectox with tree jaja, damn , there has to be a simple way, they are very common – DreaminMedia Queretaro May 11 '13 at 20:20
  • He is definitely not far off, recursion is the way to go. Just have a look here, this should give you the answer: http://stackoverflow.com/questions/14740429/flat-php-array-to-hierarchy-tree – bouscher May 11 '13 at 20:32
  • Yep been there, in that post, as i said before, i have tried to build and array like that in tree, i cant, i just simple not know much about arrays, and make one in a tree form jaaj is far i think. maybe not, but i have tried to.. no luck at all.. – DreaminMedia Queretaro May 11 '13 at 20:40
  • There are some typos in my code, I wrote this in a few minutes so it's not perfect, and needs refinement. But recursion is your answer you just need to find a good way to start the recursion off. So it looks like the function would be good, we'll just need a better handler to start the function off. – Micah May 12 '13 at 18:36