[UPHPU] Hierarchical Menu Ideas - RESOLVED!

Brandon Stout hplsbyufan at imapmail.org
Sun Dec 11 23:34:58 MST 2005


I found the problem.  This line of code:

 if ($n = 1) {

Should have had == since I'm testing for equality, not assigning a 
value.  The line should be:

 if ($n == 1) {

The code below has the error fixed.

Thank you all again for the help.

Brandon Stout
http://mscis.org

> [CODE]
> $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);
> $lastdepth=0;                                       // initialize the 
> previous row's depth tracker
> $n=0;                                                   // initialize 
> the row counter
>
> echo "<ul id=\"containerul\">";               // open the <ul>
> while($row = mysql_fetch_row($mainMenu)) {
> $depth = $row[0];
> $n++;                                                   // increment 
> the row counter, starting with 1
> if ($depth == $lastdepth) {
>  if ($n == 1) {                                       // for the first 
> list item, simply start the first list item.
>   echo "\r\n";
>   echo "<li>$row[1]";                            // don't close the 
> <li> yet, it may contain a <ul>
>  } else {                                               // for all 
> other sequential same-depth items
>   echo "</li>";                                       // close the 
> previous <li>
>   echo "\r\n";
>   echo str_repeat(" ", "$depth"*2);        // indent the <li> by it's 
> depth
>   echo "<li>$row[1]";                           // don't close the 
> <li> yet, it may contain a <ul>
>  }
> }
> elseif ($depth > $lastdepth) {               // don't close a <li> 
> when the depth increases
>  echo "\r\n";
>  echo str_repeat(" ","$depth");              // indent <ul> by depth
>  echo "<ul>";
>  echo "\r\n";
>  echo str_repeat(" ","$depth"*2);          // indent <li> by depth
>  echo "<li>$row[1]";                            // don't close the 
> <li> yet, it may contain a <ul>
> } else {
>  for ($i = ($lastdepth - $depth); $i > 0; $i--) {
>   $d = $lastdepth;                                 // depth can 
> decrease by more than 1
>   echo "</li>";                                       // this loop 
> should close all <ul>'s and <li>'s
>   echo "\r\n";                                         // until $depth 
> = $lastdepth
>   echo str_repeat(" ","$lastdepth");
>   echo "</ul>";
>   echo "\r\n";
>   $d--;
>  }
>   echo str_repeat(" ","$lastdepth");
>   echo "</li>";                                       // close the 
> <li> that contained sub <ul>'s
>   echo "\r\n";
>   echo str_repeat(" ","$depth"*2);
>   echo "<li>$row[1]";                           // write the next <li>
> }
> $lastdepth = $depth;
> }
> for ($i = $depth; $i > 0; $i--) {             // last row can have a 
> depth > 1
> $d = $lastdepth;                                  // this should close 
> all <ul>'s and <li>'s
> echo "</li>";                                        // until depth = 0
> echo "\r\n";
> echo str_repeat(" ","$lastdepth");
> echo "</ul>";
> echo "\r\n";
> ($d--);
> }
> echo "</li>\r\n";                                    // close the last 
> depth = 0 <li>
> echo "</ul>\r\n";                                   // close the 
> opening <ul>
> [/CODE]
>
> The result is nearly perfect, but it appears that there is a bug 
> somewhere.  2 list items seem to run through the loop intended only 
> for the first row (the $n = 1 loop).  Here is the resulting code, with 
> comments on the problem results:
>
> [CODE]
> <ul id="containerul">
> <li>TechWriting
> <ul>
>  <li>Hardware
>  <ul>
>    <li>Desktop</li>
>  </ul>
>  </li>
>  <li>Network
> <li>Security
> <li>Software</li>
> </ul>
> </li>
> <li>ProjectManagement
> <ul>
>  <li>phpMyResume</li>
> </ul>
> </li>
> <li>WebMaster
> <ul>
>  <li>FamilyPages</li>
> </ul>
> </li>
> </ul>
> [/CODE]
>
> Any ideas where the problem is?
> Now for testing Fred's Larson's latest suggestion... I'm learning more 
> than I intended with this one... And having fun!
>
>
> 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