[PHP] Re: Problem with menu

2002-07-03 Thread JJ Harrison

Thx.

I a bit of a n00b so don't always understand.

I is a lot clearer now thank you


--
JJ Harrison
[EMAIL PROTECTED]
www.tececo.com
"Richard Lynch" <[EMAIL PROTECTED]> wrote in message
news:20020703212954.BFUU903.sccrmhc03.attbi.com@[192.168.1.103]...
> >when I echo menu($id) I get the current page's title.
> >
> >How do I print it's peers and it's single parent?
>
> You will need a second query to ask for all the children of the parent:
>
> $peer_query = "select id as child_id, title from meta_data where pid =
> $pid";
> $peers = mysql_query($query) or error_log(mysql_error());
> while (list($child, $child_title) = mysql_fetch_row($peers)){
>   echo "$child_title ($child)\n";
> }
>
> >P.P.P.S. All pages have information provided by this script:
> >
> >$fn = explode("/", $_SERVER['PHP_SELF']);
> >  $num_of_s = count($fn) - 1;
> >  $fn = "$fn[$num_of_s]";
> >  $query = "SELECT * FROM meta_data WHERE page_name = '$fn'";
> >  $result = mysql_query($query);
> >  $num_results = mysql_num_rows($result);
> >  $row = mysql_fetch_array($result);
> >  $id = $row['id'];
> >  $pid = $row['pid'];
> >  $title = $row['title'];
> >  $description = $row['description'];
> >  $keywords = $row['keywords'];
> >
> >It is my metadata page and is used all over the place. so why re-query
the
> >db for it's parent?
>
> I'm saying:
> Don't do that.  Don't use * in this first query.  In *ANY* query, specify
> *exactly* which columns you need.  You'll be a lot less confused by your
> data when you start forcing yourself to be more precise in your code about
> what you want.
>
> *CHANGE* the line above to:
> $query = "select id, pid, title, description, keywords from meta_data
where
> page_name = '$fn'";
>
> Then, after you have the $pid, add in the stuff I wrote above.
>
>
>
> --
> 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




[PHP] Re: Problem with menu

2002-07-03 Thread Richard Lynch

>when I echo menu($id) I get the current page's title.
>
>How do I print it's peers and it's single parent?

You will need a second query to ask for all the children of the parent:

$peer_query = "select id as child_id, title from meta_data where pid =
$pid";
$peers = mysql_query($query) or error_log(mysql_error());
while (list($child, $child_title) = mysql_fetch_row($peers)){
  echo "$child_title ($child)\n";
}

>P.P.P.S. All pages have information provided by this script:
>
>$fn = explode("/", $_SERVER['PHP_SELF']);
>  $num_of_s = count($fn) - 1;
>  $fn = "$fn[$num_of_s]";
>  $query = "SELECT * FROM meta_data WHERE page_name = '$fn'";
>  $result = mysql_query($query);
>  $num_results = mysql_num_rows($result);
>  $row = mysql_fetch_array($result);
>  $id = $row['id'];
>  $pid = $row['pid'];
>  $title = $row['title'];
>  $description = $row['description'];
>  $keywords = $row['keywords'];
>
>It is my metadata page and is used all over the place. so why re-query the
>db for it's parent?

I'm saying:
Don't do that.  Don't use * in this first query.  In *ANY* query, specify
*exactly* which columns you need.  You'll be a lot less confused by your
data when you start forcing yourself to be more precise in your code about
what you want.

*CHANGE* the line above to:
$query = "select id, pid, title, description, keywords from meta_data where
page_name = '$fn'";

Then, after you have the $pid, add in the stuff I wrote above.



-- 
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




[PHP] Re: Problem with menu

2002-07-02 Thread JJ Harrison

Thanks for that info.

I am still a bit new to programming/PHP to fully grasp that but I am sure I
will in time.

*anyway*

when I echo menu($id) I get the current page's title.

How do I print it's peers and it's single parent?


--
JJ Harrison
[EMAIL PROTECTED]
www.tececo.com

P.S. I originally wanted to build a hierachy based menu like this but failed

Page 3
Page 4
 P4 C1
 P4 C2(I am here)
 etc...

I went fine until I have to print the parent and it's peers like shown in
the example

P.P.S if you had a couple of functions which would help me do this I would
be grateful, but don't write anything.

P.P.P.S. All pages have information provided by this script:

$fn = explode("/", $_SERVER['PHP_SELF']);
  $num_of_s = count($fn) - 1;
  $fn = "$fn[$num_of_s]";
  $query = "SELECT * FROM meta_data WHERE page_name = '$fn'";
  $result = mysql_query($query);
  $num_results = mysql_num_rows($result);
  $row = mysql_fetch_array($result);
  $id = $row['id'];
  $pid = $row['pid'];
  $title = $row['title'];
  $description = $row['description'];
  $keywords = $row['keywords'];

It is my metadata page and is used all over the place. so why re-query the
db for it's parent?

"Richard Lynch" <[EMAIL PROTECTED]> wrote in message
news:20020701232031.VNMV6023.sccrmhc02.attbi.com@[192.168.1.103]...
> >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
> >pid(parent)
>
> 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 g'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){
> >  ?>
> >
> >
> >   >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 '[  >href="'.$row['page_name'].'">'.$row['title'].' ]';
> >}
> >  }
> >?>
> >
> >
> >   >  }
>
> 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;
>   }
>   else{
> $result = '';
>   }
>   return $result;
> }
>
> NOTE:
>
> 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




Re: [PHP] Re: Problem with menu

2002-07-01 Thread Analysis & Solutions

On Mon, Jul 01, 2002 at 08:00:39PM -0400, Analysis & Solutions wrote:
> Hi Richard:

Oops.  Meant to reply directly to Ricahrd.  Sorry 'bout that.

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

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




Re: [PHP] Re: Problem with menu

2002-07-01 Thread Analysis & Solutions

Hi Richard:

Nice to have you back on the list.  I've noticed your replies are not 
winding up back in the threads due to the Reference header not being set 
by Outlook.

While this isn't going to cause the end of the world, it'd be nice if they
were there.

Enjoy,

--Dan

-- 
   PHP classes that make web design easier
SQL Solution  |   Layout Solution   |  Form Solution
sqlsolution.info  | layoutsolution.info |  formsolution.info
 T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409

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




[PHP] Re: Problem with menu

2002-07-01 Thread Richard Lynch

>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
>pid(parent)

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 g'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){
>  ?>
>
>
>  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 '[ href="'.$row['page_name'].'">'.$row['title'].' ]';
>}
>  }
>?>
>
>
>}

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;
  }
  else{
$result = '';
  }
  return $result;
}

NOTE:

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