Guest User

Untitled

a guest
Feb 16th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. <?php
  2. if(isset($_POST['a'])) {
  3. setcookie("prisCookie", $_POST['a'], time()+3600*48, "/");
  4. setcookie("areaCookie", $_POST['b'], time()+3600*48, "/");
  5. setcookie("lanCookie", $_POST['c'], time()+3600*48, "/");
  6. }
  7.  
  8. if (isset($_COOKIE)){
  9. foreach ($_COOKIE as $cookieName => $value) {
  10. $cookieName = htmlspecialchars($cookieName);
  11. $value = htmlspecialchars($value);
  12. $$cookieName = explode(',', $value);
  13. }
  14. }
  15.  
  16. /*foreach($_POST as $key => $value){
  17. echo $key . ' ' . $value;
  18. }*/
  19.  
  20. //echo "hej";
  21. //echo $_POST['b'];
  22. //echo $_POST['c'];
  23.  
  24. //$a = $_POST['a'];
  25. //$prisCookie = explode(',', $a);
  26. //$b = $_POST['b'];
  27. //$areaCookie = explode(',', $b);
  28.  
  29. $server = "mysql-2017.csc.kth.se";
  30. $username = "gidekull_admin";
  31. $password = "";
  32. $dbname = "gidekull";
  33.  
  34. //$conn = mysqli_connect($server, $username, $password, $dbname);
  35.  
  36. try {
  37. $pdo = new PDO("mysql:host=$server;dbname=$dbname;charset=utf8", $username, $password);
  38. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  39. }
  40. catch(PDOException $e)
  41. {
  42. echo "Misslyckad anslutning " . $e->getMessage();
  43. }
  44.  
  45. //echo $kvm[0] . " " .$kvm[1];
  46.  
  47. $sql_query = 'SELECT * FROM bostader WHERE lan = :lan0
  48. AND area BETWEEN :area0 AND :area1
  49. AND pris BETWEEN :pris0 AND :pris1
  50. ORDER BY ';
  51.  
  52. if(isset($_GET['sort']) && in_array($_GET['sort'], ['lan', 'objekttyp', 'adress', 'area', 'rum', 'pris', 'avgift'])){
  53. $sort = $_GET['sort'];
  54. } else {
  55. $sort = 'pris';
  56. }
  57. if(isset($_GET['asc_desc']) && in_array($_GET['asc_desc'], ['ASC', 'DESC'])){
  58. $ASC_DESC = $_GET['asc_desc'];
  59. } else {
  60. $ASC_DESC = 'ASC';
  61. }
  62.  
  63. $sql_query .= $sort . ' ' . $ASC_DESC;
  64.  
  65. //echo $sql_query . "<br>";
  66.  
  67. //echo $prisCookie[9] . " " . $areaCookie;
  68.  
  69. try{
  70. $statement = $pdo->prepare($sql_query);
  71.  
  72. $statement->execute(['area0' => $areaCookie[0], 'area1' => $areaCookie[1], 'pris0' => $prisCookie[0]*1000, 'pris1' => $prisCookie[1]*1000, 'lan0' => $lanCookie[0]]);
  73.  
  74. }
  75.  
  76. catch(Exception $e){
  77. echo $e->getMessage();
  78. }
  79. //TODO, encode with JSON. Skicka bara med kolumn. (funkar med)
  80.  
  81. $result = $statement->fetchAll(PDO::FETCH_ASSOC);
  82.  
  83.  
  84. foreach ($result as $key => $row) {
  85. foreach ($row as $column => $value) {
  86. if (in_array($column, ['pris', 'avgift', 'rum', 'area'])) {
  87. $result[$key][$column] = (float) $value;
  88. }
  89. }
  90. }
  91. echo json_encode($result, JSON_UNESCAPED_UNICODE);
  92. //echo json_encode($statement->fetchAll());
  93.  
  94.  
  95.  
  96. ?>
Add Comment
Please, Sign In to add comment