It's a search by criterion form.
I want to display the results corresponding to the values selected in my <select multiple="multiple" name="secteur_searched[]" > together with those that match the checked type_of_contrat checkboxes (if checked). The user can fill in / select / check as many criterions as desired, or none for a general output.
My problem lays is the WHERE IN condition = in the multiselect and checkboxes. For now let's focus only on my multiselect.
How would you write the code supposed to go into the curly brackets in here :?
if (!empty($REQUEST['secteur_searched']) AND is_array($REQUEST["secteur_searched"]) )
{
$where[] = "j.job_secteur IN (:job_secteur)";
$param[":job_secteur"] = implode(",", $_REQUEST['secteur_searched']); // This code returns errors
}
Thank u very very very much 🙂
Here's my almost full code so you can see what I've done so far to display the results:
$query = "SELECT * FROM marinterim_job_offers j JOIN marimmo_villes v ON v.ville_id =j.job_ville ";
$where = array();
$param = array();
if (!empty($_REQUEST['job_searched']))
{ $where[] = "j.job_intitule=:job_intitule ";
$param[':job_intitule'] = $_REQUEST['job_searched'];
}
if (!empty($_REQUEST['job_keyword']))
{ $where[] = " j.job_descriptif LIKE CONCAT('%', :job_descriptif, '%') " ;
$param[':job_descriptif'] = $_REQUEST['job_keyword'];
}
/* if (!empty($_REQUEST['secteur_searched']) AND is_array($_REQUEST["secteur_searched"]) )
{
$where[] = "j.job_secteur IN (:job_secteur)";
$param[":job_secteur"] = implode(",", $_REQUEST['secteur_searched']);
}
*/
if (!empty($_REQUEST["secteur_searched"]) && is_array($_REQUEST["secteur_searched"]))
{ $x= array_values($_REQUEST["secteur_searched"]);
$secteur = implode(',', array_fill(0, count($_REQUEST["secteur_searched"]), '?'));
$where[] = " j.job_secteur IN ( :job_secteur )" ;
//$param[':job_secteur']= $secteur;
$param = explode(",", "$x");
}
if (!empty($where))
{
$query.= ' WHERE ' . implode(' AND ', $where);
}
$query.= " ORDER BY j.job_date_insertion DESC";
$sth =$marInterim ->prepare($query);
$sth->execute($param);
$compte = $sth->fetchAll();
$nb_resultats = count($compte);
$errors['nb_resultats_recherche'] = $nb_resultats; // ok
/********************/
if ( !empty($_REQUEST['afficher_x_resultats'])) // Mostrar x resultados
{ $per_page=$_REQUEST['afficher_x_resultats'];
}
else {$per_page=10; }
/* Results per page */
$nb_pages = ceil($nb_resultats/$per_page);
$current_page = isset($_REQUEST['page']) && ($_REQUEST['page'] > 0) && ($_REQUEST['page'] <= $nb_pages) ? $_REQUEST['page'] : 1;
$start = ($current_page-1)*$per_page;
$query2= $query." LIMIT $start,$per_page ";
// echo '<br /> $query2 = '.$query2;
$sth2 =$marInterim ->prepare($query2);
$sth2-> execute($param);
echo "<pre>";
echo "<br>QUERY <br>";
print_r($query2);
echo "<br>PARAM<br>";
print_r($param);
echo "</pre>";
while($datos= $sth2->fetch(PDO::FETCH_ASSOC))
{ $en_date_insertion=$datos['job_date_insertion'];
$explode_insertion= explode("-", $en_date_insertion);
$date_insertion_fr = $explode_insertion[2]."-".$explode_insertion[1]."-".$explode_insertion[0];
$job_id= $datos['job_id'];
$job_intitule= strtoupper($datos['job_intitule']);
$job_ville = $datos['ville_nom'];
$job_cp = $datos['cp'];
echo "
<div class='offers btns'>
<a class='link_vers_offre' href='job_offer_detail.php?job_id=$job_id'>
<table id='table_liste_des_offres'>
<tr>
<td class='liste_intitule'> " .$job_intitule." </td>
<td class='liste_ref'>Offre n°" .$job_id." du ".$date_insertion_fr ." </td>
<td class='liste_ville'> ".$job_cp. " ".$job_ville." </td>
</tr>
</table>
</a>
</div>
";
}
} catch(Exception $e)
{ exit('<b>Catched exception at line '. $e->getLine() .' :</b> '. $e->getMessage());
}
}