Jump to content

[SOLVED] looping back in a while statement


Walker33

Recommended Posts

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.

Link to comment
Share on other sites

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 "";
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

!$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.