如果搜索为空,如何显示所有结果

时间:2014-03-23 09:30:10

标签: php mysql sql

我有以下代码,我尝试按国家搜索。在主页上有一个下拉框,用于选择国家/地区。我的问题是它显示了所选国家/地区的结果,但如果没有选择任何内容则没有结果。

如果没有选择,我希望它显示所有国家/地区的结果。

如果country_select为空,则在mysql搜索中使用此代码,则结果为零:

and bam.country='".mysql_real_escape_string(trim($_POST['country_select']))."'

以下是完整代码:

$searchsql = "SELECT pp_id, appmt_id, price, max( stay_from )
                        FROM (
                        SELECT bp.pp_id, bam.appmt_id, bam.appmt_name, bp.price, bp.stay_from
                        FROM bsi_apartment_master AS bam, bsi_appmt_features AS baf, bsi_priceplan AS bp
                        WHERE bam.status=true and baf.appmt_id = bam.appmt_id".$addquery." AND baf.bedroom >=".$this->bedroom." AND baf.bathroom >=".$this->bathroom."
                        and bam.country='".mysql_real_escape_string(trim($_POST['country_select']))."'
                        AND bam.appmt_id NOT IN (SELECT appmt_id FROM bsi_bookings WHERE is_deleted = FALSE AND (( '".$this->mysqlCheckInDate."'
                        BETWEEN checkin_date AND checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) )
                        OR ( DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) BETWEEN checkin_date AND checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) )
                        OR ( checkin_date BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) )
                        OR ( checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) )))
                        AND bam.appmt_id = bp.appmt_id
                        AND if( ( ".$staycount." NOT BETWEEN bp.stay_from AND bp.stay_to ) = true, 
                        ( ".$staycount." NOT BETWEEN bp.stay_from AND bp.stay_to ) AND bp.default_pp !=0, 
                        ( ".$staycount." BETWEEN bp.stay_from AND bp.stay_to ) )
                        order by bp.stay_from desc
                        ) AS t1
                        GROUP BY appmt_id
                        order by price ".$this->sorting;

5 个答案:

答案 0 :(得分:2)

$searchsql = "SELECT pp_id, appmt_id, price, max( stay_from )
                    FROM (
                    SELECT bp.pp_id, bam.appmt_id, bam.appmt_name, bp.price, bp.stay_from
                    FROM bsi_apartment_master AS bam, bsi_appmt_features AS baf, bsi_priceplan AS bp
                    WHERE bam.status=true and baf.appmt_id = bam.appmt_id".$addquery." AND baf.bedroom >=".$this->bedroom." AND baf.bathroom >=".$this->bathroom;

  if(!empty($_POST['country_select']))
  {
     $searchsql .=" and bam.country='".mysql_real_escape_string(trim($_POST['country_select']))."'";
   }
                    $searchsql.="AND bam.appmt_id NOT IN (SELECT appmt_id FROM bsi_bookings WHERE is_deleted = FALSE AND (( '".$this->mysqlCheckInDate."'
                    BETWEEN checkin_date AND checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) )
                    OR ( DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) BETWEEN checkin_date AND checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) )
                    OR ( checkin_date BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) )
                    OR ( checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) )))
                    AND bam.appmt_id = bp.appmt_id
                    AND if( ( ".$staycount." NOT BETWEEN bp.stay_from AND bp.stay_to ) = true, 
                    ( ".$staycount." NOT BETWEEN bp.stay_from AND bp.stay_to ) AND bp.default_pp !=0, 
                    ( ".$staycount." BETWEEN bp.stay_from AND bp.stay_to ) )
                    order by bp.stay_from desc
                    ) AS t1
                    GROUP BY appmt_id
                    order by price ".$this->sorting;

答案 1 :(得分:1)

在搜索参数

之前和之后,使用like符号添加=而不是%

and bam.country like '%".mysql_real_escape_string(trim($_POST['country_select']))."%'

答案 2 :(得分:1)

您可以将WHERE子句设置为值,或者为空,如:

AND (bam.country = $country_value OR $country_value = '')

这样可以使得不设置值将返回所有可能的值。但是,如果您的国家/地区的行实际为空白,则无法仅过滤那些行。

答案 3 :(得分:1)

您可以使用以下代码

$country_select  = $_POST['country_select'];

//To intialize search condition
$search_condition = '';

//Search condition for selected country
$search_condition .= $country_select!=""?"bam.country = '$country_select'":"";

$searchsql = "SELECT pp_id, appmt_id, price, max( stay_from )
                        FROM (
                        SELECT bp.pp_id, bam.appmt_id, bam.appmt_name, bp.price, bp.stay_from
                        FROM bsi_apartment_master AS bam, bsi_appmt_features AS baf, bsi_priceplan AS bp
                        WHERE bam.status=true and baf.appmt_id = bam.appmt_id".$addquery." AND baf.bedroom >=".$this->bedroom." AND baf.bathroom >=".$this->bathroom." $search_condition
                        AND bam.appmt_id NOT IN (SELECT appmt_id FROM bsi_bookings WHERE is_deleted = FALSE AND (( '".$this->mysqlCheckInDate."'
                        BETWEEN checkin_date AND checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) )
                        OR ( DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) BETWEEN checkin_date AND checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) )
                        OR ( checkin_date BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) )
                        OR ( checkin_date AND DATE_SUB(checkout_date, INTERVAL 1 DAY) BETWEEN '".$this->mysqlCheckInDate."' AND DATE_SUB('".$this->mysqlCheckOutDate."', INTERVAL 1 DAY) )))
                        AND bam.appmt_id = bp.appmt_id
                        AND if( ( ".$staycount." NOT BETWEEN bp.stay_from AND bp.stay_to ) = true, 
                        ( ".$staycount." NOT BETWEEN bp.stay_from AND bp.stay_to ) AND bp.default_pp !=0, 
                        ( ".$staycount." BETWEEN bp.stay_from AND bp.stay_to ) )
                        order by bp.stay_from desc
                        ) AS t1
                        GROUP BY appmt_id
                        order by price ".$this->sorting;

答案 4 :(得分:1)

如果没有国家/地区选择,则country_select中的$_POST密钥为空?那么如何将查询的那部分保留在外面呢。所以你的巨大查询看起来像

query = "";
queryAllCountries = "SELECT FOO, BAR FROM TABLE WHERE FOO=SOMETHING"; 
if ($_POST[country_select] != null) {
    query = queryAllCountries + " AND COUNTRY = $_POST[country_select]";
} else {
    query = queryAllCountries;
}

mysql_query(query)

如果country_select参数是POSTED,那么你可以附加WHERE子句 ONLY

ps:自从我触及PHPMySQL以来,它已经很久了,所以语法可能有误。请原谅那一部分。我希望你能得到逻辑吗?