>I have a table with id, pid(parent), title and page_name(url) fields.
>The vars provided to the script are the current page's title, id and

You can look up the parent and in the database, so it's not horribly
important that those be provided.

*UNLESS* you have a heterarchy, and not a hierarchy -- In other words,
*UNLESS* there are two different "paths" to get to 'id' through different
parents.  In other other words, *UNLESS* you have duplicate id's in the
database...  If you *DO* have a heterarchy, you'd need to track the user's
path as they traveled to know which route to display.

I'll assume you don't have a heterarchy, for now.

>When I am on the parent page I get this(Which is what I want):
>[ Chronological History ][ Website Chronological History ]
>When I am in the Child I get this:
>[ Website Chronological History ][ Website Chronological History ]
>What am I doing wrong???

You really can't get all the parents in one SQL statement.

You'll need to look up the parent.  Then the grand-parent.  Then the
great-grand-parent.  And so on.

Each with a different SQL query.

Now, this is not ideal for performance.

In fact, it's *HORRIBLE* if (A) your tree is really "deep" -- If you have 10
generations of "depth" to the ggggg'great-grand-parents, then it's gonna
take 9 queries to look it up.

If you're only looking at three or four levels, it's really no big deal... 
Unless your site is, like, getting a zillion hits.

Let's assume it's not getting a zillion hits for now, okay?

>I have stared at this two many times now and am probably missing the obvious
>function menu($id, $pid, $title) {
>    $query = "select * from meta_data WHERE pid = '$id' OR pid = '$pid' &&
>pid != 0";

This will give both the current record and the parent record, but never,
ever, ever, the grand-parent record...

>    $result = mysql_query($query);
>    $num_results = mysql_num_rows($result);
>  if($num_results != 0){
>  ?>
><table width="100%">
><tr><td align="center">
>  <?
>    echo '[ '.$title.' ]';
>    for ($i=0; $i < $num_results; $i++)
>      {
>        $row = mysql_fetch_array($result);
>        if($id == $row['id']){
>            echo '[ '.$row['title'].' ]';
>        } elseif($row['pid'] == $id || $row['id'] == $pid && $pid != 0) {
>            echo '[ <a
>href="'.$row['page_name'].'">'.$row['title'].'</a> ]';
>        }
>      }
>    ?>
>  <?
>  }

Try something more like this:

function menu($id){
  $query = "select pid, title from meta_data where id = $id";
  $meta = mysql_query($query) or error_log(mysql_error()); # Check HTTP
error_log for SQL errors!
  list($pid, $title) = mysql_fetch_row($meta, 0);
  if (isset($pid) && $pid)){
    # Switch the menu() and $title parts around if you want bread-crumbs in
the other direction
    $result = menu($pid) . $title;
    $result = '';
  return $result;


There *ARE* techniques for encoding the SQL in such a way that a single SQL
statement can get the entire "path" at once, but they get kinda complicated
and gnarly, and, really, as I said, if you have a shallow tree, it's just
not worth the hassle...  If you have a really *DEEP* tree, you'll need to do
some more research.

Like Music?  http://l-i-e.com/artists.htm

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to