我正在编写一个旅行社网站。我有hotels
在数据库中有一个列称为stars
这是一个整数,它的值是1到5之间。
我有一个表单,用户可以根据他们的stars
搜索酒店。字段是checkbox
,因此他可以一起搜索five stars
和four stars
酒店。
我读取stars array
并使用以下代码将访问者重定向到搜索页面:
$stars_param = "";
$i = 1;
foreach($stars as $star){
if($i == 1){
$stars_param .= $star;
}else{
$stars_param .= "&".$star;
}
$i++;
}
$parameters = "filter=true&&stars=".$stars_param."&&price=".$price."&&area=".$area;
return 'Redirect::to('/hotels-search/?'.$parameters);
url是这样的:
hotels-search?filter=true&&stars=5&1&&price=300&&area=taksim
在hotels-search
页面中,我读取了$_GET
,并爆炸了stars变量并编写了这个查询来获取数据:
$stars = $_GET['stars'];
$stars_array = explode('&', $stars);
$price = $_GET['price'];
$area = $_GET['area'];
$this['hotels'] = 'Lilessam'Hotels'Models'Hotel::orderBy('id', 'desc')->where(function($query) use($stars_array){
$i = 1;
foreach($stars_array as $star){
if($i == 1){
$query->where('stars', $star);
}else{
$query->orWhere('stars', $star);
}
$i++;
}
})->where('price', '<=', $price)->whereHas('area', function($query) use($area){
$query->where('name', 'LIKE', $area);
})->paginate(5);
但是如果我搜索5星和1星的酒店,我只能得到5星的酒店!
如果我搜索4星和3星的酒店,我只得到4星的酒店!其他时候我从数据库中什么也得不到!!
我怎么能让查询,所以我可以得到1或3或5星级的酒店在同一时间?
为什么不用这个
$this['hotels'] = 'Lilessam'Hotels'Models'Hotel::orderBy('id', 'desc')
->whereIn('start',$stars_array)
->where('price', '<=', $price)
->whereHas('area', function($query) use($area){
$query->where('name', 'LIKE', $area);
})->paginate(5);
get变量由单个&
分隔,而不是&&
。每个名称-值对的形式为:- name=value。
试试这个。
$stars_param = implode(",",$stars);
$parameters = "filter=true&stars=".$stars_param."&price=".$price."&area=".$area;
return 'Redirect::to('/hotels-search/?'.$parameters);
那么你的URL将是:
hotels-search?filter=true&stars=5,1&price=300&area=taksim
在酒店搜索页面
$stars = $_GET['stars'];
$stars_array = explode(',', $stars);
$price = $_GET['price'];
$area = $_GET['area'];