Creating a "tab" menu with php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Downleader
Forum Newbie
Posts: 5
Joined: Fri Sep 24, 2004 3:11 pm

Creating a "tab" menu with php?

Post by Downleader »

Hi, I've got the following situation on my site.

-----------> ------------------------------------------
menu.php | |
| headerpicture |
-------------------------------------------
| Home | Forum | Downloads | Links|
------------> ----------------------------------------------

> content --------------------->

I'll hope you understand it. On each page I want to include (php include) the menu.php so it is not difficult to change it when I want to. The menu is made of tables. What I want are links like this: http://www.mydomain.com/?page=forum or /index.php?page=forum
I would like to know if it is possible If I click on the "forum" link, a php-script is loading the "forum"-content and the php script is changing the background colour of the table-cell with the forum link.
If this works, you've got a menu like a tab menu, without having to change the menu on all the pages. I'll hope somebody is understanding. Does somebody know a script for it?

Thanks in advance and sorry for my bad english. :oops:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you could create a function like

Code: Select all

function tabcheck($name)
{

if ($_GET['page'] == $name ? $bg = '#FFFFFF' : $bg = '#000000');

return $bg;

}
Hope this gives you an idea...
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Or if you switch to frames, JavaScript could give you some help.

Research: [google]javascript onClick table background change[/google]
Downleader
Forum Newbie
Posts: 5
Joined: Fri Sep 24, 2004 3:11 pm

Post by Downleader »

Okay, before everyone thinks that I know a lot of php, I don't. :oops:

I've founden something in an onther script like this:

<td class="<?php echo ($page == 'categories') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_categories.php">Categories</a></b></td>

The "puncon1cent" and "puncent" are defined in a stylesheet. When I click on a link, the menu automatic change to the correct stylesheet (and then the background changes).

Can somebody tell me how this works, cause it seems to be very simple, but I don't know how this script see on which page I am.

In this script the header.php is inserted with "require" what is the difference between it and "include"

And I don't care if links are like this: ../admin.php or page?admin. I'd prefer the first. I just want the most simple option :D

The whole script:

Code: Select all

<?php
//
// Displays link to admin pages (for admins)
//
function admin_menu($page = '')
{

?>
<table class="punspacer" cellspacing="1" cellpadding="4">
	<tr>
		<td class="<?php echo ($page == 'categories') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_categories.php">Categories</a></b></td>
		<td class="<?php echo ($page == 'forums') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_forums.php">Forums</a></b></td>
		<td class="<?php echo ($page == 'users') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_users.php">Users</a></b></td>
		<td class="<?php echo ($page == 'options') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_options.php">Options</a></b></td>
		<td class="<?php echo ($page == 'permissions') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_permissions.php">Permissions</a></b></td>
		<td class="<?php echo ($page == 'censoring') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_censoring.php">Censoring</a></b></td>
		<td class="<?php echo ($page == 'ranks') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_ranks.php">Ranks</a></b></td>
		<td class="<?php echo ($page == 'bans') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_bans.php">Bans</a></b></td>
		<td class="<?php echo ($page == 'prune') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_prune.php">Prune</a></b></td>
		<td class="<?php echo ($page == 'maintenance') ? 'puncon1cent' : 'puncent'; ?>" style="width: 10%; white-space: nowrap"><b><a href="admin_maintenance.php">Maintenance</a></b></td>
		<td class="<?php echo ($page == 'reports') ? 'puncon1cent' : 'puncent'; ?>" style="width: 9%; white-space: nowrap"><b><a href="admin_reports.php">Reports</a></b></td>
	</tr>
</table>

<?php

}
Thanks
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

i meant more or so something alongs the lines of this

Code: Select all

<style type="text/css">
	
		.puncon1cent {
			width: 9%; 
			white-space: nowrap;
			background-color: #FFFFFF;
			}
			
		.puncent {
			width: 9%; 
			white-space: nowrap;
			background-color: #000000;
			}	
	
	</style>

<?

	function admin_menu($name)
	{
		if ($_GET['page'] == $name ? $class= 'puncon1cent' : class = 'puncent';
	
		return $class;
	}


?>

	<table class="punspacer" cellspacing="1" cellpadding="4">
		<tr>
			<td class="<?= admin_menu('categories'); ?>"><b><a href="admin_categories.php">Categories</a></b></td>
			<td class="<?= admin_menu('forums'); ?>"><b><a href="admin_forums.php">Forums</a></b></td>
			<td class="<?= admin_menu('users'); ?>"><b><a href="admin_users.php">Users</a></b></td>
			<td class="<?= admin_menu('options'); ?>"><b><a href="admin_options.php">Options</a></b></td>
			<td class="<?= admin_menu('permissions'); ?>"><b><a href="admin_permissions.php">Permissions</a></b></td>
			<td class="<?= admin_menu('censoring'); ?>"><b><a href="admin_censoring.php">Censoring</a></b></td>
			<td class="<?= admin_menu('ranks'); ?>"><b><a href="admin_ranks.php">Ranks</a></b></td>
			<td class="<?= admin_menu('bans'); ?>"><b><a href="admin_bans.php">Bans</a></b></td>
			<td class="<?= admin_menu('prune'); ?>"><b><a href="admin_prune.php">Prune</a></b></td>
			<td class="<?= admin_menu('maintenance'); ?>"><b><a href="admin_maintenance.php">Maintenance</a></b></td>
			<td class="<?= admin_menu('reports'); ?>"><b><a href="admin_reports.php">Reports</a></b></td>
		</tr>
	</table>
Downleader
Forum Newbie
Posts: 5
Joined: Fri Sep 24, 2004 3:11 pm

Post by Downleader »

But in this script, how do I define the page name in the content page. And how are the links working: page?=test or /test.php

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's specified through $_GET['page'] directly.
Downleader
Forum Newbie
Posts: 5
Joined: Fri Sep 24, 2004 3:11 pm

Post by Downleader »

I tried it but php says:

Code: Select all

Parse error: parse error, unexpected T_CLASS in /var/www/html/users/89ggg/test3/menu.php on line 5
This is line 5

Code: Select all

<?php
        if ($_GET['page'] == $name ? $class= 'puncon1cent' : class = 'puncent';

?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

http://www.alistapart.com/articles/keepingcurrent/

Although i wouldn't test it like they do.

Code: Select all

$pages = array('page1.php' => 'Nice Name',  'page2.php' => 'Nicer name');

foreach($pages as $page => $label)
{

  echo "<li id='";

  if ($_GET['page'] == $page)
  {
     echo "notcurrent";
  }
  else
  {
    echo "currentpage";
  }

    echo "'><a href='$page'>$label</a></li>";
}
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

here's how my menu works, maybe you'll find something of it useful ;)

Code: Select all

$menu_count = 0;

$query['menu'] = mysql_query ("SELECT `id`,`pos`,`title`,`curl` FROM `content` WHERE `visible` = 1 ORDER BY `pos`");
##### loop trough each page, adding it to the menu
while ($cmenu = mysql_fetch_assoc ($query['menu'])) {

	if (!empty ($cmenu['curl'])) { $smenu['link'] = $cmenu['curl']; } // if link is set to link to a external file
	else { $smenu['link'] = "/p/{$cmenu['id']}/"; } // normal internal page link

	$output['menu'] .= "<li";
	$output['menu'] .= "><a href='{$smenu['link']}' title='{$cmenu['title']}'";
	if ($output['id'] == $cmenu['id'] || $output['pid'] == $cmenu['id']) $output['menu'] .= " id='active'";
	$output['menu'] .= ">{$cmenu['title']}</a></li>";
	$menu_count++;

} ## /loop trough each page, adding it to the menu
Downleader
Forum Newbie
Posts: 5
Joined: Fri Sep 24, 2004 3:11 pm

Post by Downleader »

Thanks, timvw, I visited your link with a very good and usefull tutorial. The menu is working now. :D :D :D :D Everyone who helped me...thanks!
Post Reply