NYCPHP Meetup

NYPHP.org

[nycphp-talk] Building trees

Jim Hendricks(Biz Computing) jim at bizcomputinginc.com
Wed Oct 16 19:45:32 EDT 2002


>
>
>Sure, multiple queries would do it, and depending on the environment
>may be the best way.  I've had to tackle similar problems, and found
>that a single dynamically generated SQL query is often best.  If you
>form the joins properly (LEFT JOIN table ON something=somethingelse)
>you can match much of this functionality (I even use it for subselects,
>and have only ran into a wall once).
>
>In a for loop, bascially just join the table to itself (or to another
>for that matter) with an incrementing aliased name and then set the
>criteria at each level in the WHERE clause.  This is all Oracle's
>CONNECT TO is doing behind the scenes - you can even maintain a counter
>so you know how far each row is "indented."
>
>Here's some code totally ripped from context that probably won't help
>at all:
>
>for( $i = 1; $i < count($keys); ++$i) {
>   $tag = mysql_escape_string($keys[$i]);
>   $select .= ",m$i.mid,m$i.R_mid,m$i.file,m$i.tag,m$i.title";
>   $from .= " LEFT JOIN psite.map m$i ON m$i.R_mid=m".($i-1).".mid";
>   $where .= " AND m$i.tag='$tag'";
>}
>
>$result = mysql_query("SELECT $select FROM $from WHERE $where", $DB);
>  
>
I wouldn't say it's useless ( at least not yet ;-)  ) I just don't have 
the mental capacity left after a day banging on my keyboard, I'll have 
to look at this with a fresh brain ( how do you get one of those anyway? )

My initial knee-jerk response is how do I get what I want through JOINS? 
 That may allow me to connect records together, but in my case I'm 
looking to order the data in a strange ( at least to SQL )way. Unless 
you are looking to create a derived record which contains a string 
encapsulating the whole branch.  If that's the case, then I question 
what the performance would be to parse the results into a displayable 
format.

Jim





More information about the talk mailing list