反正在参数的功能中添加条件了吗?

时间:2015-04-20 08:19:40

标签: php sql

我有一个网格,我需要有一些过滤器。 实际上我只有2个参数可用,但问题是我可能有5/6/7甚至更多,我需要一个很好的方法来管理我的json编码。

我通过这种方式管理它:

if(!empty($_GET['q']) && empty($_GET['s'])) {
        $client = $_GET['q'];

    $requeteDevis = myPDO::getInstance()->prepare(<<<SQL
            SELECT SC_libelle, ETP_libelle, DVS_numDevis, DVS_libelle, DVS_montant, DVS_statut, DVS_pourcent, DVS_montant,
            USR_Nom, USR_Prenom, CTC_Nom, CTC_Prenom, ctc.idContact, DVS_date
            FROM Utilisateur usr, Devis dvs, SiteClient sc, Client clt, Contact ctc, Entreprise etp
            WHERE usr.idEntreprise = etp.idEntreprise
            AND usr.idUtilisateur = dvs.idUtilisateur
            AND dvs.idSiteClient = sc.idSiteClient
            AND sc.idClient = clt.idClient
            AND dvs.idContact = ctc.idContact
            AND clt.CLT_libelle = ?
SQL
);


    $requeteDevis->execute(array($client));    
} else if (!empty($_GET['s']) && empty($_GET['q'])) {
$stat = $_GET['s'];

    $requeteDevis = myPDO::getInstance()->prepare(<<<SQL
            SELECT SC_libelle, ETP_libelle, DVS_numDevis, DVS_libelle, DVS_montant, DVS_statut, DVS_pourcent, DVS_montant,
            USR_Nom, USR_Prenom, CTC_Nom, CTC_Prenom, ctc.idContact, DVS_date
            FROM Utilisateur usr, Devis dvs, SiteClient sc, Client clt, Contact ctc, Entreprise etp
            WHERE usr.idEntreprise = etp.idEntreprise
            AND usr.idUtilisateur = dvs.idUtilisateur
            AND dvs.idSiteClient = sc.idSiteClient
            AND sc.idClient = clt.idClient
            AND dvs.idContact = ctc.idContact
            AND dvs.DVS_statut = ?
SQL
);



    $requeteDevis->execute(array($stat));
}
    else if ......

那么,你认为有没有办法只有一个sql语句?否则它将完成我将有50个:/

由于

2 个答案:

答案 0 :(得分:0)

$cond1 = '';
$cond2 = '';
$cond3 = '';
$var = 2;
if(1==2){
$cond1 = ' AND column1>20';
}
if(3==3){
$cond2 = ' AND column2 < '.$var;
}
$cond3 = ' ORDER BY id DESC';
$baseSQL = "SELECT  * FROM table WHERE 1=1 $cond1 $cond2 $cont3";

答案 1 :(得分:0)

我找到了如何管理它:

以下是代码:

if(empty($_GET['s'])) { 
            $state = null;
        } else $state = $_GET['s'];

        if(empty($_GET['q'])) {
            $client = null;
        } else $client = $_GET['q'];


        $requeteDevis = myPDO::getInstance()->prepare(<<<SQL
            SELECT SC_libelle, ETP_libelle, DVS_numDevis, DVS_libelle, DVS_montant, DVS_statut, DVS_pourcent, DVS_montant,
            USR_Nom, USR_Prenom, CTC_Nom, CTC_Prenom, ctc.idContact, DVS_date
            FROM Utilisateur usr, Devis dvs, SiteClient sc, Client clt, Contact ctc, Entreprise etp
            WHERE usr.idEntreprise = etp.idEntreprise
            AND usr.idUtilisateur = dvs.idUtilisateur
            AND dvs.idSiteClient = sc.idSiteClient
            AND sc.idClient = clt.idClient
            AND dvs.idContact = ctc.idContact
            AND (dvs.DVS_statut = :statut OR :statut is null)
            AND (clt.CLT_libelle = :client OR :client is null)
SQL
);  

        $requeteDevis->execute(array(":statut" => $state,
                                     ":client" => $client));

因此,只需一个请求,您就可以管理每个过滤器。谢谢你的帮助