Walker33 Posted May 6, 2009 Share Posted May 6, 2009 Good morning! my select statement is returning four rows. If the first row gives me a result of $free_trial = true; I need it to go back and check the second, third and fourth for a $free_trial = false; thereby giving the user the false message. As I have it, if the first row = true, it doesn't check the next four rows. <?php // check table to see if reporter has been an active user in last 18 months $query3 = mysql_query("SELECT * FROM customer_details WHERE FirstName = '$InitFName' AND LastName = '$InitLName' AND Email = '$InitEmail' and status like '%revoked%'"); $num3 = mysql_num_rows($query3); // the date - 18 months from now $expire = date('Y-m-d', strtotime('-18 MONTHS')); // this variable determines if free trial is allowed $free_trial = false; while (!$free_trial && $result = mysql_fetch_assoc ($query3)) { //converting date so that year comes first and can use > or < for comparison. $my_date = $result["End_Date"]; $my_time = strtotime($my_date); $datenew = date('Y-m-d',$my_time); if($num3>0 && $selected_radio=='reporter' && $datenew < $expire) {$free_trial = true; echo ""; } else { $error .= "<p align='justify'><font face='Times New Roman' size='3'>You were an existing user up until ".$datenew.". Blah blah. <br>"; // End validation if($error){ echo("<align='left'><font face='Times New Roman' size='5'><b>ERROR :</b> $error <br>"); echo('<a href="javascript:history.back(1)">Back to the Free Trial page</a> <br>'); exit; } } } ?> Any help is GREATLY appreciated! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/ Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Take out !$free_trial in the while loop. Along with the &&. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827707 Share on other sites More sharing options...
Zhadus Posted May 6, 2009 Share Posted May 6, 2009 Change it to look like this: <?php // this variable determines if free trial is allowed $free_trial = true; while (($free_trial == true) && ($result = mysql_fetch_assoc ($query3))) { //converting date so that year comes first and can use > or < for comparison. $my_date = $result["End_Date"]; $my_time = strtotime($my_date); $datenew = date('Y-m-d',$my_time); if($num3>0 && $selected_radio=='reporter' && $datenew < $expire) {$free_trial = false; echo ""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827710 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Zhadus, you have syntax errors there. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827713 Share on other sites More sharing options...
Zhadus Posted May 6, 2009 Share Posted May 6, 2009 Fixed I believe. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827716 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Fixed I believe. Nope. I'm referring to your closings on curly braces. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827717 Share on other sites More sharing options...
Walker33 Posted May 6, 2009 Author Share Posted May 6, 2009 Ken's suggestion did the trick. Thanks! But is there a reason to switch it around to Zhadus's way? Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827720 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Not really. Zhadus's way fails because if the return values are - true, true, false, true - his loop will stop after it hits false. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827723 Share on other sites More sharing options...
Zhadus Posted May 6, 2009 Share Posted May 6, 2009 Fixed I believe. Nope. I'm referring to your closings on curly braces. It was segmented code, not complete. Glad his info worked for you Walker33. My code was responding to where you stated that if it comes back as true you want it to check, therefore I figured if it was "false" you do not want it to continue the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827728 Share on other sites More sharing options...
Walker33 Posted May 6, 2009 Author Share Posted May 6, 2009 okay, I think I understand. Thanks a ton for your help! And I'm trying to understand, as well as get it right, so if I could ask one question: What did the !$free_trial represent? Does it mean free_trial = false doesn't exist? I was never clear on that or how it worked when it was suggested to me yesterday. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827735 Share on other sites More sharing options...
Zhadus Posted May 6, 2009 Share Posted May 6, 2009 !$free_trial means if $free_trial is equal to false. What you did in your code was assign $free_trial equal to false, so when it ran the loop it would go once. Whenever a record return that value to true, it would stop the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827738 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Okay let me clarify. A while loop runs as long as the condition is true right? So if $free_trial is false, !$free_trial is true. ! == not. So not false is true. And vice-versa. But if !$free_trial == false (meaning $free_trial has to be true for !$free_trial to be false), the while loop stops because false && anything is false. This is boolean logic. Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827742 Share on other sites More sharing options...
Walker33 Posted May 6, 2009 Author Share Posted May 6, 2009 ah, okay, I understand. Thanks for both clarifications. Thanks all, you were a great help! Quote Link to comment https://forums.phpfreaks.com/topic/157111-solved-looping-back-in-a-while-statement/#findComment-827746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.