Jump to content


Photo

Category Tree


  • Please log in to reply
4 replies to this topic

#1 End User

End User

    Member

  • Members
  • PipPip
  • 309 posts
  • Gender:Male
  • Interests:PHP, mySQL, and a number of other things, not necessarily computer-related.
  • Languages:php, javascript, css, html, perl, mysql

Posted 10 June 2010 - 04:01 PM

I think someone may have posted a similar question not long ago, but this might be a bit different. I'm looking for some category management code that will work with an arbitrary number of categories and subcategories, to any reasonable depth.

I've found a few so far (see below), but I'm wondering if anyone has any other suggestions, something they've used and would recommend? I've found quite a few examples of category tree code that are inefficient and rack up a large number of queries needlessly as they recursively build the tree, so I'm hoping to find something that's efficient or "thrifty" if you will.

I found this page that has an example that seems to fit the bill, but there's no clear example given:
http://bytes.com/top...s-subcategories (scroll down a bit)

Takes just one query and then it hands the dirty work off to PHP. This is what I'd like to do, but looking at what's there doesn't quite clue me in.


Here's a category class that also seems promising:
http://www.phpclasse...s-database.html

So...any suggestions or code snippets to share?
I don't live on the edge, but sometimes I go there to visit.

#2 boen_robot

boen_robot

    XSLT senior

  • Moderator
  • PipPipPipPipPipPipPip
  • 8,486 posts
  • Gender:Male
  • Location:europe://Bulgaria/Plovdiv
  • Interests:Everything having to do with computers... and science.
  • Languages:(X)HTML, CSS, XML, XSLT, Schema, PHP, JavaScript (a little), other XML based...

Posted 10 June 2010 - 04:21 PM

If you really need arbitrary levels, you should probably consider using tags instead. You know, where a certain thing is part of one or more tags, and you can filter stuff based on if it has or hasn't a certain tag.

The DB structure to implement it is relatively easy to construct and work with. You have one table for tags (with tag ID and the actual tag), a table with the "stuff" that the tags apply to (ID and all), and a third table with two primary keys (and nothing more) - the ID of the thing, and the tag ID.
The greatest difficulty in programming is not in finding answers, but in asking yourself the right questions. -- If nobody has said it before, then I'd like to take credit of thinking this up (during summer of 2010).
"Complex problems often have the simplest solutions" -- Not sure who said that first.
=== My projects (all feedback welcomed) ===
XML_XSLT2Processor(0.5.3) - perform XSLT 2.0 transformations in PHP.
PEAR2_Net_Transmitter(1.0.0a4) - reliable sockets.
PEAR2_Cache_SHM(0.1.2) - persistent data storage wrapper.
=== Useful tools ===
NetBeans - full featured PHP IDE, as well as a decent code editor for other things.
Fiddler2 - The best free HTTP debugger. Performance tuning, security check, integrity check, custom requests and more, all made easy.
Gobby - That's NOT my Nickname! Look at the topic.

#3 wirehopper

wirehopper

    Member

  • Members
  • PipPip
  • 385 posts
  • Languages:PHP, javascript, XHTML/CSS, mobileOK

Posted 10 June 2010 - 07:34 PM

Display only, meaning this is just to view the categories, not manage them?

Edited by wirehopper, 10 June 2010 - 07:37 PM.


#4 End User

End User

    Member

  • Members
  • PipPip
  • 309 posts
  • Gender:Male
  • Interests:PHP, mySQL, and a number of other things, not necessarily computer-related.
  • Languages:php, javascript, css, html, perl, mysql

Posted 10 June 2010 - 11:32 PM

Display only, meaning this is just to view the categories, not manage them?

Yes, just to display them, probably in a tabled "drill-down" format (or in a dropdown box). I just need the category generation code, I can write the back-end stuff to manage the categories (add, edit, delete).
I don't live on the edge, but sometimes I go there to visit.

#5 wirehopper

wirehopper

    Member

  • Members
  • PipPip
  • 385 posts
  • Languages:PHP, javascript, XHTML/CSS, mobileOK

Posted 11 June 2010 - 12:07 AM

Assuming the number of categories, including necessary HTML is within the bounds of an acceptable page size (meaning <60K?), what about using a cron job to write the HTML into a static file, which uses simple DIV/ul/li/hide/toggle to display the categories and drill down. The page content can be delivered either using AJAX or by reloading the page.

If you used a SELECT statement with ORDER BY `parent_category`,`category` and then a loop through the resulting data, it might work.

The page could be .shtml or .php - including the static HTML.

Although the initial page load would have extra content, navigation through the categories would be entirely client-side, and content delivery would be limited to only the requested content, also, you could use the title tag to create a breadcrumb navigation path at the top of the page.

If you post or PM the schema - I'll think on it a bit more ...

Edited by wirehopper, 11 June 2010 - 12:08 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users