Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Expanding & Collapsing Menus


Right, so you are making a new website, and you wanna spiff up your design with collapsing and expanding menus? I’ve created an easy to use way to put in as many menus as you like, and it uses sessions to store which menus to show and which to hide. I’ll outline the basics below:

<?php session_start();

if($_GET['set'] > 0)
{

if($_SESSION['whattoclose'] == NULL)
{

$_SESSION['whattoclose'] = $_GET['set'];

} else {

$_SESSION['whattoclose'] = $_SESSION['whattoclose'] . '|' . $_GET['set'];

}

}

if($_GET['undo'] > 0)
{

if(!empty($_SESSION['whattoclose'])){

$_SESSION['whattoclose'] = str_replace("|".$_GET['undo'] , "" , $_SESSION['whattoclose']);
$_SESSION['whattoclose'] = str_replace($_GET['undo'] , "" , $_SESSION['whattoclose']);

}

}

?>

These two IF statements check whether you are trying to show a menu, or hide a menu. If you opted to hide a menu, it adds the menu ID to the session variable containing the list of menus not to show. If you opted to show a menu, it removes the menu ID from the variable so it will be shown again. The reason there are two STR_REPLACE functions is because the format of the string might be thus:

1|2|3|4|5

So, if it was just the one statement (i.e. replace “|2” with nothing, thus removing) and the user tried to put 1 through it, it wouldn’t remove 1 because 1 doesn’t have the | with it, which is why we have two.

The following functions should be placed at the top of the PHP file, so that when the rest of it processes, it will include the new results instead of having to refresh, which would be what you would have to do if they were at the end of the script.

Function 1 - Menu Titles & Open/Close Options

function showTitle($id)
{

$title = array(
"First title",
"Second title",
"Third title",
"Etc..."
);

$data = $_SESSION['whattoclose'];

$not = explode( "|" , $data );
if(!in_array( $id , $not ))
{

echo '<a href="'.$_SERVER['PHP_SELF'].'?set='.$id.'">close</a> - ';
echo $title[$id-1];

} else {

echo '<a href="'.$_SERVER['PHP_SELF'].'?undo='.$id.'">open</a> - ';
echo $title[$id-1];

}

}

This is the first of the two functions that perform the basic operations of our menu system. This function in particular shows the title, and the appropriate link to open/ close the menu. As you can see, its fairly simple, and starts with an array specifying menu names, in order of first menu to last menu. If you want to add new menus, the first step is to put a comma after the last array stream, then put your new title in quotes below, without a comma after it.  After processing the $title array, the script splits the string into pieces depending on which menus are NOT to be displayed, and checks whether your selected menu is allowed to be shown.  If it is, the 'close' link is shown, and the title. Otherwise it shows the open link and the title.

I’ve used $_SERVER[‘PHP_SELF’] so that you do not have to change the link URL for each website - it’s now automatic!  Bear in mind, though, that this probably won’t work if it’s in a folder, so if you are intending on putting it in a folder, change this to your URL/filename.

Function 2 - Showing the Menu Content

function checkIt($id)
{

$data = array(
'<p><a href="#">Link1</a></p>
<p><a href="#">Link2</a></p>
<p><a href="#">Link3</a></p>',
'<p>The second tables data here. Blah blah blah hehehe.</p>',
'<p>Getting tired yet? Hahaha</p><p>Weeeeeeeeeeeeeeeeeeeeeeeeee</p><p><a href="#">scrowler</a> is so cool!</p>',
'<p>Foobar... This website is awesome eh? Divs and all... Screw tables!</p>'
);

$str = $_SESSION['whattoclose'];

$not = explode( "|" , $str );
if(!in_array( $id , $not ))
{

echo '<div class="table">';
echo $data[$id-1];
echo '</div>';

}

}

Our second function is rather essential, and shows the content in each box. So, if you want to add new menus, do the same as you did with the $title array in the first function, but this time make sure that you wrap it inside <p></p> tags and throw all your content in there. If you want to put in ' characters, use alternative tags, as the ' character here has relevance in the code and adding it will screw up your PHP. I used apostrophes instead of double quotes because this way you can put hyperlinks in with no trouble.

Following these content definitions, the code then checks that the menu can be shown. If it can, it shows the whole DIV and the information as well - So, if the menu isn’t shown, the DIV doesn’t get shown either, and you don’t get the lower frame at all. Cool huh? Well that’s all of the main PHP out of the way, the rest is HTML and function calling.

And Finally: Showing Your Menus!

<div class="header">
<? showTitle(1) ?>
</div>
<? checkIt(1) ?>
<br>
<div class="header">
<? showTitle(2) ?>
</div>
<? checkIt(2) ?>

I’ve cut my code down to only show 2 menus. All formatting is defined in my example CSS file menus.css which is part of the source code I have included with this tutorial. You can completely customize the way your menus will look through this source code. And, of course, I wouldn’t want to give you CSS code with errors, so I validated it to be sure.

That’s it from me! If you need more assistance, contact me via the Creative Forums, link is located on your right. Or click my name below to go to my Creative Profile, where you can email me, or send a private message. Registration is required.

Click here to download a .zip file containing some example source code for this tutorial.

- Tutorial written by Scrowler

Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments

User:  smw (#57346)
Date: Wed Aug 12, 2009. 10:19:18

Post #1 of 1

How would I rearrange the code so the open and close links are behind the titles?

example:
title - open

Reply to this post


--- View Entire Thread ---
Amazing Font Pack!

Featured Tutorialsmore

Sunbursts
Sunbursts
- Adobe Illustrator -
Xpresso Tutorial -...
Xpresso Tutorial -...
- Maxon Cinema 4D -
Razor Stryke Logo
Razor Stryke Logo
- Adobe Photoshop -
Pixel Stretch Swirl
Pixel Stretch Swirl
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Download Source File
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673