如何在PHP中正确选择?

时间:2019-05-23 07:51:34

标签: php mysql sql

我有以下问题:有一个巨大的查询,它将一组用户可选择的条件(选择框,文本字段等)连接起来。 默认情况下,如果仍然未选择任何内容(没有条件),则根据各种参数进行排序(排序依据)。问题是,如果有条件,我们只能添加单词“ WHERE”,并且只能添加一次,但是如果没有条件,则不应添加单词,因为在WHERE ORDER BY之后无法写入。如何解决这个问题?

$payment_select = "select payment_id,
        payment_agreement,
                payment_dateagreement,
                payment_action,
                payment_close,
                payment_charge,
                payment_pay,
                study_id,
                card_id,
                name_nominative,
                surname_nominative,
                patr_nominative,
                studgroups_number,
                dep_name,
                study_kurs`

        from
                study
        inner join card on card_id = study_card_id
        inner join name on name_id = card_name_id
        inner join surname on surname_id = card_surname_id
        inner join dep on dep_id = study_dep_id
        inner join studgroups on study_studgroups_id = studgroups_id
        left join payment on study_id = payment_study_id
        left join patr on patr_id = card_patr_id
        ";

        if (isset($name_filter) && ($name_filter)) {
            $payment_select. = " and name_nominative like '%".$name_filter. "%' ";
        }
        if (isset($surname_filter) && ($surname_filter)) {
            $payment_select. = " and surname_nominative like '%".$surname_filter. "%' ";
        }
        if (isset($patr_filter) && ($patr_filter)) {
            $payment_select. = " and patr_nominative like '%".$patr. "%' ";
        }
        if (isset($group_filter) && ($group_filter)) {
            $payment_select. = " and studgroups_number like '%".$group_filter. "%' ";
        }
        if (isset($agreement_filter) && ($agreement_filter)) {
            $payment_select. = " and payment_agreement like '%".$agreement_filter. "%' ";
        }
        if (isset($debt_filter) && ($debt_filter == 1)) {
            $payment_select. = " and (payment_charge - payment_pay) > 0 ";
        }
        if (isset($debt_filter) && ($debt_filter == 2)) {
            $payment_select. = " and (payment_charge - payment_pay) <= 0 ";
        }
        if (isset($card_filter) && $card_filter) {
            $payment_select. = " and card_id = '$card_filter' ";
        }
        if (isset($study_filter) && $study_filter) {
            $payment_select. = " and study_id = '$study_filter' ";
        }
        if (isset($recordbook_filter) and $recordbook_filter){
            $payment_select. = " and study_recordbook like '$recordbook_filter%' ";
        }
        if (isset($action_filter) && ($action_filter == 1)) {
            $payment_select. = " and payment_action = 1 ";
        }
        if (isset($action_filter) && ($action_filter == 2)) {
            $payment_select. = " and payment_action = 0 ";
        }
        if (isset($close_filter) && ($close_filter == 1)) {
            $payment_select. = " and payment_close = 0 ";
        }
        if (isset($close_filter) && ($close_filter == 2)) {
            $payment_select. = " and payment_close = 1 ";
        }

        if (isset($dep_filter) && ($dep_filter)) {
            $dep = select_dep_array();
            $payment_select. = " and dep_acronym like '".$dep[$dep_filter]. "' ";
        }
        if (isset($kurs_filter) && ($kurs_filter > 1)) {
            $payment_select. = " and study_kurs = ". ($kurs_filter - 1). " ";
        }
        if (isset($educform_filter) && ($educform_filter >= 1)) {
            $payment_select. = " and study_formeduc_id = ".$educform_filter. " ";
        }
        if (isset($progr_filter) && ($progr_filter >= 1)) {
            $payment_select. = " and study_program_id = ".$progr_filter. " ";
        }
        if (preg_match("/^\d(\d)?\.\d(\d)?\.\d\d\d\d$/", $date_from_filter)) {
            $date_from_mysql = date_user_to_mysql($date_from_filter);
        } else {
            if ($date_from_filter)
                $error_msg = "Дата введена неправильно.";
        }
        if (preg_match("/^\d(\d)?\.\d(\d)?\.\d\d\d\d$/", $date_to_filter)) {
            $date_to_mysql = date_user_to_mysql($date_to_filter);
        } else {
            if ($date_to_filter)
                $error_msg = "Дата введена неправильно.";
        }
        if ((isset($date_from_mysql) && ($date_from_mysql)) && !(isset($date_to_mysql) && ($date_to_mysql))) {
            $payment_select. = "and payment_date_agreement >= '".$date_from_mysql. "' ";
        }
        if (!(isset($date_from_mysql) && ($date_from_mysql)) && (isset($date_to_mysql) && ($date_to_mysql))) {
            $payment_select. = "and payment_dateagreement <= '".$date_to_mysql. "' ";
        }

        if ((isset($date_from_mysql) && ($date_from_mysql)) && (isset($date_to_mysql) && ($date_to_mysql))) {
            $payment_select. = "and payment_dateagreement >= '".$date_from_mysql.
            "' and payment_dateagreement <= '".$date_to_mysql. "' ";
        }

        if (isset($order) && ($order)) {
            if ($order == 3) {
                if ($desc) {
                    $payment_select. = " order by surname_nominative desc, name_nominative desc, patr_nominative desc";
                } else {
                    $payment_select. = " order by ".$order_array[$order];
                }

            } else {
                $payment_select. = " order by ".$order_array[$order];
                if ($desc) {
                    $payment_select. = " desc ";
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

您可以直接在数组中收集所有“ where子句”,然后检查它是否不为空,然后将其内爆,而不是直接扩展SQL。像这样:

//your select
$payment_select = "SELECT ....";

//helper var
$where_clauses = [];

//from your example
if (isset($name_filter) && ($name_filter)) {
    $where_clauses[] = "name_nominative like '%".$name_filter. "%'";
}

//from your example
if (isset($surname_filter) && ($surname_filter)) {
    $where_clauses[] = "surname_nominative like '%".$surname_filter. "%'";
}

//now append the clauses if there any
if (! empty($where_clauses)) {
    $payment_select .= " WHERE " . implode(" AND ", $where_clauses)
}