Log in

View Full Version : Need help with WHILE loop please



kuau
03-10-2009, 12:29 AM
I inherited a website where for the past 4 years every time someone did a price check, it would insert a new record into the 'checks' table with the date and time. This resulted in hundreds of records per day. I have written new code that results in only one record per day that holds the total checks for that day.

I need to write a little one-time script that will "balance forward" the existing data. Basically it needs to do this for each day from 2005-8-19 to the present...


$sql = "SELECT * FROM checks WHERE Ch_Date = '2005-08-19'";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$Price_Checks = mysql_num_rows($result);
$sql = "INSERT INTO newtable ( `checkdate`, `checks` ) VALUES ( '$checkdate', '$Price_Checks' ) ";
$result2 = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");

I don't know how to make the while loop that will increment the day by 1 so it will go through all of the dates. I know this must be simple, but counters were never my forte. Any assistance would be greatly appreciated.

Mahalo, erin :)

ricmetal
03-10-2009, 02:09 AM
<?php
$dtStart = "2009-03-25";
$dtEnd = "2009-04-06";

$tmStart = strtotime($dtStart);
$tmEnd = strtotime($dtEnd);

for($i=$tmStart;$i<=$tmEnd;$i=$i+86400) {
echo date("d", $i)."<br>";
}


?>

code printed on anther forum raltive to gettin dates
i hope it'll come in handy
or at least its a starting point
or check
http://pt2.php.net/strtotime

kuau
03-10-2009, 03:17 AM
Dear ricmetal:

OK, this is the code I used based on yours above:


$dtStart = "2005-08-19";
$dtEnd = "2009-03-09";

$tmStart = strtotime($dtStart);
$tmEnd = strtotime($dtEnd);

for($i=$tmStart; $i<=$tmEnd; $i=$i+86400) {
echo date("Y-m-d", $i)."<br>";
$cdate = date('Y-m-d',$i);
$sql = "SELECT * FROM checks WHERE Ch_Date = '$cdate'";
$result = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
$Price_Checks = mysql_num_rows($result);

$sql = "INSERT INTO checktots ( `cdate`, `checks` ) VALUES ( '$cdate', '$Price_Checks' ) ";
$result2 = mysql_query($sql,$connection) or die("Couldn't execute $sql query.");
}

It worked... yahoo! Thanks very much, e :)