PHP|Débutant :: Forums

Advertisement

Besoin d'aide ? N'hésitez pas, mais respectez les règles

Vous n'êtes pas identifié(e).

#26 Re : MySQL, PostgreSQL, etc... » [Résolu] Recherche complexe » 11-06-2009 11:14:29

Maljuna Kris a écrit :

Est-ce que l'internaute peut choisir la même option dans $_POST['critere1'] et $_POST['critere2'] ? (même si ce ne serait pas logique)
Et est-ce que ces options peuvent être toutes les deux 'default' ? (ce qui correspondrait à un non-choix, de fait)

il peut oui choisir le même critère (et ainsi avoir 6 possibilité de valeur)
il doit faire au moins un choix oui (le second pouvant rester sur default, si le premier en est différent)

#27 Re : MySQL, PostgreSQL, etc... » [Résolu] Recherche complexe » 11-06-2009 11:14:29

affiche_option rempli les options des select a partir de la base ^^


/********************************************/
/* fonction affiche_option                  */
/********************************************/
function affiche_option($maTable, $option)
{
    $result = recup_listebox($maTable);
    echo "<option value='default'>Choisissez...</option>";
    foreach ($result as $value) {
        list($id, $lib) = explode('|', $value);
        if ($option == $id) {
?>
    <option value="
    <?php
            echo $id;
?>
    " selected>
    <?php
            echo $lib;
?>
    </option>
    <?php
        } else {
?>
    <option value="
    <?php
            echo $id;
?>
    ">
    <?php
            echo $lib;
?>
    </option>
    <?php
        }
    }
}
/********************************************/
 

et du coup le code de recup_listbox :


/********************************************/
/* fonction recup_listebox                  */
/********************************************/
function recup_listebox($maTable)
{
    // on defini link pour pourvoir le reutiliser
    global $link;
    // on construit la requete
    $req = "SELECT * FROM ".$maTable.";";
    // on execute la requetes
    $result = mysql_query($req);
    //on parcour les resultats pour renvoyer le tableau
    while ($row = mysql_fetch_assoc($result)) {
        $tabResultats[] = $row[$maTable."_id"]."|".$row[$maTable."_libelle"];
    }
    return $tabResultats;
}
/********************************************/
 

#28 Re : MySQL, PostgreSQL, etc... » [Résolu] Recherche complexe » 11-06-2009 11:14:29

merci pour ton aide.

effectivement, il y a de l'ajax ^^.


et merci pour tes liens, je sais que l'affichage par tableau c'est moche, mais je dois faire au plus simple, l'application doit être optimisée pour IE6...
les autres sont de bons conseils aussi, je vais me les garder sous le coudes ^^

#29 Re : MySQL, PostgreSQL, etc... » [Résolu] Recherche complexe » 11-06-2009 11:14:29

pas de problème, c'est moi qui n'est pas non plus trés réactif je dois dire ^^



<form action="index.php?action=rechercher&valeurs=resultats" method="post" name="fomulaire_recherher_complexe">
<?php
//critere 1
?>
<table class="tabbordless" width="600" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td>
  Pour une recherche complexe, veuillez selectionner les critères<br /><br />
<table height="160" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td width="15%">Premier critère : </td>
    <td width="2%"><select id="critere1" name="critere1" onChange="afficheCritere('form_critere','critere1');">
  <option value="default">Choisissez...</option>
  <option value="statuts">Statuts</option>
  <option value="nom">Nom/Prenom</option>
  <option value="processus">Processus Cible</option>
  </select></td>
    <td colspan="2" rowspan="5">
  <span id="boitecritere1">
   
    </span></td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
</table>
<?php
//critere 2
?>
<br />
<table height="160" border="0" cellspacing="0" cellpadding="5">
  <tr>
    <td width="15%">Second critère : </td>
    <td width="2%"><select id="critere2" name="critere2" onChange="afficheCritere('form_critere','critere2');">
  <option value="default">Choisissez...</option>
  <option value="statuts">Statuts</option>
  <option value="nom">Nom/Prenom</option>
  <option value="processus">Processus Cible</option>
  </select></td>
    <td colspan="2" rowspan="5">
  <span id="boitecritere2">
   
    </span></td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
  <tr>
    <td colspan="2"> </td>
  </tr>
</table>
<div align="right"><span id="alerte_rechercher_complexe" style="color:#FF0000"></span><input onclick="valideRecherche(fomulaire_recherher_complexe);" name="btn_valider" type="button" value="Valider" /></div>
  </td>
  </tr>
</table>

</form>
 

voila pour la partie "html"

et voila pour le renvoi ajax (qui renvois dans les deux SPAN de la partie HTML) :


switch ($_POST['critere1']){
      case "nom" :
        echo "<table border='0' cellspacing='0' cellpadding='5'>
        <tr>
        <td>Nom : <input name='na1' id='na1' type='text'></td>
        <td></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td>Nom : <input name='nb1' id='nb1' type='text'></td>
        <td></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td>Nom : <input name='nc1' id='nc1' type='text'></td>
        <td></td>
        </tr>
        </table>"
;
      break;
      case "statuts" :
        echo "<table border='0' cellspacing='0' cellpadding='5'>
        <tr>
        <td><select name='sa1' id='sa1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='sa2' id='sa2'>"
;
        affiche_option('statuts');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='sb1' id='sb1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='sb2' id='sb2'>"
;
        affiche_option('statuts');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='sc1' id='sc1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='sc2' id='sc2'>"
;
        affiche_option('statuts');
        echo "</select></td>
        </tr>
        </table>"
;
      break;
      case "processus" :
        echo "<table border='0' cellspacing='0' cellpadding='5'>
        <tr>
        <td><select name='pa1' id='pa1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='pa2' id='pa2'>"
;
        affiche_option('processus');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='pb1' id='pb1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='pb2' id='pb2'>"
;
        affiche_option('processus');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='pc1' id='pc1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='pc2' id='pc2'>"
;
        affiche_option('processus');
        echo "</select></td>
        </tr>
        </table>"
;
      break;
    }
    switch ($_POST['critere2']){
      case "nom" :
        echo "<table border='0' cellspacing='0' cellpadding='5'>
        <tr>
        <td>Nom : <input name='nd1' id='nd1' type='text'></td>
        <td></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td>Nom : <input name='ne1' id='ne1' type='text'></td>
        <td></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td>Nom : <input name='nf1' id='nf1' type='text'></td>
        <td></td>
        </tr>
        </table>"
;
      break;
      case "statuts" :
        echo "<table border='0' cellspacing='0' cellpadding='5'>
        <tr>
        <td><select name='sd1' id='sd1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='sd2' id='sd2'>"
;
        affiche_option('statuts');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='se1' id='se1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='se2' id='se2'>"
;
        affiche_option('statuts');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='sf1' id='sf1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='sf2' id='sf2'>"
;
        affiche_option('statuts');
        echo "</select></td>
        </tr>
        </table>"
;
      break;
      case "processus" :
        echo "<table border='0' cellspacing='0' cellpadding='5'>
        <tr>
        <td><select name='pd1' id='pd1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='pd2' id='pd2'>"
;
        affiche_option('processus');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='pe1' id='pe1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='pe2' id='pe2'>"
;
        affiche_option('processus');
        echo "</select></td>
        </tr>
        <tr>
        <td colspan='2'>Ou</td>
        </tr>
        <tr>
        <td><select name='pf1' id='pf1'>
        <option value='egal'>egal a</option>
        <option value='dif'>different de</option>
        </select></td>
        <td><select name='pf2' id='pf2'>"
;
        affiche_option('processus');
        echo "</select></td>
        </tr>
        </table>"
;
      break;
 

#30 Re : MySQL, PostgreSQL, etc... » [Résolu] Recherche complexe » 11-06-2009 11:14:29

voila déjà un screen du formulaire :

screenfkg.jpg

Mon script test chaque valeurs pouvant être renvoyés :
les $_POST['pXX'] correspondent au chant de recherche par processus
les $_POST['nXX'] correspondent au chant de recherche par nom
les $_POST['sXX'] correspondent au chant de recherche par statuts

et pour chaque valeur concatène l'opérateur avec la valeur ainsi :

[code = php]
if ($_POST['sa2'] != "default" && $_POST['sa2'] != ""){
$cond = $cond.conditionsStatuts($_POST['sa1'],$_POST['sa2'],$appli);
}
[/code]
si le champs (ici le premier statut) est saisie, alors j'ajoute a mes conditions mon opérateur et ma valeur saisie (les fonction conditionsXXX se chargent de concaténer les 2 )

(pour les cas ou il y a une distinction entre OR et AND, c'est parce que lorsque la personne choisira "différent de" dans plusieurs champs il faut mettre OR pour avoir tout les cas correspondant) et c'est la que je suis un peu coincés, ou je ne vois plus trop clair hmm

c'est plus claire maintenant ?

#31 MySQL, PostgreSQL, etc... » [Résolu] Recherche complexe » 11-06-2009 11:14:29

Bibi
Réponses : 16

Bonjour,

Voila mon soucis, j'ai a faire une recherche complexe de mes fiches.
Pour ce faire, j'ai un formulaire :

2 critères principaux (creitere1 et critere2) pour lesquelles 3 types sont possible (statuts, nom ou processus)
Pour chaque types, il est possible de donner 3 valeurs, chacune pouvant être = ou != au type.

j'ai donc un peut de mal a jongler avec les OR, AND et les parenthèses :s.

voici le script de génération des conditions qui complète le select qui lui est toujours le même :

$cond = "";
        //debut des conditions
        // critere 1
        // statut 1
        $cond = "(".$cond;
        if ($_POST['sa2'] != "default" && $_POST['sa2'] != ""){
          $cond = $cond.conditionsStatuts($_POST['sa1'],$_POST['sa2'],$appli);
          //echo $_POST['sa2']."_".$cond."<br>";
        }
       
        // statut 2
        if ($_POST['sb2'] != "default" && $_POST['sb2'] != ""){
        if ($_POST['sb1'] == "egal"){
        $cond = $cond." OR ".conditionsStatuts($_POST['sb1'],$_POST['sb2'],$appli);
        }elseif ($_POST['sb1'] == "dif"){
        $cond = $cond." AND ".conditionsStatuts($_POST['sb1'],$_POST['sb2'],$appli);
        }
       
        //echo $_POST['sb2']."_".$cond."<br>";
        }
        // statut 3
        if ($_POST['sc2'] != "default" && $_POST['sc2'] != ""){
        if ($_POST['sc1'] == "egal"){
        $cond = $cond." OR ".conditionsStatuts($_POST['sc1'],$_POST['sc2'],$appli);
        }elseif ($_POST['sc1'] == "dif"){
        $cond = $cond." AND ".conditionsStatuts($_POST['sc1'],$_POST['sc2'],$appli);
        }
       
        //echo $_POST['sc2']."_".$cond."<br>";
        }
        // nom prenom 1
        if ($_POST['na1'] != ""){
          $cond = $cond.conditionsNom($_POST['na1'],$appli);
          //echo $_POST['sa2']."_".$cond."<br>";
        }
        // nom prenom 2
        if ($_POST['nb1'] != ""){
        $cond = $cond." OR ".conditionsNom($_POST['nb1'],$appli);
        //echo $_POST['sb2']."_".$cond."<br>";
        }
        // nom prenom 3
        if ($_POST['nc1'] != ""){
        $cond = $cond." OR ".conditionsNom($_POST['nc1'],$appli);
        //echo $_POST['sc2']."_".$cond."<br>";
        }
        // processus 1
        if ($_POST['pa2'] != "default" && $_POST['pa2'] != ""){
          $cond = $cond.conditionsProcessus($_POST['pa1'],$_POST['pa2'],$appli);
          //echo $_POST['sa2']."_".$cond."<br>";
        }
        // processus 2
        if ($_POST['pb2'] != "default" && $_POST['pb2'] != ""){
        if ($_POST['pb1'] == "egal"){
        $cond = $cond." OR ".conditionsProcessus($_POST['pb1'],$_POST['pb2'],$appli);
        }elseif ($_POST['pb1'] == "dif"){
        $cond = $cond." AND ".conditionsProcessus($_POST['pb1'],$_POST['pb2'],$appli);
        }
       
        //echo $_POST['sb2']."_".$cond."<br>";
        }
        // processus 3
        if ($_POST['pc2'] != "default" && $_POST['pc2'] != ""){
        if ($_POST['pc1'] == "egal"){
        $cond = $cond." OR ".conditionsProcessus($_POST['pc1'],$_POST['pc2'],$appli);
        }elseif ($_POST['pc1'] == "dif"){
        $cond = $cond." AND ".conditionsProcessus($_POST['pc1'],$_POST['pc2'],$appli);
        }
       
        //echo $_POST['sc2']."_".$cond."<br>";
        }
        // and du milieu
        if ($_POST['critere1'] != "default" && $_POST['critere2'] != "default"){
        $cond = $cond.") AND (";
        //echo $_POST['sd2']."_".$cond."<br>";
        }
       
        //debut des conditions
        // critere 2
        // statut 1
        if ($_POST['sd2'] != "default" && $_POST['sd2'] != ""){
        $cond = $cond.conditionsStatuts($_POST['sd1'],$_POST['sd2'],$appli);
        //echo $_POST['sd2']."_".$cond."<br>";
        }
        // statut 2
        if ($_POST['se2'] != "default" && $_POST['se2'] != ""){
        if ($_POST['se1'] == "egal"){
        $cond = $cond." OR ".conditionsStatuts($_POST['se1'],$_POST['se2'],$appli);
        }elseif ($_POST['se1'] == "dif"){
        $cond = $cond." AND ".conditionsStatuts($_POST['se1'],$_POST['se2'],$appli);
        }
       
        //echo $_POST['se2']."_".$cond."<br>";
        }
        // statut 3
        if ($_POST['sf2'] != "default" && $_POST['sf2'] != ""){
        if ($_POST['sf1'] == "egal"){
        $cond = $cond." OR ".conditionsStatuts($_POST['sf1'],$_POST['sf2'],$appli);
        }elseif ($_POST['sf1'] == "dif"){
        $cond = $cond." AND ".conditionsStatuts($_POST['sf1'],$_POST['sf2'],$appli);
        }
       
        //echo $_POST['sf2']."_".$cond."<br>";
        }
        // nom prenom 1
        if ($_POST['nd1'] != ""){
          $cond = $cond.conditionsNom($_POST['nd1'],$appli);
          //echo $_POST['sa2']."_".$cond."<br>";
        }
        // nom prenom 2
        if ($_POST['ne1'] != ""){
        $cond = $cond." OR ".conditionsNom($_POST['ne1'],$appli);
        //echo $_POST['sb2']."_".$cond."<br>";
        }
        // nom prenom 3
        if ($_POST['nf1'] != ""){
        $cond = $cond." OR ".conditionsNom($_POST['nf1'],$appli);
        //echo $_POST['sc2']."_".$cond."<br>";
        }
        // processus 1
        if ($_POST['pd2'] != "default" && $_POST['pd2'] != ""){
          $cond = $cond.conditionsProcessus($_POST['pd1'],$_POST['pd2'],$appli);
          //echo $_POST['sa2']."_".$cond."<br>";
        }
        // processus 2
        if ($_POST['pe2'] != "default" && $_POST['pe2'] != ""){
        if ($_POST['pe1'] == "egal"){
        $cond = $cond." OR ".conditionsProcessus($_POST['pe1'],$_POST['pe2'],$appli);
        }elseif ($_POST['pe1'] == "dif"){
        $cond = $cond." AND ".conditionsProcessus($_POST['pe1'],$_POST['pe2'],$appli);
        }
       
        //echo $_POST['sb2']."_".$cond."<br>";
        }
        // processus 3
        if ($_POST['pf2'] != "default" && $_POST['pf2'] != ""){
        if ($_POST['pf1'] == "egal"){
          $cond = $cond." OR ".conditionsProcessus($_POST['pf1'],$_POST['pf2'],$appli);
        }elseif ($_POST['pf1'] == "dif"){
        }
          $cond = $cond." AND ".conditionsProcessus($_POST['pf1'],$_POST['pf2'],$appli);
        //echo $_POST['sc2']."_".$cond."<br>";
        }
        $cond = $cond.")";

Les trois fonctions conditionsXXX, servent juste a concaténer le = ou le != avant d'ajouter la valeur.

Le soucis c'est que lorsque par exemple, mon premier critère est "statuts", que je lui demande tout les statuts donc = a 2 et tout les statuts != de 10, a l'affichage, je n'ai que les statuts = a 2 qui s'affiche.

auriez vous une idée ? comment gérer autrement les ET/OU et les parenthèses.

#32 Re : Défouloir » Quel style présentation avez-vous choisi ? » 04-06-2009 09:15:15

Cobalt pour ma part ^^

je vais regretter le marron de l'ancien ^^


mais au moins, je pourrais éditer mes post !!!!

et pour ça je vous dit un grand merci ! big_smile

Pied de page des forums

Propulsé par FluxBB