Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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 03:38 AM.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    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.
    I can't really think of anything to write here now...

  • #4
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #5
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    925
    Thanks
    76
    Thanked 29 Times in 29 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •