Click here to Skip to main content
15,888,126 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I populated a html table this way:
sqlquery = ...
echo "<table width='100%'><tr>";
 if (mysql_num_rows($result)>0)
 {
         //loop thru the field names to print the correct headers
         $i = 0;
         while ($i < mysql_num_fields($result))
         {
               echo "<th>". mysql_field_name($result, $i) . "</th>";
                  $i++;
           }
           echo "</tr>";

...
 echo "<tr>";
                 foreach ($rows as $data)
                    {
                        echo "<td align='center'>". $data . "</td>";
...

The problem is: in the page I have that table I want to appear only ten rows (ten results), and then I have below a 1,2, .... next page and in that section where table is, appears next 10 results (rows).

How can I accomplish that ? I thing I have to use JS but I dont know how to do it, so I really appreciate your help.
Posted
Updated 7-Jan-11 1:18am
v3

You can use SQL to do this:

SELECT * FROM `table` LIMIT 0, 10 

will result in rows 1 to 10

SELECT * FROM `table` LIMIT 10, 20 

will result in rows 11 to 20

Of course you need to keep track of the current position so you send it back when the user clicks on next or previous.

[Edit]

But you would not leave the page but reload is. User is at: www.site.com/data.php?start=0 and for example redirected to www.site.com/data.php?start=10 when clicking next. The user is still om the same page but loads with another set of data. Another way to do something like that is using AJAX. This means that you reload a some part of the page asynchronously without reloading the complete page.

Have a look here for this:
w3cschool: http://www.w3schools.com/ajax/default.asp[^]
ajax php: http://www.tizag.com/ajaxTutorial/ajaxphp.php[^]

[/Edit]

Good luck!
 
Share this answer
 
v3
Comments
Paula Freire 7-Jan-11 7:32am    
But my problem is not the SQL.. is the next and previous button.

because I want to select "next 10 results", mas not leaving the page.

Any ideas how to do that ?
E.F. Nijboer 7-Jan-11 9:21am    
But you would not leave the page but reload is. User is at: www.site.com/data.php?start=0 and for example redirected to www.site.com/data.php?start=10 when clicking next. The user is still om the same page but loads with another set of data.
Another way to do something like that is using AJAX. This means that you reload a some part of the page asynchronously without reloading the complete page. Have a look here for this:
w3cschool: http://www.w3schools.com/ajax/default.asp
ajax php: http://www.tizag.com/ajaxTutorial/ajaxphp.php
thatraja 8-Jan-11 2:54am    
@E.F. Nijboer : Hi update the things in your answer & notify enquirer(instead of pasting your valuable explanations in comments). I'll update now for demo :-). cheers.
Dalek Dave 9-Jan-11 14:26pm    
Excellent answer.
You can do it directly in the database:

SQL
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr, ...
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name']
[FROM table_references
[WHERE where_definition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_definition]
[ORDER BY {col_name | expr | position}
[ASC | DESC] , ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}][PROCEDURE procedure_name(argument_list)]
[FOR UPDATE | LOCK IN SHARE MODE]]


You can page the results in function of the current page:

Example:

int page = 1;
int maxrows = 10;

"SELECT *
FROM table
WHERE 1=1
LIMIT " & 1 * page & ", " & maxrows * page 6 ";";


This code is paging the results in groups of 10 rows.
 
Share this answer
 
Comments
Paula Freire 7-Jan-11 7:33am    
But cant I do the many "10 table rows" in the same page?

Because with your solution looks like I must have different pages.
adocas 7-Jan-11 7:47am    
If you can't change the page, you must implement some Javascript code that gets some lines when you're showing bottom lines...
You may pass the data in XML to Javascript, you can look at JQUERY or JSON libraries.
I can't help you with this at the moment.
Dalek Dave 9-Jan-11 14:26pm    
Excellent Answer.
I wrote a Tip/Trick to do this last year.

Check it out - Dynamic Pagination[^]
 
Share this answer
 
Comments
Dalek Dave 9-Jan-11 14:26pm    
Good Link.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900