Last active
May 13, 2017 16:42
-
-
Save julp/3b8b55bbe5e7895395f9cd78fbfd76aa to your computer and use it in GitHub Desktop.
search.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// simulation d'une soumission de formulaire (à supprimer en prod, évidemment) | |
$_POST = [ | |
'search' => 'foo', | |
'envieselect' => range(7, 9), | |
'materiauselect' => range(1, 4), | |
]; | |
// $bdd = new PDO(/* ... */); | |
// les champs sur lequels tenter un LIKE | |
const LIKE_FIELDS = [ | |
'mprojets.nomprojet', | |
'mprojets.descriptionprojet', | |
]; | |
// les sous-parties de la clause WHERE | |
// initialisée à ['1=1'] (condition par défaut) pour tout afficher par défaut | |
$where = ['1=1']; | |
// dépendances entre tables (pour générer les INNER JOIN) | |
$tables = ['mprojets' => []]; | |
function identifier(string $id): string { | |
return '`' . str_replace('`', '``', $id) . '`'; | |
} | |
function in_int(?string $table, string $col, array $ids): string { | |
return ($table ? identifier($table) . '.' : '') . identifier($col) . ' IN(' . implode(', ', array_map('intval', $ids)) . ')'; | |
} | |
if (isset($_POST['materiauselect']) && is_array($_POST['materiauselect'])) { | |
$tables['mprojets']['mprojetsmateriaux'] = 'idprojet'; | |
$tables['mprojetsmateriaux']['mmateriaux'] = 'idmateriau'; | |
$where[] = in_int('mmateriaux', 'idmateriau', $_POST['materiauselect']); | |
} | |
if (isset($_POST['outilselect']) && is_array($_POST['outilselect'])) { | |
$tables['mprojets']['mprojetsoutils'] = 'idprojet'; | |
$tables['mprojetsoutils']['moutils'] = 'idoutil'; | |
$where[] = in_int('moutils', 'idoutil', $_POST['outilselect']); | |
} | |
if (isset($_POST['envieselect']) && is_array($_POST['envieselect'])) { | |
$tables['mprojets']['mprojetsenvies'] = 'idprojet'; | |
$tables['mprojetsenvies']['menvies'] = 'idenvie'; | |
$where[] = in_int('menvies', 'idenvie', $_POST['envieselect']); | |
} | |
if (isset($_POST['search'])) { | |
$where[] = '(' . implode(' OR ', array_map(function ($v) use ($bdd) { return $v . ' LIKE ' . $bdd->quote('%' . $_POST['search'] . '%'); }, LIKE_FIELDS)) . ')'; | |
} | |
$query = 'SELECT * FROM mprojets '; | |
foreach ($tables['mprojets'] as $table => $col) { | |
$query .= ' INNER JOIN ' . identifier($table) . ' USING(' . identifier($col) . ') '; | |
// TODO: trouver mieux | |
foreach ($tables[$table] as $table => $col) { | |
$query .= ' INNER JOIN ' . identifier($table) . ' USING(' . identifier($col) . ') '; | |
} | |
} | |
$stmt = $bdd->query($query . ' WHERE ' . implode(' AND ', $where)); | |
foreach ($stmt as $recherche_data) { | |
echo '<p><a href="../projets/' . urlencode($recherche_data['url']) . '" title="' . htmlspecialchars($recherche_data['descriptionprojet']) . '">' . htmlspecialchars(mb_convert_case($recherche_data['nomprojet'], MB_CASE_TITLE)) . '</a></p>'; | |
} | |
/* | |
FROM mprojets p | |
INNER JOIN mprojetsmateriaux pmat | |
ON pmat.idprojet = p.idprojet | |
INNER JOIN mmateriaux materiaux | |
ON pmat.idmateriau=materiaux.idmateriau | |
INNER JOIN mprojetsoutils pout | |
ON pout.idprojet = p.idprojet | |
INNER JOIN moutils outils | |
ON pout.idoutil=outils.idoutil | |
INNER JOIN mprojetsenvies penv | |
ON penv.idprojet = p.idprojet | |
INNER JOIN menvies envies | |
ON penv.idenvie=envies.idenvie | |
*/ | |
/* | |
mprojets (p) | |
mprojetsmateriaux (pmat) | |
mmateriaux (materiaux) | |
mprojetsoutils (pout) | |
moutils (outils) | |
mprojetsenvies (penv) | |
menvies (envies) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment