我使用codeigniter为客户建立租赁网站。我只是在没有租借期符合我的查询时才会返回true。基本上有一个带有开始日期和结束日期的租赁表。
我的客户从日期选择器中选择开始日期,结束日期是开始日期后的设定天数。
所以我想知道是否在这些日期租用了任何租赁物品以验证客户是否可以预订该物品。以下是我到目前为止所做的事情,我需要使用Codeigniters活动记录来实现这一目标......
function check_product($periodLength) {
//This is the start time they chose from the picker
$startTime = $_POST['date'];
//The period length is a variable of days, so the end date would be their chosen start date plus a certain amount of days...
$endTime = strtotime('+'.$periodLength.'day', $startTime);
$this->db->where('productsId', $_POST['productId']);
//This is where I need help!!! What query would give me records that are NOT between the start and end dates that i'm wanting
$query = $this->db->get('rentals');
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'id' => $row->id,
'customerId' => $row->customerId,
'productsId' => $row->productsId,
'periodStart' => $row->periodStart,
'periodEnd' => $row->periodEnd,
'status' => $row->status,
'specialInstructions' => $row->specialInstructions,
'adminNotes' => $row->adminNotes
);
}
return $data;
}
我的大部分问题都在我脑海中,我确定但我需要弄清楚我的开始日期是否已经保留。
答案 0 :(得分:2)
试试这个:
$startTime = $_POST['date'];
$endTime = strtotime('+'.$periodLength.'day', $startTime);
$this->db->where('productsId', $_POST['productId']);
$this->db->where(" $startTime NOT BETWEEN periodStart AND periodEnd AND $endTime NOT BETWEEN periodStart AND periodEnd OR ($startTime < periodStart AND $endTime > periodEnd) ");
//Since this is a complex where clause i would recommend
//to put enitre clause in single where statement rather than
//putting in different where statement.
//And it is upto codeigniter active record standards
$query = $this->db->get('rentals');