Jump to content

if...else and insert <code> help


mcrackin
Go to solution Solved by mcrackin,

Recommended Posts

Hi everyone,

 

I'm working on a personal website with a friend from school. You can see it here http://luptakmcleod.co.nf

 

You can see that I've spent considerable time on the main menu. When the current page is open, the menu will show the appropriate image, while all other menu items will show the mouseover effect with the appropriate image. For each page corresponding with each menu item, I've had to create different code for the menu to show the right image when that page is open.

 

Now, I want to use the <?php include> tag for the menu and place the menu in a single file (menu.php) so that when I make changes to the menu, the changes appear on every page. The problem with this is that I need to have a certain image open for each page. Ie. if index.php is currently open, then the menu_home_open.jpg image will appear instead of the hover image. When I am in the aboutus.php file, the menu_about_open.jpg image should appear instead of the hover image.

 

The only way I can think of doing this is by the if...else statements. I am extremely new to php so I have little knowledge on how to do this. I've started with the following:

 

//The ifelse code
<?php


$menuopen = $_SERVER['PHP_SELF'];


if ($menuopen = "index.php")
{
$homeimage = "<a href="index.php" border="0"><img src="menu_home_open.jpg"></a>";
}
else
{
$homeimage = "<a class="homeButton" href="index.php">home</a>";
}


?>


//Place this where I want the code to insert
<?php include '$homeimage'; ?>

I'm not sure of any of the above and I"m sure there's basic errors. I'm very newbie. But as you can see I basically want a line of code to appear if that page is open, or else the other line of code to appear.

 

 

 

Any help is appreciated!

Edited by mcrackin
Link to comment
Share on other sites

Are you just looking for feedback on best practices...or is something not working? Note that you'll want to review the quotes within the code. In some cases, you have double quotes within double quotes. Later, there is a variable within single quotes...which won't work.

 

For what it's worth, using if statements should work just fine. You could, however, limit the number times you write $menuopen by using switch() instead:

http://php.net/manual/en/control-structures.switch.php

Link to comment
Share on other sites

To start, its not working. 

 

Parse error: syntax error, unexpected T_STRING in /srv/disk2/1429064/www/luptakmcleod.co.nf/indextest.php on line 8

 

It's probably something to do with all the quotes?

Link to comment
Share on other sites

To start, its not working. 

 

Parse error: syntax error, unexpected T_STRING in /srv/disk2/1429064/www/luptakmcleod.co.nf/indextest.php on line 8

 

It's probably something to do with all the quotes?

 

Yep, double quotes can't be used within double quotes unless they are escaped. You can also switch the quote types. For example

<?php
//...
 
$homeimage = '<a href="index.php" border="0"><img src="menu_home_open.jpg"></a>';  //<-- the outside quotes were changed to single quotes
 
//...
?>

More information about using quotes with strings can be found here:

http://php.net/manual/en/language.types.string.php

Link to comment
Share on other sites

As for approach, using a switch is one way, or something like this (not tested):

//menu.php
$menu = array(
    'index.php' => array('class'=>'homeButton', 'text'=>'home', 'image'=>'menu_home_open.jpg'),
    'aboutus.php' => array('class'=>'aboutButton', 'text'=>'about us', 'image'=>'menu_aboutus_open.jpg'),
    'contactus.php' => array('class'=>'contactButton', 'text'=>'contact us', 'image'=>'menu_contactus_open.jpg')
);

foreach($menu as $page => $options) {
    $link = '<a href="'.$page.'" class="'.$options['class'].'">';
    if($page == basename($_SERVER['PHP_SELF'])) {
        $link .= '<img src="'.$options['image'].'" alt="'.$options['text'].'"></a>';
    } else {
        $link .= $options['text'].'</a>';
    }
    echo $link;
}
Link to comment
Share on other sites

Here's my new code.


<?php

$menuopen = $_SERVER['PHP_SELF'];

if ($menuopen = "/indextest.php")
{
	$homeimage = '<a href="index.html" border="0"><img src="menu_home_open.jpg"></a>';
}
else
{
	$homeimage = '<a class="homeButton" href="index.php">home</a>';
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="lmcstyle.css">
</head>
<body>

<?php echo $homeimage; ?>

</body>
</html>

Everything appears to be working except the $_SERVER['PHP_SELF'] part. No matter what the name is of the current file, it still shows the "open" code in the document. You can see this by visiting this link and this link

 

Am I using the wrong code to get the current open file?

Link to comment
Share on other sites

Ok it's working now with this code. You can see what happens to the exact same code when indextest.php is open, and when something else is open.


<?php

$menuopen = basename($_SERVER['PHP_SELF']);

if($menuopen == 'indextest.php')
{
	$homeimage = '<a href="index.html" border="0"><img src="menu_home_open.jpg"></a>';
}
else
{
	$homeimage = '<a class="homeButton" href="index.php">home</a>';
}

?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="lmcstyle.css">
</head>
<body>

<?php echo $homeimage; ?>

</body>
</html>

But like you guys say, I'll have to redo this code for each menu item (7 times). Can someone work with me with the code in this reply to get the desired effect for all 7 menu items?

Link to comment
Share on other sites

  • Solution

Thanks for the help everyone. You can see the menu in action here.

 

The full menu code can be seen here. menu.txt is inserted via php include for each layout.php. The code isn't as refined as it could be, but it does everything I want it to. Using the switch/elseif commands would only reduce the code by 1kb so I'm not too concerned. I'll keep an eye on this thread if anyone want to help me out with refining the code. It's an easy edit now since I have the entire code in menu.txt =)

Edited by mcrackin
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.