0

Is there a way to tweak the SELECT statement on the fly using a href or a button, without leaving the page?

On my page I'm retrieving data with this statement:

$sql= $db->prepare("
SELECT * FROM db
WHERE `some` = 'thing' AND mydate >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
ORDER BY `mydate` DESC");
$sql->execute();

What would be cool is if I could display the data in some other way by passing in a variable and at the same time refreshing the page. Let's say a href or button that says "View All"..

I'm thinking maybe this can be done with presetting a few $_GET variables on the page and fetching with a href/button, but I'm not sure where to begin.

CinCout
  • 9,486
  • 12
  • 49
  • 67
ChrVik
  • 315
  • 1
  • 2
  • 18
  • The subject of this question is very confusing. MySQL is not HTML or PHP to write buttons.... – Marcos Pérez Gude Mar 08 '16 at 12:47
  • 1
    I would recommend looking into jQuery's `.ajax()` – rybo111 Mar 08 '16 at 12:48
  • 2
    Ajax is the solution, Refer [This](http://api.jquery.com/jquery.post/) and [that](https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&cad=rja&uact=8&ved=0ahUKEwiIydSRkLHLAhWDRI4KHbQVAnoQFggnMAI&url=http%3A%2F%2Fwww.w3schools.com%2Fjquery%2Fjquery_ajax_intro.asp&usg=AFQjCNEXWxvcrTm_pPwmqGQcwMiQuRDe5A&sig2=SEKyj4JMCM8JfXT1gxEn3Q) maybe [this](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) also – Sandeep Mar 08 '16 at 12:50
  • Thanks everybody, will take a look at AJAX! – ChrVik Mar 08 '16 at 13:00

2 Answers2

0

As suggested above by others, I would recommend you to use AJAX. In order to work with AJAX, you need to have a page normally a controller method or a stand alone script in any programming language say PHP, Java etc. Then as you need on click of "View all" button, you make an AJAX call to that script or method, you can pass parameters to this method from your AJAX call, The server side script would fetch the data, which you can show/update on your webpage with help of .HTML method of JQuery. Details of AJAX method of jquery can be found here http://api.jquery.com/jquery.ajax/. Hope this helps.

Krishna
  • 1
  • 3
  • AJAX helps you to update data on a webpage without reloading its content. Leaning AJAX would be very helpful in future. – Krishna Mar 08 '16 at 15:07
0

If you want to get the solution without using AJAX, you can get the solution using $GET but page will reload.

First you need to create a form.

<html>
<head></head>
<body>
<form action="" method="GET">
   <input type="submit" name="submit">
</form>

<?php
if(isset($_GET['submit'])){
   $sql= $db->prepare("
   SELECT * FROM db
   WHERE `some` = 'thing' AND mydate >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
   ORDER BY `mydate` DESC");
   $sql->execute();
   $result = $sql->fetchAll();
   print_r($result);       //print your DB result
}else{
   echo "GET ERROR!!!";
}
?>
</body>
</html>

This will print your query result when you click the submit button...

Sabin Chacko
  • 713
  • 6
  • 17