0

I'm attempting to implement multiple if else conditions which check the status of a proposal and should only execute a specific code if the status code is set to 1 or 5.

For some reason I'm having difficulties with implementing this. Currently the logic in the code is if the proposal status does not match 1 or 5 then returns a message otherwise execute the next query. When I specify just the one number i.e. (1 or 5) it will work fine.

Another problem I am facing with if and else conditions is in this part:

if ($count == 1) {

        $feedback = '<p class="text-danger"> You have already accepted an application. You cannot accept or apply for any others. If this is a mistake then please contact the supervisor concerned directly.</p>'; 
    }

    if ($count < 1) {

        $status = $db_conx->prepare ("SELECT status_code FROM record WHERE student_record_id = :user_record_id AND proposal_id = :proposal");

        $status->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $status->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $status->execute();

        $proposalstatus = $status->fetchColumn();

        if($proposalstatus != 1)
        {
                //echo $proposalstatus;
            $feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>';
        }
    }

    else {

This works when I run each part separately but when I try and put it together in an if statement it fails and doesn't check for these conditions at all and just completes the task which updates the database and displays a success message.

The full code is here:

try
    {
     $db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);

     $db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     $username = $_SESSION['username'];

     $sql = $db_conx->prepare("SELECT username, user_record_id FROM login_details
        WHERE username = :username");

     $sql->bindParam(':username', $username, PDO::PARAM_STR);

     $sql->execute();
     $user_record_id = $sql->fetchColumn(1);

     $proposal = $_POST['proposal_id'];

     $acceptCheck = $db_conx->prepare ("SELECT * FROM record WHERE student_record_id = :user_record_id AND status_code = 3");
     $acceptCheck->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
     $acceptCheck->execute();

     $count = $acceptCheck->rowCount();

     if ($count == 1) {

        $feedback = '<p class="text-danger"> You have already accepted an application. You cannot accept or apply for any others. If this is a mistake then please contact the supervisor concerned directly.</p>'; 
    }

    if ($count < 1) {

        $status = $db_conx->prepare ("SELECT status_code FROM record WHERE student_record_id = :user_record_id AND proposal_id = :proposal");

        $status->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $status->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $status->execute();

        $proposalstatus = $status->fetchColumn();

        if($proposalstatus != 1 || 5) //status must be either 'Approved' code 1 or 'Held' code 5
        {
                //echo $proposalstatus;
            $feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>';
        }
    }

    else {

                //Update all application records to 'Not available' when a proposal has been accepted

        $updateOtherRecords = $db_conx->prepare("UPDATE record SET status_code = 8, last_updated = now()
            WHERE proposal_id = :proposal");
        $updateOtherRecords->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $updateOtherRecords->execute();

                //Update other applicationa for the user concerned to 'Rejected'

        $updateUserRecord = $db_conx->prepare("UPDATE record SET status_code = 7, last_updated = now()
            WHERE student_record_id = :user_record_id");
        $updateUserRecord->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $updateUserRecord->execute();

                //Update the proposal concerned and assign it to the user

        $update = $db_conx->prepare("UPDATE record SET status_code = 3, last_updated = now()
            WHERE proposal_id = :proposal AND student_record_id = :user_record_id");
        $update->bindParam(':user_record_id', $user_record_id, PDO::PARAM_STR);
        $update->bindParam(':proposal', $proposal, PDO::PARAM_STR);
        $update->execute();

        $feedback = '<p class="text-success"> The proposal has been successfully accepted <span class="glyphicon glyphicon-ok"/></p>';
    }
} 

I really need to know how I can sort this because I'll be using if and else a lot in this statement. Any guidance would be much appreciated!

Thank you in advance!

6
  • 4
    The expression $proposalstatus != 1 || 5 doesn't do what you mean it to. You have to fully express both conditions so it looks like if($proposalstatus != 1 && $proposalstatus != 5) which means changing it to && to make the logic work. Commented Apr 15, 2015 at 20:09
  • vardump($proposalstatus); , you should use. Commented Apr 15, 2015 at 20:09
  • print_r($proposalstatus) is useful too Commented Apr 15, 2015 at 20:13
  • @MichaelBerkowski by using && that would mean both conditions need to be true for the query to execute right? so would i use | | for OR? because i need either of those status codes to be present for the code to execute Commented Apr 15, 2015 at 20:14
  • 1
    @user610 If you use || with != either of them negates the other. If the value is != 1, it may be == 5, but the logical OR would still cause that condition to be true. So you need it to be != 1 AND != 5. You could also express it with arrays: if (!in_array($proposalstatus, array(1,5))) Commented Apr 15, 2015 at 20:16

2 Answers 2

2

Your conditions aren't mutually exclusive

if ($count < 1) { 
  some stuff
}

if ($count == 1) {
 ...
} else 
 ... this code will execute when $count is *NOT* equal to 1,
  which includes when it's LESS than 1, e.g. "< 1" is true here
}

Perhaps you want

if ($count == 1) {
} else if ($count < 1) {
} else {
}

So that the final else will only run if/when $count >= 1

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your response. Yeah basically, i just want to check if the count is 1 and if it is then to return the first message and if its not then to execute the second part. so you're saying i should do this as an else if condition?
well, consider that your code has part 1 (the first if), and then parts 2A and 2B. as written, you'd execute parts 1 and 2B if $count really is 1. no idea if that's what you want.
im a little confused. part 1 would check whether or not the count == 1 and if it doesn't then part 2A would be count < 1 which would then check the status of the proposal and if thats all good then i just want it to continue from there. i think I'm confused myself because i have lots of nested if statements to check various things
is it possible to have an if else within another if else? without it causing issues?
you can nest if() as deeply as you want, but it's usually a sign of bad code design, and makes it very hard to follow the program logic. it's an opinion, but anything deeper than 2 levels should be avoided.
1

replace your condition with proposal status 1 or 5

if(!($proposalstatus == 1 || $proposalstatus == 5)) {
$feedback = '<p class="text-danger">The proposal is not at a status where it can be accepted</p>';
}

1 Comment

thanks for your input. both this solution and the one provided by @MichaelBerkowski work perfectly :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.