1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to write if...elseif...else

Discussion in 'PHP' started by slsv, Jul 31, 2014.

  1. #1
    Hi,
    I have this code
    while($rndmpnts = mysql_fetch_array($random_points_sql)) {
            $calculate = $rndmpnts['time_last'] + $time_now;
            if ($calculate > $time_now) {
                echo "<div class='alert-box error'><span>Error:</span> </div>";
            } else {
                mysql_query("UPDATE 'users' SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'");
                mysql_query("UPDATE 'random_points' SET 'time_last` = '$time_now' WHERE 'username'' = '$my_username', 'user_id' = '$my_id'");
            }
        }
        else {
            mysql_query("UPDATE 'users' SET points = '$new_points' WHERE 'username' = '$my_username', 'user_id' = '$my_id'");
            mysql_query("INSERT into 'random_points'('username', 'user_id', 'time_last', 'points_last') VALUES ('$my_username', '$my_id', '$time_now', '$rand_points')");
        }
    PHP:
    So if $calculate > $time_now I get error else I've update database. But I need 3rd else here. How can be done?
    Can be done 3rd else to become if?
    while($rndmpnts = mysql_fetch_array($random_points_sql)) {
            $calculate = $rndmpnts['time_last'] + $time_now;
            if ($calculate > $time_now) {
                echo "<div class='alert-box error'><span>Error:</span> </div>";
            } else {
                mysql_query("UPDATE 'users' SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'");
                mysql_query("UPDATE 'random_points' SET 'time_last` = '$time_now' WHERE 'username'' = '$my_username', 'user_id' = '$my_id'");
            }
        }
        if ($calculate < $time_now){
            mysql_query("UPDATE 'users' SET points = '$new_points' WHERE 'username' = '$my_username', 'user_id' = '$my_id'");
            mysql_query("INSERT into 'random_points'('username', 'user_id', 'time_last', 'points_last') VALUES ('$my_username', '$my_id', '$time_now', '$rand_points')");
        }
    PHP:

     
    Solved! View solution.
    slsv, Jul 31, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    That will always trigger, unless the $calculate and $time_now variables are exactly the same.
    (basically, what you're doing is checking for one condition, and if that isn't true, you trigger the first else-block.
    I'm assuming that what you're trying to do is check the variable, and if it's higher than $time_now, throw an error (which isn't defined), and if not, update the rows of they exist, and if they don't exist, insert a new row?
     
    PoPSiCLe, Jul 31, 2014 IP
  3. slsv

    slsv Active Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Yes, this is what I'm trying.
    Is defined outside the loop:
    $time_now = time();
     
    slsv, Jul 31, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Then you need to check and see if the query returns any rows, and if not, insert. If it does, update. Besides, you shouldn't be using mysql_ - it's deprecated, and full of potential security holes. Use mysqli_ or PDO instead, and prepared queries.
     
    PoPSiCLe, Jul 31, 2014 IP
  5. slsv

    slsv Active Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #5
    My problem is that my code is like first snippet - 1 if with two else`s and I get error "Parse error: syntax error, unexpected 'else' (T_ELSE) in..." .. Now I know what it means but I don't know how exactly to construct this IF-ElSEIF-ELSE . In code like this what can I put for second condition in elseif() ? $calculate<$time_now?
     
    slsv, Jul 31, 2014 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    You're putting the second else as an else to the while-loop, which won't work. As I said, you need to check the result returned from the query to see if there should be inserted or updated. Also, elseif needs to be done before the last else (if, elseif, else) and it needs to be within the loop. I'm on my phone, which makes writing code a hassle, else I would write an example.
     
    PoPSiCLe, Jul 31, 2014 IP
  7. dreamer_12

    dreamer_12 Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    Try to use Switch Case for conditions like these. Its more optimized and you can have a default section which runs in case values doesn't match anything. Also you can run multiple sections by not putting a break statement in code after section finishes.

    Check php doc for switch case for more information on this.
     
    dreamer_12, Jul 31, 2014 IP
  8. #8
    We need to do small correction for your code, What you are doing is:
    If calculate time is greater than time now, then you are showing error messages, and in remaining cases you are updating the query.
    But you need to check one more condition then only you need to update.We can resolved it with IF-ElSEIF-ELSE like below.

    while($rndmpnts = mysql_fetch_array($random_points_sql)) {
    $calculate = $rndmpnts['time_last'] + $time_now;
    if ($calculate > $time_now) {
    // your error code
    echo "<div class='alert-box error'><span>Error:</span> </div>";
    }
    else if ($calculate < $time_now){
    // if above condition is failed then this block will be executed, write your code here
    }
    else {
    //If above 2 conditioned failed then this block will be executed
    mysql_query("UPDATE 'users' SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'");
    mysql_query("UPDATE 'random_points' SET 'time_last` = '$time_now' WHERE 'username'' = '$my_username', 'user_id' = '$my_id'");
    }
    }
     
    Last edited: Jul 31, 2014
    xhtmlchamps, Jul 31, 2014 IP
  9. slsv

    slsv Active Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #9
    What if I get the second else out of the while loop? Like this
            while($rndmpnts = mysql_fetch_array($random_points_sql)) {
                    $calculate = $rndmpnts['time_last'] + $time_now;
                  
                    if ($calculate > $time_now) {
                            echo "<div class='alert-box error'><span>Error:</span></div>";
                    }
                    else {
                            mysql_query("UPDATE `users` SET points = '$new_points' WHERE username = '$my_username', user_id = '$my_id'");
                            mysql_query("UPDATE `random_points` SET `time_last` = '$time_now' WHERE `username` = '$my_username', `user_id` = '$my_id'");
                    }
            }
            else {
                    mysql_query("UPDATE `users` SET points = '$new_points' WHERE `username` = '$my_username', `user_id` = '$my_id'");
                    mysql_query("INSERT into `random_points`(`username`, `user_id`, `time_last`, `points_last`) VALUES ('$my_username', '$my_id', '$time_now', '$rand_points')");
            }
    }
    PHP:
    In this case I think I don't need second condition or I'm wrong?
     
    Last edited: Aug 1, 2014
    slsv, Jul 31, 2014 IP
  10. xhtmlchamps

    xhtmlchamps Active Member

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    53
    #10
    Actually this process will not give result what you are expecting. If you write update condition out of the while loop then it will check with last record only.
    But it will not check remaining records, that is why we need to keep update query in while loop only.
     
    xhtmlchamps, Jul 31, 2014 IP
  11. slsv

    slsv Active Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #11
    Thank's for help!
     
    slsv, Aug 1, 2014 IP
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    Actually, an "else" after a while-loop will fail - it will throw an error, simply because you're lacking the if {}-statement for the else {} - so it won't check the "last record", it will fail, with either a critical error or a warning (I'm not sure which, I couldn't be bothered testing it right now.
     
    PoPSiCLe, Aug 1, 2014 IP