Results 1 to 3 of 3

Thread: Need help with WHILE loop please

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Need help with WHILE loop please

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

    Code:
    $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

  2. #2
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    <?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

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear ricmetal:

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

    Code:
    $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
    Last edited by kuau; 03-10-2009 at 03:39 AM. Reason: figured it out

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •