Recursive function with unsorted list

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ratamaster
Forum Newbie
Posts: 23
Joined: Fri Oct 15, 2004 6:36 am

Recursive function with unsorted list

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Recursive function with unsorted list

Post by s.dot »

You could just get the return from the function as an array. And then use implode() to get into a list.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
ratamaster
Forum Newbie
Posts: 23
Joined: Fri Oct 15, 2004 6:36 am

Re: Recursive function with unsorted list

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Recursive function with unsorted list

Post by VladSun »

There is no need to use arrays - use simple string concatenation.

Code: Select all

 
$arrStr = '';
........
$arrStr .= "HTML here";
 
There are 10 types of people in this world, those who understand binary and those who don't
ratamaster
Forum Newbie
Posts: 23
Joined: Fri Oct 15, 2004 6:36 am

Re: Recursive function with unsorted list

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Recursive function with unsorted list

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
ratamaster
Forum Newbie
Posts: 23
Joined: Fri Oct 15, 2004 6:36 am

Re: Recursive function with unsorted list

Post by ratamaster »

you are right, thank you for your help and the array-string issue :) I changed all to string concatenation
Post Reply