过滤几个选项php mysql

时间:2017-04-27 21:15:27

标签: php mysql sql input

我有一个度假旅游数据库,想要同时按3个参数(目的地国家,出发日期,酒店)对其进行排序。

所以我以html格式创建了三个输入(目的地,日期,酒店)

<form action="/search-tours" method="GET">
<input name="destination" type="text">
<input name="date" type="date">
<input name="hotel" type="text">
<button type=submit>Search tours</button>

我正在使用Laravel,所以我的php代码在控制器中(&#34; Tour&#34;是model =连接到表&#34; tour&#34;在DB中)。

        $destination = $request->get('destination');
        $date = $request->get('date');
        $hotel = $request->get('hotel');


        $tours = Tour::where('destination', '=', $destination)
                      ->where('departure', '=' , $date)
                      ->where('hotel', '=', $hotel)
                     ->get();

当我提交所有输入时,它会给出正确的结果,但是当缺少任何输入时,它根本不起作用。

我以为我可以通过If-else解决它,但后来我意识到可能有7种情况(1-参数存在,0 - 缺失: 对于前目的地(1) - 日期(0) - 酒店(0)= 100,总计: 111,100,010,001,110,011,101 - 我们不关心000)。如果我们有4个输入,它将是15,5个参数--31等。

因此写下这么多的if-else会很乏味,那就意味着还有另一种方法。

如果只给出1或2个输入,我应采取什么方法才能使其工作?

1 个答案:

答案 0 :(得分:2)

using (IAmazonS3 client = new AmazonS3Client())
{
  // do stuff
}