1,105,177 Community Members

php and subcategories

Member Avatar
OsaMasw
Posting Whiz in Training
212 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello there I managed somehow to create and call categories and subcategories in DB
my problem now how to design them as I want
I want it something like this
catss

I used this code to retrieve the categories

function display_children($parent) { 

    $result = mysql_query('SELECT * FROM category '. 
                            'WHERE parent_id="'.$parent.'";'); 

    // display each child 
    echo "<ul>";
    while ($row = mysql_fetch_array($result)) { 
        echo "<li class='child'><a href='cat.php?cat=".$row['cat_id']."' >".$row['cat_name']."</a></li>"; 
    } 
    echo "</ul>";
} 

$result = mysql_query('SELECT * FROM category WHERE parent_id= 0'); 
echo "<div id='cat_cont' >"; 
while ($row = mysql_fetch_array($result)) {
echo "<div class='parent' align='left'><img src='test.gif' width='30' height='30'/> <a href='ads.php?cat=".$row['cat_id']."' >".$row['cat_name']."</a></div>";
display_children($row['cat_id']);
}
echo "</div>";

and CSS for cat_cont div

#cat_cont {
  position:absolute;
  top:110px;
  margin: 0 auto; 
  background-color:#f1f1f1; 
  border-radius:10px;
  height:400px;
  width:1000px;
  border:1px black;
  }
Attachments
Member Avatar
jkon
Posting Pro in Training
442 posts since Jan 2009
Reputation Points: 110 [?]
Q&As Helped to Solve: 73 [?]
Skill Endorsements: 6 [?]
 
0
 

This is a very common problem. There are many solutions. The one you gave is one of them , to query db for each category – subcategory , one other is the nested tree model and many more. I will talk you about the Indexed List Model.

You can see an example outside OOP at http://www.daniweb.com/web-development/php/code/437655/getting-products-details-from-categories-subcategories-with-ajax-call- .

Let’s see the fundamentals of it. If your categories have id’s and parent id’s (whith 0 in parent id meaning that is a top category) there is no reason to query db for each category – subcategory. You could retrieve all active categories and have them in a multidimentional array where id’s are the keys with a recursive function (for example let’s say $cat[5] = “shoes” and $cat[5][16] = “casual” that means that shoes is a top category that has a subcategory casual …

I am very happy to see that over the last year that solution gained ground. And of course that maybe I wrote it but is not mine , the first idea of a indexed list born working with Cobol temporary tables and the first implementation was in Java and not in PHP. What you would gained using an IndexedList is that you could get an object based on the value of its property, or (if you have defined it) a sublist of the original list of objects having the same value in a specific property. (Just to comment that this architecture could apply in any OOP framework)

In PHP there is a fundamental way of reflection (with the larger meaning of the word) , variable variables , and with that way you can have a more robust indexed list of objects. I hope you don’t see that post as theoretical nonsense but give you the motive to try new solutions your self.

Question Answered as of 1 Year Ago by jkon
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article