Paging with PDO
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?
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.
$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);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$rowcount = count($rows);
Since you're doing a fetchAll(), you could just do an array_slice() on the resulting array to get the desired page data, e.g.:
$pageData = array_slice($rows, ($pageNbr - 1) * $numPerPage, $numPerPage);
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Hi, thanks for your reply. I checked array_slice on PHP manual understood what you mean.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)