[RESOLU] Recherche avec conditions

Eléphant du PHP | 233 Messages

03 avr. 2015, 13:45

Bonjour à tous,
j'ai fais un script de recherche sur plusieurs tables sur plusieurs champs, ça fonctionne mais je voudrais simplifier la requête sql qui n'est pas très ergonomique en lecture.

Je sais qu'il y a quelque comme ça :
<?php If(!empty($_POST['region']){
$sql .= 'and idregion='.$idregion;
} ?>


mais je n'arrive pas à l'adapter à mon script écrit avec mysqli

Merci de vos suggestions ...
<?php if(empty($_POST['region'])) {

echo'<br /><font color="red">Merci de choisir une région !</font><br /><br />';
}

elseif(empty($_POST['departement']) && empty($_POST['ville'])) {
$sql = mysqli_query($bdd, "SELECT * FROM membre, region, departement, ville WHERE membre.id_departement=departement.id_departement and region.id_region=departement.id_region and membre.id_ville=ville.id_ville and membre.valide=1 and membre.id_region='".$_POST['region']."' and sexe='".$_POST['sexe']."' ORDER BY id DESC")or die(mysqli_error($bdd));

}

elseif(empty($_POST['ville'])) {
$sql = mysqli_query($bdd, "SELECT * FROM membre, region, departement, ville WHERE membre.id_departement=departement.id_departement and region.id_region=departement.id_region and membre.id_ville=ville.id_ville and membre.valide=1 and membre.id_departement='".$_POST['departement']."' and sexe='".$_POST['sexe']."' ORDER BY id DESC")or die(mysqli_error($bdd));

}

else {
$sql = mysqli_query($bdd, "SELECT * FROM membre, region, departement, ville WHERE membre.id_departement=departement.id_departement and region.id_region=departement.id_region and membre.id_ville=ville.id_ville and membre.valide=1 and membre.id_departement='".$_POST['departement']."' and membre.id_ville='".$_POST['ville']."' and sexe='".$_POST['sexe']."' ORDER BY id DESC")or die(mysqli_error($bdd));

} ?>

ViPHP
ViPHP | 1996 Messages

04 avr. 2015, 00:45

Rapidement mais sans vérifier
<?php 
if(empty($_POST['region'])) 
	echo'<br /><font color="red">Merci de choisir une région !</font><br /><br />';
else {
	$requete = "SELECT * FROM membre, region, departement, ville WHERE membre.id_departement=departement.id_departement and region.id_region=departement.id_region and membre.id_ville=ville.id_ville and membre.valide=1";

	if(empty($_POST['departement']) && empty($_POST['ville'])) 
		$requete .= " and membre.id_region='".$_POST['region']."' and sexe='".$_POST['sexe']."' ORDER BY id DESC";	

	elseif(empty($_POST['ville'])) 
		$requete .= " and membre.id_departement='".$_POST['departement']."' and sexe='".$_POST['sexe']."' ORDER BY id DESC";	

	else 
		$requete .= " and membre.id_departement='".$_POST['departement']."' and membre.id_ville='".$_POST['ville']."' and sexe='".$_POST['sexe']."' ORDER BY id DESC";
	
	$sql = mysqli_query($bdd,$requete)or die(mysqli_error($bdd));
}
?>
It is nice to be important but it is more important to be nice
http://www.aureuswebfactory.fr

Eléphant du PHP | 233 Messages

04 avr. 2015, 10:11

Bonjour et merci de cette réponse même rapide car ça fonctionne :)

merci encore de votre aide