• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

Logic problem: Double expanding menu

Hi,
I have made a little php "menu" system at
http://quickxxx.info/test/nav.php

as you can see you can "open" the menu and clicking the node again will close the menu, if node one is open and you click on node 2, node 1 automatically closes...I would like it to be so that you can have 2 or more nodes open at the same time....

here is the code i am using so far (please visit the above url first so u will know what i am talking about)

########## Start Code ##################

<table border=0 cellpadding=0 cellspacing=0>
<?php

$main_bg="yellow";

$p=$_GET['p']; // present number
$x=$_GET['x']; // to expand

if(!isset($x)){$x=0;}
if($x==$p){$x=0;}


switch ($x)
{
case 1:
$y="Fruits";
break;

case 2:
$y="Numbers";
break;

case 3:
$y="Holes";
break;
}

$fruits = array (
  "Fruits" => array("a" => "http://www.jumac.com", "b" => "banana", "c" => "apple"),
  "Numbers" => array(0,1, 2, 3, 4, 5),
  "Holes" => array(1=>"first",2=>"second",3=>"third"),
  "Something.com" => "http://Something.com"
);

/* Show main menu/array*/
$i=1;
reset($fruits);
while (list($key, $val) = each($fruits)) {
if(is_array($val) && $y != $key){$key2="[+]</a> ". $key;}
else if(is_array($val) && $y == $key){$key2="[ - ]</a> ". $key;}
else if(!is_array($val)){$key2="[*] ". $key; $not_array=1;}

if($not_array != 1){ echo "<tr><td bgcolor=$main_bg><b><a href=nav.php?x=$i&p=$x>$key2 <b/></td></tr>\n";}
else{ echo "<tr><td bgcolor=$main_bg><b><a href=$val>$key2 </a></b></td></tr>\n";}


/* Show sub menu/sub array */
if($y == $key)
      {

            reset($fruits[$y]);
            while (list($key, $val) = each($fruits[$y]))
            {
            if(is_array($val)){$key="[+] ". $key;}

            echo "<tr><td align=right> [ <a href=$val>$key</a> ] </td></tr>";

            }

      } // End of if($y == $key)  
$i++;
} // End of while
?></table>
####################### End Code ############

Any ideas? If you know of any other script that does this.. that would be great too...i just need the functionality.
Also, I KNOW this can be done better with javascript but in this way it is 100% browser compatable even for old browsers...thats why i am not using JS.

Thanks,
Mag
0
mag1c1an
Asked:
mag1c1an
  • 2
  • 2
1 Solution
 
keteracelCommented:
<table border=0 cellpadding=0 cellspacing=0>
<?php

$main_bg="yellow";

$openThese = array();

if (isset($_GET['p']) && $_GET['p'] != "") {
      $p=$_GET['p']; // present number

      $openThese = explode("-", $p);
}

$fruits = array (
  "Fruits" => array("a" => "http://www.jumac.com", "b" => "banana", "c" => "apple"),
  "Numbers" => array(0,1, 2, 3, 4, 5),
  "Holes" => array(1=>"first",2=>"second",3=>"third"),
  "Something.com" => "http://Something.com"
);


function generateP($d, $add, $arr) {
      $str = "";
      
      foreach ($arr as $item) {
            if ($item != $d) {
                  $str .= $item . "-";
            }
      }
      if ($add == true) {
            $str .= $d . "-";
      }
      
      return "-".trim($str, "-");
}


/* Show main menu/array*/
$not_array = 0;
$i=1;

while (list($key, $val) = each($fruits)) {
if(is_array($val) && !array_search($i, $openThese)){$key2="[+]</a> ". $key;}
else if(is_array($val) && array_search($i, $openThese)){$key2="[ - ]</a> ". $key;}
else if(!is_array($val)){$key2="[*] ". $key; $not_array=1;}

if($not_array != 1){
      $open = generateP($i, true, $openThese);
      $close = generateP($i, false, $openThese);
      
      if (array_search($i, $openThese)) {
            $action = $close;
      }
      else {
            $action = $open;
      }
      
      echo "<tr><td bgcolor=$main_bg><b><a href=eetest.php?p=$action>$key2 <b/></td></tr>\n";
}
else{ echo "<tr><td bgcolor=$main_bg><b><a href=$val>$key2 </a></b></td></tr>\n";}


/* Show sub menu/sub array */
if(array_search($i, $openThese))
     {

          reset($fruits[$key]);
          while (list($key2, $val) = each($fruits[$key]))
          {
          if(is_array($val)){$key2="[+] ". $key2;}

          echo "<tr><td align=right> [ <a href=$val>$key2</a> ] </td></tr>";

          }

     } // End of if($y == $key)  
$i++;
} // End of while
?></table>
0
 
keteracelCommented:
>I KNOW this can be done better with javascript but in this way
> it is 100% browser compatable even for old browsers...thats
> why i am not using JS.

It's a bummer but it's true!
0
 
mag1c1anAuthor Commented:
Hey,
Thanks for replying.

Yeah, JS + Layers = some very very good menu systems but != total_compatabilty

:-)

I'll try your modifications and post back shortly if I have any problems or something does not work, if everything cool will award you the points with an "A" for the quick reply.

Cheers,
Mag
0
 
mag1c1anAuthor Commented:
Works exactly like i wanted it.
Thanks!
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now