Categories and Subcategories, please help |
[eluser]satanrulehis[/eluser]
I have read a great article from http://sqllessons.com/categories.html about how to get all category items in 1 SQL. The table looks like this Code: create table categories Add some values to it Code: INSERT INTO `categories` (`id`, `name`, `parentid`) VALUES Now the magic which SQL statement Code: select root.name as root_name Then we have a result root_name down1_name down2_name down3_name animal birdie NULL NULL animal doggie companion chihuahua animal doggie companion poodle animal doggie herding collie animal doggie herding shepherd animal doggie hunting pointer animal doggie hunting setter animal doggie hunting terrier animal gerbil NULL NULL animal horsie NULL NULL animal kittie NULL NULL mineral feldspar NULL NULL mineral gypsum NULL NULL mineral quartz NULL NULL mineral silica NULL NULL vegetable carrot NULL NULL vegetable celery NULL NULL vegetable potato NULL NULL vegetable rutabaga NULL NULL vegetable tomato NULL NULL The problem I have : how to display them in HTML in style ULs? Example: *animal obirdie odoggie +companion #chihuahua #poodle +herding #collie Can anyone help me? I use CodeIgniter certainly Thanks alot for reading
[eluser]jedd[/eluser]
How would you do this in conventional PHP? The differences to get it going under CI would be pretty trivial, but if you show us your existing code it should be easy to point them out.
[eluser]andrewtheandroid[/eluser]
Have you read this post? "Want to show categories with subcategories" If you use one table as phil suggested the sql is simpler as to find a root you have parent id = 0. Then you could write a recursive function to create sub-catagories based on your result array. You could use the array_map and make your callback recursive. Code: // Get root categories If you wanted to use your approach then maybe you could for each row your results have the structure Code: // From your sql result your rows should have the structure The above is just pseudo code and has not been tested just to give you the algorithm. |
Welcome Guest, Not a member yet? Register Sign In |