You are invited to Log in or Register a free Frihost Account!

paging sql result

I'm working on a guestbook, and it is allmost finishes Very Happy (there is something to start with to learn php) but now I want to split my dql query in multiple pages, I'ved searched for alot of tuturials but all where not usefull for me Sad or I didn't understand them or they did not work perfect.
can anyone give me a good tuturial on how to split my query in multiple pages with next and previous links?
thanks alot
Hm.. as a developer I prefer not writing tutorials or reading them. Just need some points that can show me the road. I'll do it exactly like that. You should find out yourself and use your ideas.

So here's some way:
1. Get the number of all records in the SQL
2. Divide that number to a posts per page (PpP). You'll get the how much total pages you have (ex. 120 records in sql / 60 PpP = 2 pages)
3. Use the GET variable &page= ($page) to see which page the user choosed
4. For getting the sql records which are for choosed $page use the LIMIT X,Y feature. The X shows from which record to start, the Y shows how much records to show. We know that Y is the PpP. The X is coming in this way: $page * PpP. (ex. 2*60 = 120). In our whole example the real pages are starting from 0 not from 1. So the formulae should be ($page-1)*PpP. Now we get the value of 60 and sql query will finish with LIMIT 60,60 i.e. Start showing me the posts from 60 (1st) and show me the next 60 (2nd) Razz

I know... hard. Really hard to understand. But if you experience difficulties.. just tell me.. will try to make a code for you.

Good luck!
That is a good solution.
I use the adres-line (so:
I just read all the data in one large array and then use a loop to read the data. In the loop the decission is made wich part has to be used.
For example (not encoded, because I'm used to Perl instead of PHP):

read standard in
decide wich value has the number
store this in $number
$end is $number + 10 (to display only the first 10 messages)
select * from the database, store it in @data
($htmltext contains the actual output)

$count = 1
while $count =< $endnumber do this loop
($temp,@data) = @data
if $count >= $number
$htmltext .= $temp

$numberback=$number -10 and $numberforward=$number+10

next, you create a backbutton with value$numberback and a forwardbutton with$numberforward
This works if you write it in PHP

That's a solution too! It's about personal style of coding... That's why I didn't gave a code as solution, but explained a way to do it.

2nd: I prefer not to use SQL as "storage cabinet" Wink If any calculation can be made while getting the SQL results, I let them SQL handle it. Just trying to use the power of SQL, that makes my life (and script's one) eaiser. BTW, that way of coding came since I've made an MMPORG game server. Yikes! You can't imagine how much time I've spend in making the servier lighter. Razz

Kindly regards,
The Elf
Related topics
SQL Basics
A very good PHP MySQL Tutorial
mysql gives the wrong result [solved]
Error trying to display SQL statement in html table
SQL Problem Help
help, SQL Stored procedure - ASTM rouding rule
How to get result for this week
Strange SQL error
Subtracting one value from another from MySQL result
Prevent mysql error showing when code needs user input
PHP and Javascript Issue handling data from a mysql query
Too many re-directs problem?
Cannot find parse error
Array Update SQL script not working
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

© 2005-2011 Frihost, forums powered by phpBB.