Jump to content

Looping query is nasty. How can i remove this query from this loop?


phpnerd

Recommended Posts

Hello;

 

I know! But how can I get the same results in a proper way without having to loop the query?

 

<?php

function createDatesArray($days)

{

    $output = array();

    $month = date("m");

    $day = date("d");

    $year = date("Y");

    for($i=1; $i<=$days; $i++)

        {

            $output[] = date('Y-m-d',mktime(0,0,0,$month,($day+$i),$year));

        }

    return $output;

}

 

$dates = createDatesArray("365");

 

foreach($dates as $d)

    {

        echo $query = "INSERT INTO DATES (day) VALUES ('".$d."')";

        echo $d."<br />";

    }

?>

You would form a multi-value insert query - INSERT INTO your_table (column_name) VALUES ('value1'),('value2'),('value3'),...,('valuen')

 

You can form the value1'),('value2'),('value3'),...,('valuen by imploding the array with '),(' between each value.

Archived

This topic is now archived and is closed to further replies.

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