CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   unexpected T_ELSEIF in ... (http://www.codingforums.com/showthread.php?t=283620)

alexandruv 12-06-2012 01:33 AM

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!

LearningCoder 12-06-2012 02:30 AM

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.

tangoforce 12-06-2012 08:56 AM

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.

alexandruv 12-06-2012 09:45 AM

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!

LearningCoder 12-06-2012 01:30 PM

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.


All times are GMT +1. The time now is 01:40 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.