I have a table containing the availability of rooms
the table looks something like this (sorry for the snippet)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>availability</title> <meta name="GENERATOR" content="HeidiSQL 9.1.0.4936"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> thead tr {background-color: ActiveCaption; color: CaptionText;} th, td {vertical-align: top; font-family: "Tahoma", Arial, Helvetica, sans-serif; font-size: 8pt; padding: 3px; } table, td {border: 1px solid silver;} table {border-collapse: collapse;} thead .col0 {width: 106px;} .col0 {text-align: right;} thead .col1 {width: 80px;} .col1 {text-align: right;} thead .col2 {width: 79px;} .col2 {text-align: right;} thead .col3 {width: 80px;} .col3 {text-align: right;} thead .col4 {width: 91px;} thead .col5 {width: 51px;} .col5 {text-align: right;} thead .col6 {width: 113px;} .col6 {text-align: right;} thead .col7 {width: 113px;} .col7 {text-align: right;} thead .col8 {width: 78px;} .col8 {text-align: right;} thead .col9 {width: 79px;} .col9 {text-align: right;} </style> </head> <body> <p style="font-family: monospace; white-space: pre;">SELECT * FROM `availability` WHERE today between '2015-04-10' and '2015-04-18' and idroom = 28 LIMIT 1000</p> <table caption="jos_bookitavailability (9 rows)"> <thead> <tr> <th class="col0">idavailability</th> <th class="col1">idoffer</th> <th class="col2">idbook</th> <th class="col3">idroom</th> <th class="col4">today</th> <th class="col5">price</th> <th class="col6">price_deviation_1</th> <th class="col7">price_deviation_2</th> <th class="col8">availability</th> <th class="col9">idcategory</th> </tr> </thead> <tbody> <tr> <td class="col0">149631</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-18</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149630</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-17</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149629</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-16</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149628</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-15</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149627</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-14</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149626</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-13</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149625</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-12</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149624</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-11</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> <tr> <td class="col0">149623</td> <td class="col1">0</td> <td class="col2">0</td> <td class="col3">28</td> <td class="col4">2015-04-10</td> <td class="col5">0</td> <td class="col6"></td> <td class="col7"></td> <td class="col8">0</td> <td class="col9">1</td> </tr> </tbody> </table> <p> <em>generated 2015-04-06 13:09:39 by <a href="http://www.heidisql.com/">HeidiSQL 9.1.0.4936</a></em> </p> </body> </html>
What i want to do is: when i have a new booking, change the field 'idbook' to the booking id and the availability to 1 for each row depending on dates the book (see: today field) and idroom.
In the model i have this code (mind the "debugging")
function updateAvailability($data)
{
$this->db->select('idbook');
$this->db->where('idguests',$data['guestid']);
$this->db->where('valid_from',$data['from_date']);
$this->db->where('valid_to',$data['to_date']);
$this->db->where('idroom',$data['idroom']);
$query = $this->db->get('bookitbooking');
$result = $query->row();
$idbook = $result->idbook;
$changes = array(
'idbook' => $idbook,
'availability' => 1
);
$where = array(
'today' => "BETWEEN '{$data['from_date']}' AND '{$data['to_date']}'",
'idroom' => $data['idroom']
);
$update = $this->db->update('bookitavailability',$changes,$where);
if( $update === TRUE){
echo "done";
} else {
echo "wrong";
}
}
For a reason, the above code doesnt work, i have a "done" return echoed, but no changes in my DB.
Try to double check this string:
"BETWEEN '{$data['from_date']}' AND '{$data['to_date']}'"
to
"BETWEEN ".$data['from_date']." AND ".$data['to_date']
The Problem may be $data['from_date'] is a string so it may not be upadting the correct row.
try
"BETWEEN DATE(".$data['from_date'].") AND DATE(".$data['to_date'] . ")"
Ok... So since i saw there was an issue with the where clause not functioning as i wanted and hoped, i've made some changes to my code.
First, i've made a foreach() loop for the dates and then i passed in each loop, the dates i wanted for the where statement. Here's how i "solved" it.
function updateAvailability($data)
{
$this->db->select('idbook');
$this->db->where('idguests',$data['guestid']);
$this->db->where('valid_from',$data['from_date']);
$this->db->where('valid_to',$data['to_date']);
$this->db->where('idroom',$data['idroom']);
$query = $this->db->get('bookitbooking');
$result = $query->row();
$idbook = $result->idbook;
$changes = array(
'idbook' => $idbook,
'availability' => 1
);
$arrival = new DateTime($data['from_date']);
$departure = new DateTime($data['to_date']);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($arrival, $interval, $departure);
foreach($period as $day){
$this->db->where('idroom',$data['idroom']);
$this->db->where('today',$day->format('Y-m-d'));
$this->db->update('bookitavailability',$changes);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.