1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| <?php
//Tableau des restrictions
$restrictions = array('lieu' => '\'' . $lieu . '\'',
'superficie' => '\'' . $superf . '\'',
'prix' => '\'' . $prix . '\'',);
//Construction de la requete
$requete = 'SELECT id,' . implode(',', array_keys($restrictions)) . ',description,'
. implode('+', array_map(function($field,$value){
return 'IF(`'. $field . '` =' . $value . ',1,0)';
}, array_keys($restrictions), $restrictions) ) . ' AS `match` '
. ' FROM batiment '
. ' WHERE ' . implode(' OR ', array_map( function($field,$value){
return '`'. $field . '` =' . $value ;
}, array_keys($restrictions), $restrictions) ) . ''
. ' ORDER BY `match` DESC';
/**
* Donnera une requête de ce format
* SELECT id,lieu,superficie,
* prix,description,
* IF(`lieu` ='Toto',1,0)+IF(`superficie` ='Titi',1,0)+IF(`prix` ='Tata',1,0) AS `match`
* FROM batiment
* WHERE `lieu` ='Toto' OR `superficie` ='Titi' OR `prix` ='Tata'
* ORDER BY `match` DESC
*/
$result = mysql_query($requete);
//Si la requête a ramené quelquechose
if( $datas = mysql_fetch_assoc($result)) {
//Si le nombre de match est inférieur au nombre de restriction
if( count($restrictions) > $datas['match'] ) {
echo "<p align=center><font color=red size=+2>AUCUN RESULTAT NE CORRESPOND A VOTRE RECHERCHE!</font></p>";
} else {
?>
<table width="596" border="1" align="center">
<tr>
<td width="70" align="center" bgcolor="#FFFFFF">ID</td>
<td width="169" align="center" bgcolor="#FFFFFF">LIEU</td>
<td align="center" bgcolor="#FFFFFF">SUPERFICIE</td>
<td align="center" bgcolor="#FFFFFF">PRIX</td>
<td align="center" bgcolor="#FFFFFF">DESCRIPTION</td>
</tr>
<?php
do {
?>
<tr>
<td bgcolor="#CCCCCC"><?php echo($datas['id']); ?></td>
<td><?php echo htmlentities($datas['lieu'], ENT_QUOTES, 'UTF-8'); ?></td>
<td width="152"><?php echo htmlentities($datas['superficie'], ENT_QUOTES, 'UTF-8'); ?></td>
<td width="132"><?php echo htmlentities($datas['prix'], ENT_QUOTES, 'UTF-8'); ?></td>
<td bgcolor="#CCCCCC" width="152"><?php echo($datas['description']); ?></td>
</tr>
<?php
//On boucle tant qu'il y a des lignes et que la concordance est parfaite
}while ( $datas = mysql_fetch_assoc($result) && count($restrictions) == $datas['match']);
?>
</table>
<?php
}
//On ne rentre que si le précédent fetch a ramené des données
if( $datas ) {
?>
<BR/><BR/>
<HR/>
<p><font color="#006600" size="+2">Résultats qui pourraient vous interréssez...</font></p>
</table>
<table width="596" border="1" align="center">
<tr>
<td width="70" align="center" bgcolor="#FFFFFF">ID</td>
<td width="169" align="center" bgcolor="#FFFFFF">LIEU</td>
<td align="center" bgcolor="#FFFFFF">SUPERFICIE</td>
<td align="center" bgcolor="#FFFFFF">PRIX</td>
<td align="center" bgcolor="#FFFFFF">DESCRIPTION</td>
</tr>
<?php
do {
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo($datas['id']); ?></td>
<td><?php echo htmlentities($datas['lieu'], ENT_QUOTES, 'UTF-8'); ?></td>
<td width="152"><?php echo htmlentities($datas['superficie'], ENT_QUOTES, 'UTF-8'); ?></td>
<td width="132"><?php echo htmlentities($datas['prix'], ENT_QUOTES, 'UTF-8'); ?></td>
<td bgcolor="#CCCCCC" width="152"><?php echo($datas['description']); ?></td>
</tr>
<?php
//on boucle tant qu'il y a des lignes
} while ( $datas = mysql_fetch_assoc($result) );
?>
</table>
<?php
} //if( $datas )
}//if( $datas = mysql_fetch_assoc($result)) |
Partager