Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2013, 02:10 PM   PM User | #1
telmessos
Regular Coder

 
Join Date: Apr 2006
Posts: 127
Thanks: 6
Thanked 0 Times in 0 Posts
telmessos is an unknown quantity at this point
Paging with PDO

Hi,

I have an MySQL query which is seems to work fine with a PDO execute and I want to make paging to the results. I want to show the total number of results also. If I make paging by adding "limit :rownumber, :rowperpage" to the end of the query, I have to execute another query to get the total count number. If I don't, I don't know how to make paging. Any ideas?

Code:
$searchquery = "Select propertycode,title,price,currency,altbolgeadi,bolgeadi,description,bedrooms,bathrooms from urunler where bolgeID=:location and altbolgeID=:sublocation and price between :pricemin and :pricemax limit :sayfa, :gosterim";
$stmt = $db->prepare($searchquery);
$stmt->execute($degerler);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$rowcount = count($rows);
In above code, if I keep the limit section $rowcount becomes the limit given to MySQL. If I don't, I don't know how to make paging with the $rows results.

Many thanks
telmessos is offline   Reply With Quote
Old 02-08-2013, 02:55 PM   PM User | #2
TFlan
New Coder

 
Join Date: Dec 2012
Location: USA
Posts: 82
Thanks: 3
Thanked 17 Times in 17 Posts
TFlan is an unknown quantity at this point
Just use math to manipulate the row index you are trying to achieve (i believe this is what you're trying to do).

So if your limit $startLim, $endLim, for example $startLim = 30; $endLim = 50, that gives you 20 rows, to get $i to start at 30, $i = $startLim - which lets you use loops easier

Again - Not sure what your problem is
TFlan is offline   Reply With Quote
Users who have thanked TFlan for this post:
telmessos (02-08-2013)
Old 02-08-2013, 04:43 PM   PM User | #3
telmessos
Regular Coder

 
Join Date: Apr 2006
Posts: 127
Thanks: 6
Thanked 0 Times in 0 Posts
telmessos is an unknown quantity at this point
Thanks for your reply. So correct me if I misunderstood. I must create a counter variable increasing by 1 at every step of the loop. When the counter number matches the page number, it starts writing on the page until the numberofrows per page number reached. This seems like a good way to do it.

But there's also a concern of performance. Let's say if the total number of records are 200, all of them will be brought from MySQL which is a cost. On the other side if I add a limit to the end of the SQL query, only the rows per page will be displayed.

So still could not decide. Anyway thanks again for your response.
telmessos is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:10 PM.


Advertisement
Log in to turn off these ads.