steviemac Posted February 5, 2007 Share Posted February 5, 2007 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 More sharing options...
Psycho Posted February 5, 2007 Share Posted February 5, 2007 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"; } } ?> Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177725 Share on other sites More sharing options...
steviemac Posted February 5, 2007 Author Share Posted February 5, 2007 It is still telling me the course is open even though it should say full. ??? Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177736 Share on other sites More sharing options...
Psycho Posted February 5, 2007 Share Posted February 5, 2007 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? Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177749 Share on other sites More sharing options...
Daney11 Posted February 5, 2007 Share Posted February 5, 2007 <?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 Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177755 Share on other sites More sharing options...
steviemac Posted February 5, 2007 Author Share Posted February 5, 2007 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 Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177759 Share on other sites More sharing options...
Daney11 Posted February 5, 2007 Share Posted February 5, 2007 Did u try my peice of code?? Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177762 Share on other sites More sharing options...
steviemac Posted February 5, 2007 Author Share Posted February 5, 2007 That was it. I knew I should have paid attention in school. Thank you for your time. Steviemac Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177767 Share on other sites More sharing options...
Daney11 Posted February 5, 2007 Share Posted February 5, 2007 Glad i could help. Keep up with PHP and if you have anymore troubles be sure to come and post on here, there are a lot of people always about to help you with anything. Its a really good community to be in if you want to learn PHP. Link to comment https://forums.phpfreaks.com/topic/37206-solved-if-else-statement/#findComment-177768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.