我想要以下功能来选择住宿在某个
$minvalue
和 $maxvalue
之间的酒店。最好的方法是什么?
function gethotels($state_id,$city,$accommodation,$minvalue,$maxvalue,$limit,$pgoffset)
{
$this->db->limit($limit, $pgoffset);
$this->db->order_by("id", "desc");
$this->db->where('state_id',$state_id);
$this->db->where('city',$city);
// This one should become a between selector
$this->db->where($accommodation,$minvalue);
$result_hotels = $this->db->get('hotels');
return $result_hotels->result();
}
你应该使用
$this->db->where('$accommodation >=', $minvalue);
$this->db->where('$accommodation <=', $maxvalue);
我不确定语法,所以如果不正确,请原谅。
无论如何
BETWEEN
是使用 >=min && <=max.看着这个链接我想你可以写:
$this->db->where("$accommodation BETWEEN '$minvalue' AND '$maxvalue'");
在 Codeigniter 中,这是检查两个日期记录之间的简单方法...
$start_date='2016-01-01';
$end_date='2016-01-31';
$this->db->where('date BETWEEN "'. date('Y-m-d', strtotime($start_date)). '" and "'. date('Y-m-d', strtotime($end_date)).'"');
听起来正确,但执行此查询时可能会出现一些问题: 我建议:
$this->db->where( "$accommodation BETWEEN $minvalue AND $maxvalue", NULL, FALSE );
检查以下代码。它对我有用
$dateStart = $this->input->post('dateStart);
$dateTo= $this->input->post('dateTo);
$fromDate=date('Y-m-d',strtotime($dateStart));
$toDate=date('Y-m-d',strtotime($dateTo));
$this->db->from('tbl_sales sl');
$this->db->where('DATE(sl.created) >=',$setDate);
$this->db->where('DATE(sl.created) <=',$dateend);
$this->db->get();
// Codeigniter 3.1.11
public function getContraMarking($from_date2='',$to_date2='',$acctno2='')
{
if($from_date2!='' && $to_date2!='' && $acctno2!='')
{
$this->db->select('account_no,txn_date,SUM(pd_amt) AS debit,SUM(cr_amt) AS credit,COUNT(value_date) AS total_count');
$this->db->from('ut_sbi_reco_paid_master');
$this->db->where('account_no', $acctno2);
$this->db->where('txn_date BETWEEN "'. $from_date2. '" AND "'. $to_date2. '" ');
$this->db->group_by('account_no,txn_date,pd_amt');
$this->db->order_by("txn_date");
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->row();
}else{
return FALSE;
}
}
}
您可以将所有 where 条件打包为
get_where()
方法调用的第二个参数中的关联元素。
大于或等于和小于或等于运算符只需附加到键字符串,如下所示。
当对象也可能时,我不赞成有条件地返回
false
。 PHP 长期以来一直支持可空类型,并专门提供空合并操作。因为 row()
返回一个对象或 null
,所以我建议所有返回 row()
数据的模型方法应该返回 null
或在处理过程中发生“不好”的情况时抛出异常。
public function getContraMarking(
string $from_date2 = '',
string $to_date2 = '',
string $acctno2 = ''
): ?object {
if (!$from_date2 || !$to_date2 || !$acctno2) {
return null;
}
return $this->db
->select([
'account_no',
'txn_date',
'SUM(pd_amt) debit',
'SUM(cr_amt) credit',
'COUNT(value_date) total_count'
])
->group_by('account_no, txn_date, pd_amt')
->order_by('txn_date')
->get_where('ut_sbi_reco_paid_master', [
'account_no' => $acctno2,
'txn_date >=' => $from_date2,
'txn_date <=' => $to_date2
])
->result();
}
}
您可能还会遇到错误消息。 “操作数类型冲突:日期与 int 不兼容。
在日期周围使用单引号。例如:
$this->db->where("$accommodation BETWEEN '$minvalue' AND '$maxvalue'");
我想我们可以这样写:
$this->db->where('accommodation >=', minvalue);
$this->db->where('accommodation <=', maxvalue);
没有美元($)符号