2 Replies - 1321 Views - Last Post: 07 October 2011 - 09:59 PM Rate Topic: -----

#1 mattrmclaren  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 10-September 11

If else with multiple variables

Posted 07 October 2011 - 06:29 PM

I just started coding a very simple PHP program to check a user's various attribute values to determine their chances of getting into certain universities. Originally, as you'll see below, I was checking each variable separately but decided it would be more efficient to check them altogether, as the attribute values aren't all independent of one another. When I run the code below, it's ignoring the first if else statement (the one which groups the variables together) and simply continues with the if else statements that check variables independently. What am I missing? Please and thank you.
<!DOCTYPE html>
    <html id="en_us" lang="en">
<head>
<h1>Dataweavr</h1>
</head>
<body>
<?php 
if (!isset($_POST['submit'])){
    ?>
    <form method="post" action="dataweavr.php">
    SAT score: <input type="text" name="satscore" /><br />
    GPA: <input type="text" name="gpa" /><br />
    Grade: <input type="text" name="grade" /><br />
    AP Classes: <input type="text" name="AP classes" /><br />
    <input type="submit" value="submit" />
    </form>
    
    <?php
    } else {
    $sat = $_POST['satscore'];
    $gpa = $_POST['gpa'];
    $grade = $_POST['grade'];
    $ap = $_POST['ap'];
    if ($sat >= '2100' && $gpa >= '4' && $grade == 'freshmen' && $ap >= '2'){
        echo "Great job! This test worked!";
    }   elseif ($sat <= '2100' && $gpa <= '4' && $grade == 'sophmore' && $ap <= '2') {
            echo "Terrible job! But this test still works!";
    }
    }
    ?>
    
    //*
     <?php
    } else {
    $sat = $_POST['satscore'];
    if ($sat >= '2100'){
        echo "Great score!";
    } elseif ($sat <= '2000') {
        echo "You need to retake the SAT!";
    }
    }
    ?>
    <?php
    $gpa = $_POST['gpa'];
    if ($gpa >= '4') {
        echo "Great GPA!";
    } elseif ($gpa <= '4') {
        echo "You need to get straight A's!";
    }
    ?>
    <?php
    $grade = $_POST['grade'];
    if ($grade == 'freshmen') {
        echo "You're on track!";
    }
    ?>
    <?php
    $ap = $_POST['ap'];
    if ($ap >= '3') {
        echo "You're a terrific Harvard candidate!";
    }
    ?>
    *//
    
</body>
</html>


This post has been edited by macosxnerd101: 07 October 2011 - 06:35 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: If else with multiple variables

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,205
  • Joined: 19-January 10

Re: If else with multiple variables

Posted 07 October 2011 - 08:44 PM

I see no problems with your if...else, but I find it weird how you separate every tidbit of code with php tags.

Oh, and your $_POST['ap'] should be $_POST['AP classes'], remember, the names have to be exact.
Was This Post Helpful? 1
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: If else with multiple variables

Posted 07 October 2011 - 09:59 PM

You should probably convert the numeric values to integer:
    $sat = (int)$_POST['satscore'];
    $gpa = (int)$_POST['gpa'];
    $grade = $_POST['grade'];
    $ap = (int)$_POST['ap'];
    if ($sat >= 2100 && $gpa >= 4 && $grade == 'freshmen' && $ap >= 2)
	{
        echo "Great job! This test worked!";
    } elseif ($sat <= 2100 && $gpa <= 4 && $grade == 'sophmore' && $ap <= 2) {
            echo "Terrible job! But this test still works!";
    }

This will ensure your comparisons are working correctly. For example, with strings:
  • $sat = '2100' ... $sat >= '2100' evaluates to true
  • $sat = '213' ... $sat >= '2100' evaluates to true

... but with integers...
  • $sat = 2100 ... $sat >= 2100 evaluates to true
  • $sat = 213 ... $sat >= 2100 evaluates to false

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1