Nanor |
Le 11/05/2006 à 22:12 |
|
|
Bonjour  je suis sur la fin d'un programme informatique permettant de creer un moteur de recherche et je bute sur un probleme tout simple de checkbox car je ne sais pas comment me servir des informations des checkbox quand il y a plusieurs checkbox,j'ai mis en rouge tous les passages qui concernent les checkbox en esperant que quelqu'un pourra m'aider,je sais que j'abuse peut etre un petit peu en ayant fait un copier/coller de cette taille mais mon probleme est comment declarer un foreach avec mes variables et surtout ou l'inserer dans mon code.Quelqu'un peut il m'aider? J'ai declare mes checkbox de cette facon: <input name="typeperf[]" type="checkbox" id="typeperf" value="Performer">
<input name="typeperf[]" type="checkbox" id="typeperf" value="Panel">
<input name="typeperf[]" type="checkbox" id="typeperf" value="Workshop">
</p>
<?php
array('Performer', 'Panel','Workshop');
$where = 'WHERE';
$and=false; //au départ aucun type d'information n'a été détécté
if(ISSET($_POST['perfname'])&&$_POST['perfname']!='')
{$where = $where.' pr.PerfName="'.$_POST['perfname'].'"';
$and=true;}
if(ISSET($_POST['typeperf'])&&$_POST['typeperf']!='')
foreach($_POST['typeperf'] as $typeperf)
{if($and) $where = $where.' AND '; //Si il y a déjà une recherche de faite auparavant, j'ajoute un AND
$where = $where.' pr.TypePerf="'.$_POST['typeperf'].'"';
$and=true;}
if(ISSET($_POST['perfcodename'])&&$_POST['perfcodename']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pr.PerfCodeName="'.$_POST['perfcodename'].'"';
$and=true;}
if(ISSET($_POST['date'])&&$_POST['date']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.Date="'.$_POST['date'].'"';
$and=true;}
if(ISSET($_POST['start'])&&$_POST['start']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.Start="'.$_POST['start'].'"';
$and=true;}
if(ISSET($_POST['end'])&&$_POST['end']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.End="'.$_POST['end'].'"';
$and=true;}
if(ISSET($_POST['stage'])&&$_POST['stage']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pl.Stage="'.$_POST['stage'].'"';
$and=true;}
if(ISSET($_POST['locstage'])&&$_POST['locstage']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pl.LocStage="'.$_POST['locstage'].'"';
$and=true;}
if($and!=true) $where = $where.' 1';
$requete = "SELECT *";
$requete .= " FROM Table";
$requete .= " WHERE 1";
foreach($_POST['typeperf'] as $typeperf) {
$valeurSQL = addslashes($typeperf);
$requete .= " AND pr.TypePerf = '$valeurSQL'";
}
$q = 'SELECT pr.*,
TIME_FORMAT(pf.Start, \'%h:%i %p\') AS start_12,
TIME_FORMAT(pf.End, \'%h:%i %p\') AS end_12,
pf.*,
pl.*
FROM performer AS pr
INNER JOIN performance AS pf ON pr.IDPerf=pf.IDPerf
INNER JOIN place AS pl ON pl.CodeStage=pf.CodeStage '.$where;
echo $_POST['perfname'];
echo $q;
$r = mysql_query($q);
print mysql_error();
while ($row=mysql_fetch_array($r)){
$idperf = $row[IDPerf];
$perfname = $row[PerfName];
$typeperf = $row[TypePerf];
print "<td>$typeperf</td>\n";
|
Anthony |
Le 12/05/2006 à 15:50 |
|
 Admin |
Salut,
pour récupérer les valeurs de tes checkbox :
<?php
if(isset($_POST['typeperf']) AND is_array($_POST['typeperf']))
{
foreach($_POST['typeperf'] AS $valeur)
{
echo $valeur,'<br />';
}
}
?>
ça va t'afficher toutes les valeurs des checkbox sélectionnées ;) -- 
|
Nanor |
Le 12/05/2006 à 18:57 |
|
|
Merci pour ta reponse elle m'a permis de me montrer que je suis sur le bon chemin car elle m'affiche bien les 3 valeurs de typeperf quand les trois cases sont cochees  ,le probleme c'est que je voudrais me servir de ces valeurs pour aller chercher des informations dans la base de donnees  ,en gros je veux qu'il m'affiche si je coche la case "performer" et "panel" toutes les informations relatives a "Perforformer" et a "Panel" j'ai insere le code que tu ma propose entre la ligne 8 et 15 mais ca ne m'affiche rien,j'en demande peut etre beaucoup mais peux tu regarder mon code une derniere fois s'il te plait:
<?php
array('Performer', 'Panel','Workshop');
$where = 'WHERE';
$and=false; //au départ aucun type d'information n'a été détécté
if(ISSET($_POST['perfname'])&&$_POST['perfname']!='')
{$where = $where.' pr.PerfName="'.$_POST['perfname'].'"';
$and=true;}
//lignes d'Anthony inseree par Nanor
if(isset($_POST['typeperf']) AND is_array($_POST['typeperf']))
{
foreach($_POST['typeperf'] AS $valeur)
{
if(ISSET($_POST['typeperf'])&&$_POST['typeperf']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pr.TypePerf="'.$_POST['typeperf'].'"';
$and=true;}}}
//fin de l'insertion
if(ISSET($_POST['perfcodename'])&&$_POST['perfcodename']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pr.PerfCodeName="'.$_POST['perfcodename'].'"';
$and=true;}
if(ISSET($_POST['date'])&&$_POST['date']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.Date="'.$_POST['date'].'"';
$and=true;}
if(ISSET($_POST['start'])&&$_POST['start']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.Start="'.$_POST['start'].'"';
$and=true;}
if(ISSET($_POST['end'])&&$_POST['end']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.End="'.$_POST['end'].'"';
$and=true;}
if(ISSET($_POST['stage'])&&$_POST['stage']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pl.Stage="'.$_POST['stage'].'"';
$and=true;}
if(ISSET($_POST['locstage'])&&$_POST['locstage']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pl.LocStage="'.$_POST['locstage'].'"';
$and=true;}
if($and!=true) $where = $where.' 1';
$q = 'SELECT pr.*,
pf.*,
pl.*
FROM performer AS pr
INNER JOIN performance AS pf ON pr.IDPerf=pf.IDPerf
INNER JOIN place AS pl ON pl.CodeStage=pf.CodeStage '.$where;
echo $_POST['perfname'];
echo $q;
$r = mysql_query($q);
print mysql_error();
while ($row=mysql_fetch_array($r)){
$idperf = $row[IDPerf];
$perfname = $row[PerfName];
$typeperf = $row[TypePerf];
$perfcodename = $row[PerfCodeName];
$tagline = $row[Tagline];
$idperformance = $row[IDPerformance];
$date = $row[Date];
$start = $row[Start];
$end = $row[End];
$codestage = $row[CodeStage];
$stage = $row[Stage];
$locstage = $row[LocStage];
echo "<tr>";
/* Affichage du contenu de performer */
print "<td>$perfname</td>\n";
print "<td>$typeperf</td>\n";
print "<td>$perfcodename</td>\n";
print "<td>$tagline</td>\n";
/* Affichage du contenu de performance */
print "<td>$date</td>\n";
print "<td>$start</td>\n";
print "<td>$end</td>\n";
/* Affichage du contenu de stage */
echo "<td>$stage</td>\n";
echo "<td>$locstage</td>\n";
echo "</tr>";
}
?>
|
Anthony |
Le 12/05/2006 à 19:29 |
|
 Admin |
Faudrait que tu génères une variable qui elle-même va gérérer dynamiquement ta requête.
en gros :
$fin_requete='';
if(isset($_POST['blabla']...)) { $fin_requete.=' AND champ=machin'; }
par exemple..
ensuite mysql_query('SELECT machin FROM table WHERE machin '.$fin_requete); -- 
|
Nanor |
Le 12/05/2006 à 19:58 |
|
|
J'ai presque reussi j'ai refait mon code avec tes indications voila ce que cela me donne,l'ennui c'est que maintenant quand je lance une recherche et que je coche mes cases "Typeperf" il va me chercher toutes les informations qui ont des "Performer" et des "Panel"(si ces deux cases ont ete cochees)c'est a dire qu'il ne me sort aucune information,c'est pourquoi j'ai change le AND par un OR mais du coup il ne me fait plus la liaison avec les autres informations,par exemple si je lui dis va me chercher les informations qui ont des Performer et des Panel et qui jouent a telle date il va me chercher les informations qui ont des panel a toutes les dates et des performer a la date entree,en gros il ne me fait pas Panel AND Date OR Performer AND Date mais me fait Panel OR Performer AND Date et du coup ne me prend plus entierement en compte la date peux ,tu me dire comment faut il faire?(on est tres pres du but pour abandonner  ):
array('Performer', 'Panel','Workshop');
$where = 'WHERE';
$and=false; //au départ aucun type d'information n'a été détécté
if(ISSET($_POST['perfname'])&&$_POST['perfname']!='')
{$where = $where.' pr.PerfName="'.$_POST['perfname'].'"';
$and=true;}
if(isset($_POST['typeperf']) AND is_array($_POST['typeperf']))
{
foreach($_POST['typeperf'] AS $valeur)
{
if(ISSET($_POST['typeperf'])&&$_POST['typeperf']!='')
{if($and) $where = $where.' OR '; //C'est ici qu'il y a probleme j'ai change le AND par un OR
$where = $where.' pr.TypePerf="'.$valeur.'"';
$and=true;}}}
if(ISSET($_POST['perfcodename'])&&$_POST['perfcodename']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pr.PerfCodeName="'.$_POST['perfcodename'].'"';
$and=true;}
if(ISSET($_POST['date'])&&$_POST['date']!='')
{if($and) $where = $where.' AND ';
$where = $where.' pf.Date="'.$_POST['date'].'"';
$and=true;}
|
Anthony |
Le 12/05/2006 à 20:55 |
|
 Admin |
Les parenthèses (priorité des opérateurs) s'appliquent en SQL
WHERE (condition OR autre_condition) AND encore_condition est différent de WHERE condition OR (condition AND encore_condition) -- 
|
Nanor |
Le 12/05/2006 à 21:40 |
|
|
Je pense avoir compris tes explications  , l'ennui c'est que je ne sais pas ou mettre ces parentheses  et donc je ne sais pas ou determiner la priorite par exemple si je lance le cas present et que je demande a ce qu'on m'affiche la requete je vais obtenir cela: SELECT pr.*, pf.*, pl.* FROM performer AS pr INNER JOIN performance AS pf ON pr.IDPerf=pf.IDPerf INNER JOIN place AS pl ON pl.CodeStage=pf.CodeStage WHERE pr.TypePerf="Panel" OR pr.TypePerf="Workshop" AND pf.Date="5/28/2006"le probleme vient du fait que je ne sais pas ou mettre les parentheses pour qu'il me fasse la difference entre 2 type perf
if(isset($_POST['typeperf']) AND is_array($_POST['typeperf']))
{
foreach($_POST['typeperf'] AS $valeur)
{
if(ISSET($_POST['typeperf'])&&$_POST['typeperf']!='')
{if($and) $where = $where.' OR '; //Si il y a déjà une recherche de faite auparavant, j'ajoute un OR
$where = $where.' pr.TypePerf="'.$valeur.'"';
$and=true;}}}
|
Nanor |
Le 14/05/2006 à 05:57 |
|
|
En fait ce que je cherche a faire depuis le debut c'est : WHERE (pr.PerfName="Valeur choisie")AND(pr.TypePerf="Performer" OR pr.TypePerf="Panel" OR pr.TypePerf="Workshop") AND (pr.PerfCodeName="Valeur choisie" )AND (pf.Date="5/26/2006" OR pf.Date="5/27/2006" OR pf.Date="5/28/2006" OR pf.Date="5/29/2006")AND (pf.Start="Valeur choisie") AND (pf.End="valeur choisie") AND (pl.Stage="Valeur choisie") AND (pl.LocStage="Outdoor" OR pl.LocStage="Indoor") avec le code suivant mais je ne vois vraiment pas comment faire(j'ai esseye mille et une combinaisons de parentheses  ),peux tu m'aider s'il te plait?  : J'ai finalement resolu mon probleme,merci a tous ceux qui m'ont aide(principalement Anthony) merci merci beaucoup:
<?php
// Début du code proprement dit !!
array('Performer', 'Panel','Workshop');
array('5/26/2006','5/27/2006','5/28/2006','5/29/2006');
array('Indoor', 'Outdoor');
$where = 'WHERE ';
$and=false; //au départ aucun type d'information n'a été détécté
if(ISSET($_POST['perfname'])&&$_POST['perfname']!='')
{
$where .= ' pr.PerfName="'.$_POST['perfname'].'"';
$and=true;
}
if(isset($_POST['typeperf']))
{
if (is_array($_POST['typeperf']))
{
$premier = true; // A METTRE DANS LES TROIS !!
$nonvide = false;
foreach($_POST['typeperf'] AS $valeur)
{
if ($valeur != "")
{
if (!$nonvide)
{
if($and) $where .= ' AND '; //RAJOUT !!!
$where .= ' ( ';
$nonvide=true;
}
if(!$premier)// MODIF
{// MODIF
$where .= ' OR '; // MODIF
}// MODIF
else// MODIF
{// MODIF
$premier=false; // MODIF
}// MODIF
$where .= ' pr.TypePerf="'.$valeur.'"';
$and=true;
}
}
if ($nonvide)
{
$where .= ' ) ';
}
}
}
if(ISSET($_POST['perfcodename']))
{
if ($_POST['perfcodename']!='')
{
if($and) $where .= ' AND ';
$where .= ' ( ';
$where .= ' pr.PerfCodeName="'.$_POST['perfcodename'].'"';
$where .= ' ) ';
$and=true;
}
}
if(isset($_POST['date']))
{
if (is_array($_POST['date']))
{
$premier = true; // A METTRE DANS LES TROIS !!
$nonvide = false;
foreach($_POST['date'] AS $valeur)
{
if ($valeur != "")
{
if (!$nonvide)
{
if($and) $where .= ' AND '; //RAJOUT !!!
$where .= ' ( ';
$nonvide=true;
}
if(!$premier)// MODIF
{// MODIF
$where .= ' OR '; // MODIF
}// MODIF
else// MODIF
{// MODIF
$premier=false; // MODIF
}// MODIF
$where .= ' pf.Date="'.$valeur.'"';
$and=true;
}
}
if ($nonvide)
{
$where .= ' ) ';
}
}
}
if(ISSET($_POST['start']))
{
if ($_POST['start']!='')
{
if($and) $where .= ' AND ';
$where .= ' ( ';
$where .= ' pf.Start="'.$_POST['start'].'"'; //VERIFIER !!!!!!!!!!
$where .= ' ) ';
$and=true;
}
}
if(ISSET($_POST['end']))
{
if ($_POST['end']!='')
{
if($and) $where .= ' AND ';
$where .= ' ( ';
$where .= ' pf.End="'.$_POST['end'].'"'; //VERIFIER !!!!!!!!!!
$where .= ' ) ';
$and=true;
}
}
if(ISSET($_POST['stage']))
{
if ($_POST['stage']!='')
{
if($and) $where .= ' AND ';
$where .= ' ( ';
$where .= ' pl.Stage="'.$_POST['stage'].'"'; //VERIFIER !!!!!!!!!!
$where .= ' ) ';
$and=true;
}
}
if(isset($_POST['locstage']))
{
if (is_array($_POST['locstage']))
{
$premier = true; // A METTRE DANS LES TROIS !!
$nonvide = false;
foreach($_POST['locstage'] AS $valeur)
{
if ($valeur != "")
{
if (!$nonvide)
{
if($and) $where .= ' AND '; //RAJOUT !!!
$where .= ' ( ';
$nonvide=true;
}
if(!$premier)// MODIF
{// MODIF
$where .= ' OR '; // MODIF
}// MODIF
else// MODIF
{// MODIF
$premier=false; // MODIF
}// MODIF
$where .= ' pl.LocStage="'.$valeur.'"';
$and=true;
}
}
if ($nonvide)
{
$where .= ' ) ';
}
}
}
if($and!=true) $where .= ' 1';
$q = 'SELECT pr.*,
pf.*,
pl.*
FROM performer AS pr
INNER JOIN performance AS pf ON pr.IDPerf=pf.IDPerf
INNER JOIN place AS pl ON pl.CodeStage=pf.CodeStage '.$where;
echo "$q";
|