Advertisement
ostyleo

Untitled

May 13th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. <?php
  2.  
  3. $host = '127.0.0.1';
  4. $user = 'root';
  5. $pass = '';
  6. $name = 'pw';
  7. $con = new mysqli($host, $user, $pass, $name) or die("Can't connect");
  8. $criterii = json_decode($_REQUEST["criterii"]);
  9. $elements = "";
  10.  
  11.  
  12. if ($criterii->empty == 'true') {
  13.     $query = 'SELECT * FROM laptops';
  14.     $result = $con->query($query);
  15.     while ($rows = $result->fetch_assoc()) {
  16.         $elements .= $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  17.     }
  18. } else {
  19.     $prods = array();
  20.     $processors = array();
  21.     $memories = array();
  22.     $hdds = array();
  23.     $videoCards = array();
  24.     $allEl = array();
  25.  
  26.     $query = 'SELECT * FROM laptops';
  27.     $result = $con->query($query);
  28.     while ($rows = $result->fetch_assoc()) {
  29.         $el = $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  30.         array_push($allEl, $el);
  31.     }
  32.    
  33.     foreach ($criterii->Producator as $prod) {
  34.         $query = 'SELECT * FROM laptops WHERE Producator=?';
  35.         $stmt = $con->prepare($query);
  36.         if ($stmt === false) {
  37.             trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR);
  38.         }
  39.         $stmt->bind_param('s', $prod);
  40.         $stmt->execute();
  41.  
  42.         $result = $stmt->get_result();
  43.         while ($rows = $result->fetch_array(MYSQLI_ASSOC)) {
  44.             $el = $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  45.             array_push($prods, $el);
  46.         }
  47.     }
  48.  
  49.     foreach ($criterii->Procesor as $prod) {
  50.         $query = 'SELECT * FROM laptops WHERE Procesor=?';
  51.         $stmt = $con->prepare($query);
  52.         if ($stmt === false) {
  53.             trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR);
  54.         }
  55.         $stmt->bind_param('s', $prod);
  56.         $stmt->execute();
  57.  
  58.         $result = $stmt->get_result();
  59.         while ($rows = $result->fetch_array(MYSQLI_ASSOC)) {
  60.             $el = $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  61.             array_push($processors, $el);
  62.         }
  63.     }
  64.  
  65.     foreach ($criterii->Memorie as $prod) {
  66.         $query = 'SELECT * FROM laptops WHERE Memorie=?';
  67.         $stmt = $con->prepare($query);
  68.         if ($stmt === false) {
  69.             trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR);
  70.         }
  71.         $stmt->bind_param('s', $prod);
  72.         $stmt->execute();
  73.  
  74.         $result = $stmt->get_result();
  75.         while ($rows = $result->fetch_array(MYSQLI_ASSOC)) {
  76.             $el = $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  77.             array_push($memories, $el);
  78.         }
  79.     }
  80.  
  81.     foreach ($criterii->HDD as $prod) {
  82.         $query = 'SELECT * FROM laptops WHERE HDD=?';
  83.         $stmt = $con->prepare($query);
  84.         if ($stmt === false) {
  85.             trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR);
  86.         }
  87.         $stmt->bind_param('s', $prod);
  88.         $stmt->execute();
  89.  
  90.         $result = $stmt->get_result();
  91.         while ($rows = $result->fetch_array(MYSQLI_ASSOC)) {
  92.             $el = $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  93.             array_push($hdds, $el);
  94.         }
  95.     }
  96.  
  97.     foreach ($criterii->PlacaVideo as $prod) {
  98.         $query = 'SELECT * FROM laptops WHERE PlacaVideo=?';
  99.         $stmt = $con->prepare($query);
  100.         if ($stmt === false) {
  101.             trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR);
  102.         }
  103.         $stmt->bind_param('s', $prod);
  104.         $stmt->execute();
  105.  
  106.         $result = $stmt->get_result();
  107.         while ($rows = $result->fetch_array(MYSQLI_ASSOC)) {
  108.             $el = $rows['Producator'] . ' ' . $rows['Procesor'] . ' ' . $rows['Memorie'] . ' ' . $rows['HDD'] . ' ' . $rows['PlacaVideo'] . '<br>';
  109.             array_push($videoCards, $el);
  110.         }
  111.     }
  112.  
  113.     if (sizeof($prods) > 0) $allEl = array_intersect($allEl, $prods);
  114.     if (sizeof($processors) > 0) $allEl = array_intersect($allEl, $processors);
  115.     if (sizeof($memories) > 0) $allEl = array_intersect($allEl, $memories);
  116.     if (sizeof($hdds) > 0) $allEl = array_intersect($allEl, $hdds);
  117.     if (sizeof($videoCards) > 0) $allEl = array_intersect($allEl, $videoCards);
  118.  
  119.     foreach ($allEl as $el) {
  120.         $elements .= $el;
  121.     }
  122.     $con->close();
  123. }
  124.  
  125. echo $elements;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement