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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    4
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Simple if () conditional using strings

    Hey guys,

    Kinda new to this php lark and wasn't able to find anywhere that could explain why this is not working correctly -

    Basically i'm working on a menu that changes the UL background according to which section the page is currently on.

    I have a template index.php page that changes according to index.php?page=pagename

    What I have in the menu is -

    PHP Code:
    <?php 
    $pagename 
    $_GET['page'];
    function 
    setcurrent($name) {
        echo 
    "<ul class='section ".$name." current'>";
    }
    if (
    $pagename == 'whybamboo' || 'environmentallysound' || 'ourclients') {
        
    setcurrent(whybamboo);
    }
    else
    {
    echo 
    "<ul class='section whybamboo'>";
    }
    ?>
    Can anyone explain to me why the || operators are not working and if there is a simpler way of achieving this.

    I also have a problem where i'd like the title of the page to change according to the ?page=pagename variable also. Now while I can appreciate that this could be done with an array or switch conditional, what is the most common way that dynamic titles on web pages is achieved?

    Cheers.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    PHP Code:
    if ($pagename=='whybamboo' || $pagename=='environmentallysound' || $pagename=='ourclients') {
        
    setcurrent($pagename); //or setcurrent('whybamboo');//see below

    I am not sure if the above is actually doing what you want it to do , are you saying IF whybamboo OR environmentally sound OR ourclients then regardless send to 'whybamboo' , or do you want to setname() whatever the $_GET['page'] is ?

    if so an array of allowable names might be a plan..
    PHP Code:
    $okpages = array('whybamboo','evironmentallysound','ourclients','blah');
    if(
    in_array($_GET['page'],$ok_pages)){
        
    setcurrent($_GET['page']);
        
    $htmltitle $_GET['page'];
    }else{
      echo 
    'invalid page selection';
      
    $htmltitle='Error';

    <html title="<?=$htmltitle;?>">

    hope I am on the right track here ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Location
    New Zealand
    Posts
    4
    Thanks
    0
    Thanked 2 Times in 2 Posts
    First solution worked great! Simple conditional error.

    I didn't think there was any difference between them but yea:

    PHP Code:

    if ($variable == 'a' || 'b' || 'c')
    {


    and

    PHP Code:


    if ($variable == 'a' || $variable ==  'b' || $variable ==  'c')
    {




  •  

    Posting Permissions

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