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

# paging sql result

golles
I'm working on a guestbook, and it is allmost finishes (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 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
Maryndor
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)

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!
johanfh
That is a good solution.
I use the adres-line (so: yoursite.com/mypage.php?number=0)
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):

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
wend

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

next, you create a backbutton with value yoursite.com/mypage.php?number=\$numberback and a forwardbutton with yoursite.com/mypage.php?number=\$numberforward
This works if you write it in PHP

Succes!
JohanFH
Maryndor
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" 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.

Kindly regards,
The Elf