Re: nested sets?
| From: | Cal Evans | Date: | Fri, 21 Nov 2003 17:24:52 +0000 |
| Subject: | Re: nested sets? | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-31690@lists.php.net to get a copy of this message | ||
Chapter 28 (or 29) in Joe Celko's SQL for Smarites. The chapter on nested sets alone is worth the price of the book. At the very end of the chapter (after taking you through the details of a nested set) he gives a very simple and easy to implement solution for nested sets.
Let me know how I may be of service,
=C=
* Cal Evans
* http://www.eicc.com
* We take care of your IT,
* So you can take care of your business.
*
* I think inside the sphere.
ma wrote:
hi everybody! i try to make a clickable menu. it should be outputed as XML. i found a solution to make it possible to decend only the selected menues. but is there a easier solution? my goal is to first show only the first (level=0) level. if the user clicks on the menu it should select the second (level=1) level, but only from the menu he clicked. (so HAVING level<=1 doesn't help :( ) does anybody know a solution or a good reference for nested sets? (don't have the possibility to use pear - unfortunately cause there are some great tools) here's my db-structure: id (int) unsigned auto_increment left (int) unsigned right (int) unsigned level (int) unsigned (default: 0) title (varchar) link (varchar) menu (int) unsigned (default: 0) heres my solution (using singleton registry pattern and a sql-class working with adodb-functions): ####################################### $reg = ®istry::instance(); $sql = &$reg::getEntry('sql'); # $_GET['menu'] = 'About/Company/History' // <- something like this$qry = 'SELECTfirst.left,first.id,first.link,first.title,first.level, ROUND((first.right-first.left-1)/2) ASchildrenFROM'.$grz->cnf['tablePluginPrefix'].'menuASfirst,'.$grz->cnf['tablePluginPrefix'].'menuASsecond'; $open = array(); if(isset($_GET['menu'])) { $open = explode(';', $_GET['menu']); $or = join(' ORthird.id=', $open); $qry .= ','.$grz->cnf['tablePluginPrefix'].'menuASthirdWHERE (first.leftBETWEENsecond.leftANDsecond.rightANDfirst.leftBETWEENthird.leftANDthird.rightANDfirst.level<=third.level+1 AND (third.id='.$or.')) OR (first.leftBETWEENsecond.leftANDsecond.rightANDfirst.level=0) '; } else { $qry .= 'WHEREfirst.leftBETWEENsecond.leftANDsecond.rightAND####################################### thx a lot for taking your time, help would be warmly appretiated... -ma # life would be easier if i knew the source code...first.level=0 '; } $qry .= 'ANDfirst.menu='.$menuID.' GROUP BYfirst.leftORDER BYfirst.left'; $res = $sql->query($qry);