[UPHPU] Hierarchical Menu Ideas

Ryan Oldham uphpu at codershop.com
Sat Dec 3 07:46:41 MST 2005


The trouble you may be having is that $row[0]-- is working, but not as
you think it works.  When you use $row[0]-- it changes the value of
$row[0].  So when you check it the next time, it is actually one less
than before.  For example, if you run the following code:

<?php
$row[0] = 5;
$row[0]--;
echo $row[0];
?>


The resulting output is 4.  I would suggest using ($row[0] - 1) instead
of $row[0]-- when comparing values.

Hope this helps.

-Ryan

On Fri, 2005-12-02 at 23:46 -0700, Brandon Stout wrote:
> Mac Newbold wrote:
> > . . . all you really need to do is print a <ul> (and optionally adjust 
> > your indent for the <li> tags) every time the depth moves up, and 
> > print a </ul> every time it moves down . . .
> >
> > . . . As you're looping through the results, keep a variable that 
> > holds the depth of the last thing you printed. If the current one is 
> > higher than the last one, you're going up, print <ul>. If the current 
> > one is lower, you're going down, print </ul>.
> >
> > If you still want more level 2 help, ask again, and someone will write 
> > it up.
> >
> > Mac
> >
> > -- 
> > Mac Newbold        MNE - Mac Newbold Enterprises, LLC
> > mac at macnewbold.com    http://www.macnewbold.com/
> No need to write up some sample code for me yet.  To make this work, I 
> need to test a variable assigned in one column of one row to the 
> previous row.  I can test entire row contents, but I'm not sure how to 
> test one cell in a row.  I tried this:
> 
> [CODE]
> <?php
> $mainMenuQuery="(
>  SELECT
>   COUNT(`parent`.`CategoryName`) - 1 AS `depth`,
>   `node`.`CategoryName` AS `Category`
>  FROM `web`.`categories` `node`, `web`.`categories` `parent`
>  WHERE
>   `node`.`LeftValue` BETWEEN `parent`.`LeftValue` AND `parent`.`RightValue`
>   AND `parent`.`LeftValue` > 1
>  GROUP BY `node`.`CategoryID`
>  ORDER BY `node`.`LeftValue`
> )";
> $mainMenu = mysql_query($mainMenuQuery);
> echo "<ul id=\"containerul\">\r\n";
> while($row = mysql_fetch_row($mainMenu)) {
>  for($i=0; $i < mysql_num_fields($mainMenu); $i++) {
>   if($row[0] = 0) {
>    echo "<li>$row[1]";
>   } elseif($row[0] > $row[0]--) {
>    echo "</li>\r\n<ul>\r\n <li>$row[1]";
>   } else($row[0] > $row[0]--) {
>    echo "</li>\r\n</ul>\r\n<li>$row[1]";
>   }
>  }
> }
> echo "</li>\r\n</ul>";
> ?>
> [/CODE]
> 
> This doesn't work, and I'm betting it's because I cant do this:
> 
> $row[0]--
> 
> to reference the previous row's first cell - though it might subtract 
> one from the current cell's value.
> 
> My question:
> 
> How do I test one row against the previous row's value in the same column?
> 
> Brandon Stout
> http://mscis.org
> 
> _______________________________________________
> 
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
> 



More information about the UPHPU mailing list