I have a table that stores a start_date and a end_date for a given booking.
This allows me to search the homes and provide available bookings. Then I decided to use a calendar to show the user what days were booked on the items booking page. I decided use the AJAX Availability Calendar in the short run since I am short on time. However, this table for this calendar stores all dates instead of just start and end. So my thought is to just create a script that will populate the calendar booking date table based on the start_date and end_date. So basically I need to identify all dates in between including start and end.
I have this script below that does fill the dates into the calendar table. However I also need to INPUT in the item_id and the item_name along with the DATE in each row. I have tried modifying the SELECT statement to do this but I am just not getting it since it is using sprintf( function.
Here is the script I am using:
<?php
$db = array (
'host' => 'myhost',
'user' => 'mycalendar',
'pass' => 'mypass',
'dbname' => 'myDB'
);
if(!mysql_connect($db['host'], $db['user'], $db['pass']))
{
trigger_error('Connexion error: '.mysql_error());
}
elseif(!mysql_select_db($db['dbname']))
{
trigger_error('Error on selecting database '.mysql_error());
}
else
{
$sql = "SET SESSION sql_mode = 'ANSI,ONLY_FULL_GROUP_BY'";
if(!mysql_query($sql))
{
trigger_error('MySQL in ANSI niet mogelijk');
}
}
// sample data from user input:
$start_date = ('2011-08-21');
$end_date = ('2011-08-21');
// create values for each date:
$startTime = strtotime($start_date);
$endTime = strtotime($end_date);
$values = array();
for($time = $startTime; $time <= $endTime; $time = strtotime('+1 day', $time))
{
$thisDate = date('Y-m-d', $time);
$values[] = "('$thisDate')";
}
// build the actual query:
$sql = sprintf(
"INSERT INTO bookings (the_date), (id_state) VALUES\n%s",
implode(",\n", $values)
);
// show what query would look like:
echo "<pre>$sql</pre>";
/*
if(!$res = mysql_query($sql))
{
trigger_error(mysql_error().'<br />In query: '.$sql);
}
else
{
$id = mysql_insert_id();
}
*/
?>
fixed some parts, 1234 is the
id_stateand
ofcourse you can add other fields/data in similar fashion.