[UPHPU] Hierarchical Menu Ideas

David Boucha boucha at gmail.com
Sat Dec 3 14:28:07 MST 2005


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

I believe $row[0]-- is the same as ($row[0] -1).  Both subtract 1 from $row[0].


> I think I'm referencing rows [0] and [1], and not columns [0] and [1].
>
> I'm still not sure how to reference the columns.

You're correct. You can't compare the previous row with $row[0]
because $row[0] getting overwritten with a new value from the database
during each iteration of your WHILE loop. In the past I've done
something like this in the while loop so I can compare the previous
row.
$prevRow = $row[0];

I haven't tested this, but your code might look something like this:

$prevRow = '0';
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] > $prevRow) {
                                echo "</li>\r\n<ul>\r\n <li>$row[1]";
                        } else($row[0] < $prevRow) {
                                echo "</li>\r\n</ul>\r\n<li>$row[1]";
                        }
                }
                $prevRow = $row[0]; //Save the value of $row[0] for
comparison in the
                                              // next iteration of the
while loop
        }
echo "</li>\r\n</ul>";
[/CODE]

Somebody else on this list would probably know a more elegant way to
handle this, but I've got a function on some production code at work
that does comparison like that and it's working fine.


Dave



More information about the UPHPU mailing list