• 0

[PHP] Help with setting up an array with this code


Question

Hey guys, I was trying to see if I could emulate tabs using PHP instead of Javascript, so far I got to work. But I was seeing if there was a way to create an array or two out of my code, so it becomes somewhat automated when adding more tabs.

			<div id="menu_tabs">
				<style>
					.active { background-color: #FFF; border: 1px solid #808080; border-bottom: 1px solid #FFF; }
					.inactive { background-color: #EEE; border: 1px solid #808080; }
				</style>
				<? ($_SERVER['REQUEST_URI'] == '/dashboard.php' ? $dashboard = 'active' : $dashboard = 'inactive' ); ?>
				<? ($_SERVER['REQUEST_URI'] == '/mail.php' ? $mail = 'active' : $mail = 'inactive' ); ?>
				<? ($_SERVER['REQUEST_URI'] == '/files.php' ? $files = 'active' : $files = 'inactive' ); ?>
				<? ($_SERVER['REQUEST_URI'] == '/contacts.php' ? $contacts = 'active' : $contacts = 'inactive' ); ?>
				<? ($_SERVER['REQUEST_URI'] == '/agenda.php' ? $agenda = 'active' : $agenda = 'inactive' ); ?>
				<ul>
					<li class="<?=$dashboard;?>"><a href="dashboard.php">Dashboard</a></li>
					<li class="<?=$mail;?>"><a href="mail.php">Mailbox</a></li>
					<li class="<?=$files;?>"><a href="files.php">My Files</a></li>
					<li class="<?=$contacts;?>"><a href="contacts.php">Contacts</a></li>
					<li class="<?=$agenda;?>"><a href="agenga.php">Agenda</a></li>
				</ul>
			</div><!-- end tab_menu -->

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Try this...

<div id="menu_tabs">
	<style>
		.active { background-color: #FFF; border: 1px solid #808080; border-bottom: 1px solid #FFF; }
		.inactive { background-color: #EEE; border: 1px solid #808080; }
	</style>
<?php

$links['dashboard'] = '/dashboard.php';
$links['mail'] = '/mail.php';
$links['files'] = '/files.php';
$links['contacts'] = '/contacts.php';
$links['agenda'] = '/agenda.php';

foreach ( $links as $key => $value ):
	$class = 'inactive';
	if ( $_SERVER['REQUEST_URI'] == $value ):
		$class = 'active';
	endif;
	$nav .= "\t\t" . '<li class="' . $class . '"><a href="' . $value . '">' . ucfirst( $key ) . '</a></li>' . PHP_EOL;
endforeach;

?>
	<ul>
<?php
echo $nav;
?>
	</ul>
</div>

Link to comment
Share on other sites

  • 0

Try this...

<div id="menu_tabs">
	<style>
		.active { background-color: #FFF; border: 1px solid #808080; border-bottom: 1px solid #FFF; }
		.inactive { background-color: #EEE; border: 1px solid #808080; }
	</style>
<?php

$links['dashboard'] = '/dashboard.php';
$links['mail'] = '/mail.php';
$links['files'] = '/files.php';
$links['contacts'] = '/contacts.php';
$links['agenda'] = '/agenda.php';

foreach ( $links as $key => $value ):
	$class = 'inactive';
	if ( $_SERVER['REQUEST_URI'] == $value ):
		$class = 'active';
	endif;
	$nav .= "\t\t" . '<li class="' . $class . '"><a href="' . $value . '">' . ucfirst( $key ) . '</a></li>' . PHP_EOL;
endforeach;

?>
	<ul>
<?php
echo $nav;
?>
	</ul>
</div>

Hey thanks a lot, ya I had a completely different way in my mindset.

I was thinking of something like

$links = array('dashboard' => '/dashboard.php', 'inbox' => '/mail.php')

something to that affect, but I wasn't sure how to include in my IF statement.... or sure if it had correct syntax heh.

but thank you, if opened up my mind to using the foreach more efficiently!

Link to comment
Share on other sites

  • 0

Hey thanks a lot, ya I had a completely different way in my mindset.

I was thinking of something like

$links = array('dashboard' => '/dashboard.php', 'inbox' => '/mail.php')

something to that affect, but I wasn't sure how to include in my IF statement.... or sure if it had correct syntax heh.

but thank you, if opened up my mind to using the foreach more efficiently!

writing

$links['dashboard'] = '/dashboard.php';

is the same as

$links = array('dashboard' => '/dashboard.php')

Its just a different way of defining an array.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.