By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

Collapsing Menu - please help

P: n/a
Hi,

I am relatively new to php and am currently working on a site where I need
to use a collapsing menu system. I have written the code to do this
however I am completely at a loss to do one particular thing. To best
explain what the problem is I will give you an overview of how the menu
hangs together.

The menu looks like this:

Catagory1 (note these look like buttons)
Catagory2
Catagory3

When a user clicks on a catagory that catagory expands to show the sub
catagories and the menu then looks like this ( assuming they clicked on
catagory1)

Catagory1
subitem1
subitem2
Catagory2
Catagory3

If you were to click on catagory2 then the sub items for that catagory would
appear and the sub items for catagory 1 would disappear.

The menu script I created draws the catagories to look like buttons and
gives them a url in the form of 'index.php?s=#' so for example catagory1
has the url of 'index.php?s=1'

The script draws the first 'button' (catagory1)and proceeds to check to see
if that catagory is active and if so it then populates the subitems and
then loops and draws the next button (catagory2). What I would like to do
is some how when a user click on catagory1 to have the script recognise
this and rather than give the button the url of 'index.php?s=1' when the
page is drawn to simply change it to a text lable, if the user was then to
click on button 2 (catagory2) it then needs to change button 2 url to just
a text lable and button 1 url back to 'index.php?s=1'.

I hope this makes sense, i have included a cut down version of my menu
script which hopefully will make things clearer.

Any help on this will be appreciated.
---------------------------------------------------------------------------------------------
<?php
session_start();
if ((isset($_SESSION['group'])) && (!isset($_GET['s']))) {
$s = $_SESSION['group'];
}
else {
$s = $_GET[s];
}

//Get current page
$page = $_SERVER['REQUEST_URI'];
// array holds menu button names and passes menu group value
$headers = array('Cars' => 'index.php?s=1','Bikes' => 'index.php?s=2');

$sub_1 = array(
'Sports' => 'sports.php',
'Family' => 'family.php',
'4x4' => 'fourby.php'
);

$sub_2 = array(
'Motocross' => 'moto.php',
'Road' => 'road.php',
'Scooters' => 'scooter.php',
);

// Draw the menu
foreach ( $headers as $key => $value ) {

// Line below draws a button this is the part i want to be able to change
the value of if that submenu is currently active
echo "<div id=\"button\"><a href='$value'>$key</a></div>";

$_SESSION['group'] = $s;
if (($key == "Cars") && ($s == "1")) {
foreach ( $sub_1 as $key => $value ) {
$_SESSION['group'] = $s;
// Check to see if page is active is so bold it
if ($value == $page) {
echo "<b><a href='$value'>$key</a></b>";
}
else {
echo "<a href='$value'>$key</a>";
}
}

}

if (($key == "Bikes") && ($s == "2")) {
$_SESSION['group'] = $s;
foreach ( $sub_2 as $key => $value ) {
$_SESSION['group'] = $s;
// Check to see if page is active is so bold it
if ($value == $page) {
echo "<b><a href='$value'>$key</a></b>";
}
else {
echo "<a href='$value'>$key</a>";
}
}
}
}
?>
--------------------------------------------------------------------------------------------

Please note I call this script as an include from my template

Many thanks
Sep 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
rigga wrote:
Hi,

I am relatively new to php and am currently working on a site where I need
to use a collapsing menu system. I have written the code to do this
however I am completely at a loss to do one particular thing. To best
explain what the problem is I will give you an overview of how the menu
hangs together.

The menu looks like this:

Catagory1 (note these look like buttons)
Catagory2
Catagory3

When a user clicks on a catagory that catagory expands to show the sub
catagories and the menu then looks like this ( assuming they clicked on
catagory1)

Catagory1
subitem1
subitem2
Catagory2
Catagory3

If you were to click on catagory2 then the sub items for that catagory
would appear and the sub items for catagory 1 would disappear.

The menu script I created draws the catagories to look like buttons and
gives them a url in the form of 'index.php?s=#' so for example catagory1
has the url of 'index.php?s=1'

The script draws the first 'button' (catagory1)and proceeds to check to
see if that catagory is active and if so it then populates the subitems
and then loops and draws the next button (catagory2). What I would like to
do is some how when a user click on catagory1 to have the script recognise
this and rather than give the button the url of 'index.php?s=1' when the
page is drawn to simply change it to a text lable, if the user was then to
click on button 2 (catagory2) it then needs to change button 2 url to just
a text lable and button 1 url back to 'index.php?s=1'.

I hope this makes sense, i have included a cut down version of my menu
script which hopefully will make things clearer.

Any help on this will be appreciated.
--------------------------------------------------------------------------------------------- <?php
session_start();
if ((isset($_SESSION['group'])) && (!isset($_GET['s']))) {
$s = $_SESSION['group'];
}
else {
$s = $_GET[s];
}

//Get current page
$page = $_SERVER['REQUEST_URI'];
// array holds menu button names and passes menu group value
$headers = array('Cars' => 'index.php?s=1','Bikes' => 'index.php?s=2');

$sub_1 = array(
'Sports' => 'sports.php',
'Family' => 'family.php',
'4x4' => 'fourby.php'
);

$sub_2 = array(
'Motocross' => 'moto.php',
'Road' => 'road.php',
'Scooters' => 'scooter.php',
);

// Draw the menu
foreach ( $headers as $key => $value ) {

// Line below draws a button this is the part i want to be able to change
the value of if that submenu is currently active
echo "<div id=\"button\"><a href='$value'>$key</a></div>";

$_SESSION['group'] = $s;
if (($key == "Cars") && ($s == "1")) {
foreach ( $sub_1 as $key => $value ) {
$_SESSION['group'] = $s;
// Check to see if page is active is so bold it
if ($value == $page) {
echo "<b><a href='$value'>$key</a></b>";
}
else {
echo "<a href='$value'>$key</a>";
}
}

}

if (($key == "Bikes") && ($s == "2")) {
$_SESSION['group'] = $s;
foreach ( $sub_2 as $key => $value ) {
$_SESSION['group'] = $s;
// Check to see if page is active is so bold it
if ($value == $page) {
echo "<b><a href='$value'>$key</a></b>";
}
else {
echo "<a href='$value'>$key</a>";
}
}
}
}
?>
--------------------------------------------------------------------------------------------
Please note I call this script as an include from my template

Many thanks

Nobody can help? or have I not explained this well enough?
Sep 17 '05 #2

P: n/a
rigga (ri***@hasnomail.com) wrote:

: Nobody can help? or have I not explained this well enough?

You explained it fine, but the problem involves a lot of code and is not
an interesting tricky problem, just a long one to explain, with a straight
forward answer if you just work through it yourself.

Basically, you need to figure out which "echo" command is creating the
link that needs changing. Then you need to put an if/the/else around that
echo so it either outputs a link or a label, depending on what you want
for that menu thing. Then you have to figure out what variable or set of
variables contains the information that lets you decide whether it's a
link or a label.
# you receive input,
# and then your menu loop sets a loop variable
loop on menu things
{
if ($this_loop_entry is the "same" as the $expanded_menu_item)
{
echo "the html to make a label"
}
else
{
echo "the html to make a link"
}

} # end of loop

the exact details just require some elbow grease.

--

This programmer available for rent.
Sep 17 '05 #3

P: n/a

rigga (ri***@hasnomail.com) wrote:

: Nobody can help? or have I not explained this well enough?


Your problem is that the PHP is not generating HTML that behaves as you
want. So, hand-code a static HTML file that *does* behave correctly,
and then compare what the PHP is generating with what it *should* be
generating.

Sep 18 '05 #4

P: n/a
Cats wrote:

rigga (ri***@hasnomail.com) wrote:

: Nobody can help? or have I not explained this well enough?


Your problem is that the PHP is not generating HTML that behaves as you
want. So, hand-code a static HTML file that *does* behave correctly,
and then compare what the PHP is generating with what it *should* be
generating.

Thanks for the heads up guys, I will break the code down and see where I can
fix my issue.
Sep 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.