Results 1 to 3 of 3

Thread: syntax for if/else

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    syntax for if/else

    Guys,

    Not done much in php, could somebody help me with the syntax for this bit of code please?

    Basically, if the current session alliance, alianta = "EximuS" then the filter should continue with the numele, else it should only show current alliance data.

    tia
    Bob


    Code:
    ********* syntax wrong here ***
    
    if ($_SESSION['alianta']=="EximuS") {
      $aliantaSel = $_SESSION['numele'];
    else
    $aliantaSel = $_SESSION['alianta'];
    
    ****************************
    
      if (isset($_GET['numele'])) {
      
       $sql="SELECT
                name,
                sid,
                volk,
                rechte,
                lastchange,
      alianta
              FROM
                ".PREFIX."spieler
              WHERE
                aktiv='1' AND
                alianta='".$aliantaSel."'
              ORDER BY
                ".$_SESSION['sort']."";
    
    
      } 
    
    else {
        $sql="SELECT
                  name,
                  sid,
                  volk,
                  rechte,
                  lastchange,
        alianta
                FROM
                  ".PREFIX."spieler
                WHERE
                  aktiv='1'
            ORDER BY
              ".$_SESSION['sort']."";
    
      } 
    // }
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  2. #2
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: syntax for if/else

    Hi staticbob,

    The problem with the top code is you haven't closed and opened the braces, it should be
    PHP Code:
    if ($_SESSION['alianta']=="EximuS") {
      
    $aliantaSel $_SESSION['numele'];
    }
    else
    {
    $aliantaSel $_SESSION['alianta']; 
    And at the bottom, your final closing brace is commented out, that needs to be uncommented and back in your code because it is the closing brace with the parent if statement.

    I would also suggest you keep your coding properly indented (I'm not sure if it looks the way it does because it's not in [ PHP][ /PHP] tags and I can see some if it is indented but some isn't), this will help you spot problems. For example I personally find it easy to read if statements in this format:

    PHP Code:
    if($condition)
    {
         if(
    $condition2)
         {
              
    //code
         
    }
         else
         {
              
    //code
         
    }
    }
    else
    {
         
    //code

    Hope this helps
    Chris

  3. #3
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: syntax for if/else

    I was slightly confused with this one, mainly because it looked as if you wanted to only execute one line of code after the first else...

    ... but, after scrolling down it would become apparent that the second condition statement is nested within the first...

    My point, learn to indent code correctly, it makes it easier for yourself and us to read

Posting Permissions

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



Click Here to Expand Forum to Full Width