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
    922
    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 02:38 AM.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,309
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #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
    922
    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
    •