0

I am new to web development (php/html/sql) so I am trying to learn by example. I have decided to write a simple php script that reads a mysql table and renders it to HTML. This HTML is echoed back to a jquery function that will display the html into a div.

This ALL works fine and as expected.

Now what I am trying to do is to have a table with a maximum of 20 rows in it and if the sql query returns 60 rows then I need to see 1 table of 20 rows on my website and an index (1,2,3...) below that table and by clicking those number I would see the extra results.

Any idea how I can achieve this? any tutorials? idea?

The solution I'm thinking of is the folowing:

in my php script I check the numbers of rows returned by mysql query then i create multiple "20 rows tables" (so if mysql returned 60 rows then I will render 3 x 20rows tables). then I put those tables into an array and I echo this. Now jquery will receive this array and render only the first table in the div.

But then my problem is that how can I have the rest of the data? so that when someone clicks on "2" below the table he can see the rest of the results?

Ahh! I hope I was clear.

Thank you!

Kam
  • 5,878
  • 10
  • 53
  • 97

1 Answers1

1

just limit the SQL query to only return 20 rows by adding LIMIT 0,20 to the end of the query.

you can generate the 0 and 20 via php to implement pagination by doing something such as

$max_per_page = 20;
$page = 1;
$limit_start = ($page*$max_per_page)-$max_per_page;

$sql = "..... LIMIT {$limit_start},{$max_per_page}";
bizzehdee
  • 20,289
  • 11
  • 46
  • 76
  • But I still want the user to be able to access the rest of the results – Kam Feb 21 '13 at 22:11
  • then cycle through all the results, storing each result row in an array, and then use a for loop to look at the first/last/middle 20 rows or however many you wish to offset from – bizzehdee Feb 21 '13 at 22:13
  • yes exactly! my question is how can I do that? when my php echos back the array. my jquery funtion receivs it, for now it only will display the first array (i.e. table ) in a div but what should I do with the rest of the array – Kam Feb 21 '13 at 22:14
  • Are you using ajax to fetch the results? if not. why? all you would have to do is echo the results $ HTML in your php script and jquery would take care of the rest – Nick Feb 21 '13 at 22:18
  • I'm confused :$ ok this is what I have right now. a php script that retreived 60 rows. so created 3 tables in html and put them in an array. Then that array is echos back using jason encoding and retrieved by jquery as function(result){//do something}. I can easilty take result.table1 and display it in a div. But what can I do (or how can I save result.table2 and result.table3 for later use?) so that when user selects "2" below the first table he would see table2? – Kam Feb 21 '13 at 22:22
  • hmmm, if i think i understand you right, run 3 ajax queries, get the three tables, put each table in a div, then hide the div the tables are in until the user clicks to see the tables? – Nick Feb 21 '13 at 22:38
  • @Nick yes I can do that. Is this the usual way of implementing pagination? because I see this feature almost everywhere we have tabulated data. – Kam Feb 22 '13 at 15:15
  • @Kam "is this the usual way" not too sure but in the end, "does it matter?" is a better question. As a programmer always try to think outside of the box. Theres more than one way to do anything and often you will find a way that is more productive than the "norm". – Nick Feb 22 '13 at 15:17
  • @Nick Please provide the answer you proposed as an answer so I accept it. – Kam Feb 22 '13 at 15:32
  • As the topic is closed by mods, i cant post another answer, just give the credit too bizzehdee. no big deal. – Nick Feb 22 '13 at 15:38