tags:

views:

37

answers:

1

Hi,

Was wondering if it's possible to run/re-run a mysql query when a button/link is pressed/clicked?

Basically, I run a query that limits the results to the first 7, with a button below the results to show more results. When the button is clicked/pressed, I need to re-run the query and set the limit to, say 20.

Any help/advice on how to do this would be greatly appreciated. Thanks in advance, S.

+2  A: 

The code to run the query and produce the results is on the server and the button is on the browser so you need some way to communicate back to the server that you want more results. You can either use an Ajax call and update your page's contents dynamically or just make it a link and GET another page with more results on.

You already have code that returns 7 results and show them on a page - let's say your page to show the seven is www.blah.com/show. You could add a parameter to your page that accepts the number of results to show, e.g. www.blah.com/show?num=7. Now instead of hardcoding the SQL to read seven results, you first read the parameter and use that number in your query. The link to show more results can now be <a href="www.blah.com/show?num=20">Show more</a>

The Ajax option could be similar, but instead of using a <a> to retrieve the new page from the server you would call it using (in jQuery) $.ajax() and would insert the results into your page. This is slightly more complicated than the first example.

Steve Claridge
Brilliant, many thanks for the advice. Have decided to go with Ajax and have customised this - http://www.9lessons.info/2009/12/twitter-style-load-more-results-with.html
ss888