Jump to content

i want to loop through while condition is valid(DO-WHILE)


Recommended Posts

Good day, please i want to update all that  the time has elapsed a table (merged). I want the script to check for all that the current time is equals or greater than a fixed time. and update the colom (receiver). This i want the script to do see my code i tried using do-while but is not working

<?php
include "../../include/db.php";
$mergestatus="no";
$awaiting='awaiting';
$exp_to_donate='1';
$exp_tobe_paid='2';
$paidto_redonate='3';
$redonated_exp_balance='4';
//adding time db  just to get time
$new = $flash->query("SELECT * FROM `user` WHERE merged='awaiting' AND active='yes'");
if($new->rowCount() > 0){
	echo "see";
$newrecord = $new->fetch();
$newsender=$newrecord['email'];
echo $newsender;
//adding time db  just to get time
$timeget = $flash->query("SELECT * FROM `merge` WHERE status='0' AND sender='$newsender'");
if($timeget->rowCount() > 0){
	

$xxx = $timeget->fetch();
$timeget_xtime=strtotime($xxx['xtime']);

$currentTimestamp = strtotime(date('Y-m-d H:i:s'));
$payment_time= date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +3 hours"));//adding 3hrs to the current time	

if($currentTimestamp >=$timeget_xtime )
{ 


	//checking for who to pay
$placecheck = $flash->query("SELECT * FROM `user` WHERE `active`='yes' AND `right`='0' AND `merged`='no' AND (`level`='$exp_tobe_paid' OR `level`='$redonated_exp_balance') LIMIT 1");	
if($placecheck->rowCount() > 0){
	
$ddd = $placecheck->fetch();
	$togethelp=$ddd['email'];
$gonow = $flash->query("UPDATE `merge` SET `receiver`='$togethelp',`payment_time`='$payment_time'  WHERE `sender`='$newsender'");	
	$up= $flash->query("UPDATE `user` SET merged='yes' WHERE email IN ('$newsender','$togethelp')");
		
}//if there is someone active and not admin
	
else{ 	
$placeadmin = $flash->query("SELECT * FROM `user` WHERE `right`='1' LIMIT 1");
	
	$profinadmin = $placeadmin->fetch();
$togethelpadmin=$profinadmin['email'];//sponsor email
$payment_time= date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +3 hours"));//adding 3hrs to the current time		
$gonowadmin = $flash->query("UPDATE `merge` SET `receiver`='$togethelpadmin',`payment_time`='$payment_time'  WHERE `sender`='$newsender'");

$upadmin= $flash->query("UPDATE `user` SET merged='yes'  WHERE (`email`='$newsender')");
}	
	
}//if current time is above 

do{
   
    //checking for who to pay
$placecheck = $flash->query("SELECT * FROM `user` WHERE `active`='yes' AND `right`='0' AND `merged`='no' AND (`level`='$exp_tobe_paid' OR `level`='$redonated_exp_balance') LIMIT 1");	
if($placecheck->rowCount() > 0){
	
$ddd = $placecheck->fetch();
	$togethelp=$ddd['email'];
$gonow = $flash->query("UPDATE `merge` SET `receiver`='$togethelp',`payment_time`='$payment_time'  WHERE `sender`='$newsender'");	
	$up= $flash->query("UPDATE `user` SET merged='yes' WHERE email IN ('$newsender','$togethelp')");
		
}//if there is someone active and not admin
	
else{ 	
$placeadmin = $flash->query("SELECT * FROM `user` WHERE `right`='1' LIMIT 1");
	
	$profinadmin = $placeadmin->fetch();
$togethelpadmin=$profinadmin['email'];//sponsor email
$payment_time= date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s")." +3 hours"));//adding 3hrs to the current time		
$gonowadmin = $flash->query("UPDATE `merge` SET `receiver`='$togethelpadmin',`payment_time`='$payment_time'  WHERE `sender`='$newsender'");

$upadmin= $flash->query("UPDATE `user` SET merged='yes'  WHERE (`email`='$newsender')");
}	
}
while($currentTimestamp >=$timeget_xtime && ($timeget->rowCount() > 0));



}//if status is 0 in merge table
		}// if merge is awaiting in user table
//end of assign sponsor to user
 ?>

 

Link to post
Share on other sites

I don't quite understand what you're trying to do, or what your code is actually doing (saying it's "not working" doesn't explain much), but a do/while loop will loop 1 or more times, it always runs at least once.  A regular while loop will loop 0 or more times, it might not loop at all if the condition isn't true.  Is that what you want, a loop that runs 1 or more times?

Link to post
Share on other sites
43 minutes ago, justsomeguy said:

I don't quite understand what you're trying to do, or what your code is actually doing (saying it's "not working" doesn't explain much), but a do/while loop will loop 1 or more times, it always runs at least once.  A regular while loop will loop 0 or more times, it might not loop at all if the condition isn't true.  Is that what you want, a loop that runs 1 or more times?

i want a loop that will update all the rows in a table that met a certain condition.

Link to post
Share on other sites

Is the rest of that code working?  For example, with this:

if($new->rowCount() > 0){
    echo "see";

Does that get printed?  I'm pretty sure we went over in another thread how rowCount does not work with select queries on all databases, and you should not use rowCount to determine if a select query returned any records.  It says that right in the documentation for rowCount too:

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action.

The other problem is that your loop will never end.  You have this condition:

while($currentTimestamp >=$timeget_xtime && ($timeget->rowCount() > 0))

But inside the loop you do not change $currentTimestamp, $timeget_xtime, or $timeget, so if that condition is true then the loop will never end.

Link to post
Share on other sites
6 hours ago, justsomeguy said:

Is the rest of that code working?  For example, with this:

 


if($new->rowCount() > 0){
    echo "see";

 

Does that get printed?  I'm pretty sure we went over in another thread how rowCount does not work with select queries on all databases, and you should not use rowCount to determine if a select query returned any records.  It says that right in the documentation for rowCount too:

 

 

The other problem is that your loop will never end.  You have this condition:

 


while($currentTimestamp >=$timeget_xtime && ($timeget->rowCount() > 0))

 

But inside the loop you do not change $currentTimestamp, $timeget_xtime, or $timeget, so if that condition is true then the loop will never end.

the code is working fine.and printing see, the problem is getting the code to stop running after doing the job

Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...