Jump to content

[SOLVED] if else statement


steviemac

Recommended Posts

I am new to php mysql.  I am trying to use the if else statement.  I cannot seem to get it to work. It always tells me the course is open even if the seats are equal to six. I would appreciate any help.

<?php
$query = "SELECT SUM(Seats) FROM table"; 

$result = mysql_query($query) or die(mysql_error()); {
// Seats left code
while($results = mysql_fetch_array($result))

if $result >= 0 ){
	echo "Open";

} elseif($result == 6){
	echo "The course closed.";

}else {
echo "I'm out of options";
}
}
?>

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/
Share on other sites

Well, I see a couple things.

 

1) The IF statement is malformed. It should be

 

if ($result >= 0 ){

 

2) I don't thing $result is the value you want to be testing - it is simply a pointer to the query results. You need to test the value in the result set.

 

3) There is a closing bracket for your while loop but not an opening bracket.

 

Try this:

<?php
$query = "SELECT SUM(Seats) as totalSeats FROM table"; 

$result = mysql_query($query) or die(mysql_error()); {
// Seats left code
while($record = mysql_fetch_array($result)) {

  if ($record['totalSeats'] >= 0){
    echo "Open";
  } elseif ($record['totalSeats'] == 6){
    echo "The course closed.";
  }else {
    echo "I'm out of options";
  }
}
?>

 

 

Well, I see another proble now. Look at your first two conditions. The first one is true if the value is greater than or equal to 0. The second one is true if the value is equal to 6. The 2nd condition will NEVER be run because a value of 6 will be true on the first condition.

 

I think you need to rethink your logic. I was going to try and rework it for you, but I'm really not sure what you want to happen for specific values. Can you elaborate?

<?php
$query = "SELECT SUM(Seats) as totalSeats FROM table"; 

$result = mysql_query($query) or die(mysql_error()); {
// Seats left code
while($record = mysql_fetch_array($result)) {

  if ($record['totalSeats']  == 0 || $record['totalSeats'] <= 5){
    echo "Open";
  } elseif ($record['totalSeats'] == 6){
    echo "The course closed.";
  }else {
    echo "I'm out of options";
  }
}
?>

 

I think that may work. You need the 1st part of the if statement to mean if there are 0 rows but equal too or less than 5 rows... i think i understand... or i might be tired lol

It is going to be an online registration.  What I am trying to is set it that the first 10  people or what ever the course will call for who sign up the echo will say "OPEN".  Once there are 10 in the DB under the SEATS column the echo will say "FULL".

I never had any logic to begin with.  I should have had my son look at the math part.

 

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.