Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-06-2012, 01:33 AM   PM User | #1
alexandruv
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
alexandruv is an unknown quantity at this point
Question unexpected T_ELSEIF in ...

I'm having a little problem at figuring this out:
Code:
function myMenu() //Creates a menu with a class current to the current page item
{
    $menuItems = array (
        "Strona Głowna" => "index.php",
        "O nas" => "o-nas.php",
        "Oferta" => "oferta.php",
        "Polecane kierunki" => "polecane-kierunki.php",
        "kontakt" => "kontakt.php",
    );
    foreach($menuItems as $name => $url) {
        $class = 'default';
        $index = 'index.php';
            if ((curPageName() == $url) && (curPageName() !== $index)) {
                $class='current';
            }
            echo "<li><a href='$url' class='$class'>$name</a></li>";
            elseif (curPageName() == $index) {
                $class='current home';
            }
            echo "<li><a href='$url' class='$class'>$name</a></li>";
        };
};
Basically I want to assign the 'current home' to the index.php ONLY. Maybe you can come with better solutions, I'm a newbie at this. Anyway, this gives me: PHP Parse error: syntax error, unexpected T_ELSEIF in ...

Any help sorting this out appreciated!
alexandruv is offline   Reply With Quote
Old 12-06-2012, 02:30 AM   PM User | #2
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
I think is with your if and elseif which need to be paired together. (without any other code inbetween the two).

for example:

PHP Code:
if(something){
//do this
}
elseif(
something different){
//do something else
}
else{
//acts like a 'default' should none of the other if/ifelse statements execute.

Kind regards,

LC.

Last edited by LearningCoder; 12-06-2012 at 02:38 AM..
LearningCoder is offline   Reply With Quote
Old 12-06-2012, 08:56 AM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
As LC says, you need to put the code inside the { and } braces. This is your code:
Code:
            if ((curPageName() == $url) && (curPageName() !== $index)) {
                $class='current';
            }
            echo "<li><a href='$url' class='$class'>$name</a></li>";
            elseif (curPageName() == $index) {
                $class='current home';
            }
That bold red line cannot be there. It must be either completely outside the if/elseif or inside one of the brace sets.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 12-06-2012, 09:45 AM   PM User | #4
alexandruv
New to the CF scene

 
Join Date: Dec 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
alexandruv is an unknown quantity at this point
OK so I changed it a bit to this:

Code:
function myMenu() //Creates a menu with a class current to the current page item
{
		$menuItems = array (
			"Strona Głowna" => "index.php",
			"O nas" => "o-nas.php",
			"Oferta" => "oferta.php",
			"Polecane kierunki" => "polecane-kierunki.php",
			"kontakt" => "kontakt.php",
		);
		foreach($menuItems as $name => $url) {
			$class = 'default';
			$index = 'index.php';
				if ((curPageName() == $url) && (curPageName() != $index)) {
					$class='current';
					echo "<li><a href='$url' class='$class'>$name</a></li>";
				}
				elseif (curPageName() == $index) {
					$class='current home';
					echo "<li><a href='$url' class='$class'>$name</a></li>";
				}
				
			};
};
But now, ALL menu items have the class "home current" - why?

Thank you!
alexandruv is offline   Reply With Quote
Old 12-06-2012, 01:30 PM   PM User | #5
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
If that is the case, it would seem your elseif statement is always executing.

Can you post your code for the curPageName() function? Maybe it could be the return value?

Kind regards,

LC.
LearningCoder is offline   Reply With Quote
Reply

Bookmarks

Tags
php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:05 AM.


Advertisement
Log in to turn off these ads.