0

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 ...

--------------------------- ///

789798
  • 1
  • 1
  • You need to share your code snippet. Var_dump gives no idea on where you are getting the data from and how you are processing that data in your code before dumping it – Metabolic Oct 24 '20 at 11:22
  • I think passing an array via an url should be done like described here: [passing-arrays-as-url-parameter](https://stackoverflow.com/questions/1763508/passing-arrays-as-url-parameter) – Luuk Oct 24 '20 at 11:52
  • I'm going to see all that ... thank you for answering and referring me! ... :) – 789798 Oct 24 '20 at 12:04

0 Answers0