Page 1 of 1

Recursive function with unsorted list

Posted: Tue Jan 15, 2008 7:57 pm
by ratamaster
Hi, I need help with a recursive function
This function must show an undefined number of nested categories and their products

I'm having troubles with the closing tags (</>) from <ul><li> tags
This is the function:

Code: Select all

 
function list_tree($p = 0){
  $arrStr = array();
  $db1 = new db_driver();
  $db1->query('SELECT id, name FROM categories WHERE CatParentId ='.$p.' ORDER BY CatOrder');            
  if($db1->get_num_rows()>0){      
    if($p>0){//cuando es mayor a 0 definimos el siguiente ul
      array_push($arrStr,'<ul style="display:none">');  
    }
    while($db1->fetch_row_object()){                
      array_push($arrStr,'<li id="idtree_'.$db1->row->id.'" class="catnode">'.$db1->row->name);
      //these are category products
     //I comment the next if bacause this part is fine
      /*$db2 = new db_driver();                    
      $db2->query('SELECT products.* FROM products, prodcat WHERE products.id = prodcat.id AND prodcat.id ='.$db1->row->id.' AND products.idNOT IN (SELECT tickets.id FROM tickets, products WHERE tickets.id=products.id) ORDER BY ProdOrder'); 
      if($db2->get_num_rows()>0){
        array_push($arrStr,'<ul style="display:none">');
        while($db2->fetch_row_object()){
          array_push($arrStr,'<li id="id_'.$db2->row->ProdId.'" class="prodnode">"id_'.$db2->row->name.'"</li>');
        }
        array_push($arrStr,'</ul>');
      }//fin de los productos */     
      array_push($arrStr,list_tree($db1->row->id))
      array_push($arrStr,'</li>')   
    }
    array_push($arrStr,'</ul>');
  }   
  return implode('',$arrStr);
} 
 
the <ul style="display:none"> are closed very after they should be closed

Thanks and sorry with my english :)

Re: Recursive function with unsorted list

Posted: Tue Jan 15, 2008 9:23 pm
by s.dot
You could just get the return from the function as an array. And then use implode() to get into a list.

Re: Recursive function with unsorted list

Posted: Tue Jan 15, 2008 9:50 pm
by ratamaster
hmm.. yes you are right, I will test your idea.. but is it not the same? ... I mean, at the end I'll have a multidimensional array

Re: Recursive function with unsorted list

Posted: Wed Jan 16, 2008 1:51 am
by VladSun
There is no need to use arrays - use simple string concatenation.

Code: Select all

 
$arrStr = '';
........
$arrStr .= "HTML here";
 

Re: Recursive function with unsorted list

Posted: Wed Jan 16, 2008 5:47 am
by ratamaster
VladSun, Pushing into an array or string concatenation "is the same", I choose an array because it's faster than string concatenation. This is not the problem, the problem is the structure of the function, not the why of storing the tree.

Re: Recursive function with unsorted list

Posted: Wed Jan 16, 2008 6:55 am
by VladSun
Ok .... My code to help you:

Code: Select all

 
function list_prods($id)
{
    return "<br />Product list here\n";
}
 
function list_tree($id = 0)
{
    
    $html = '';
    $result = mysql_query('SELECT id, bg_name FROM cats WHERE super_id ='.$id);
 
    if(mysql_num_rows($result)>0)
    {      
        $html .= "<ul>\n";  
 
        while($row = mysql_fetch_assoc($result))
        {                
            $html .= "<li>".$row['bg_name']."\n";
            $html .= list_prods($row['id']);
            $html .= list_tree($row['id']);
            $html .= "</li>\n";
 
        }
        $html .= "</ul>\n";
 
    }  
    return $html;
} 
 
echo list_tree();
But still, I can't agree with you about using arrays instead of strings ...
Look it yourself:
http://ipclassify.relef.net/2.php
http://ipclassify.relef.net/2.phps
...
Almost twice (it depends on the $add length) slower. And with $add length grows, your code gets slower.

Re: Recursive function with unsorted list

Posted: Wed Jan 30, 2008 8:24 am
by ratamaster
you are right, thank you for your help and the array-string issue :) I changed all to string concatenation