For my end of studies projects I decided to do a little e-commerce problem on the filter.. I use an ajax request at the click of the checkbox this goes to my controller which calls a method of my Model class but the concern is that if the array $ _POST ['brands']
has several elements then only the last one is returned to me? I thought I saw one day that if the same variable is in a url then the last value will be returned to me is for security but how can I do? I have been there for too long .. and finally despair. Help me please... :')
-------------- FROM ---------------- thx
init_brands(val, categorie){
console.log(val);
if(this.brands.indexOf(val) != -1){
this.brands.pop(val)
console.log(this.brands)
}else{
this.brands.push(val)
console.log(this.brands)
}
if(this.brands.length === 0){
$.post({
url : "./menu/"+ categorie,
})
}else{
$.post({
url : "./menu/"+ categorie,
data: {
"brands" : this.brands
}
})
}
}
/* CONTROLLER PHP */
public function control_articles_filter(){
if(isset($_POST['brands']) && !empty($_POST['brands'])){
$res = Articles::return_articles_by_categories_and_brands($_GET['a'], $_POST['brands']);
return $res;
}else{
$_POST['brands'] = 0;
$res = Articles::return_articles_by_categorie($_GET['a']);
return $res;
}
}
/* MODEL PHP */
public function return_articles_by_categories_and_brands($categorie,$brands){
$db = ModelArticles::connect_db_articles();
$sql = "SELECT marque FROM articles WHERE categorie_article=? AND marque IN (";
$params = [];
if(count($brands) > 12){
header("Location : ./");
}else{
foreach($brands as $brand){
$params[] = '?';
$binds[] = $brand;
}
$req = $db->prepare($sql . join(',', $params) . ')');
echo "<- prepare <br>" , var_dump($req);
$req->bindParam(1, $categorie);
$i = 1;
foreach($binds as $bind){
$req->bindParam(++$i, $bind);
echo "<br>";
var_dump($bind);
echo "<- bind <br>";
}
$req->execute();
echo "var_dump() of my fetchAll() <br>";
return var_dump($req->fetchAll());
}
}
<form action="/" method="POST">
<?php $i = 0; ?>
<?php foreach ($brands as $brand): ?>
<input type="checkbox" onclick="filter.init_brands('<?= $brand['marque'] ?>', '<?= $_GET['a'] ?>')" >
<p class="is-inline is-size-7"><?= $brand['marque'] ?></p>
<?php endforeach; ?>
</form>
}
request payload:
brands%5B%5D=arai&brands%5B%5D=bell
reply :
<- prepare var_dump ()
object (PDOStatement) # 2 (1) {["queryString"] => string (73) "SELECT mark FROM articles WHERE categorie_article =? AND mark IN (?,?)"}
string (4) "arai" <- bind
string (4) "bell" <- bind
var_dump () of my fetchAll ()
array (2) {[0] => array (2) {["brand"] => string (4) "bell" [0] => string (4) "bell"} [1] => array (2 ) {["brand"] => string (4) "bell" [0] => string (4) "bell"}}
image_icon
since the last value taken into account is bell then I get two items of the bell brand from my bdd but no arai ...
--------------------------- ///