I have in my database 40 items and I want to display 6 on every page. Please tell me what I did wrong in this script because shows me only one page in the end.

<?php include 'config.php'; $per_page = 6; // 1 - if the uer didn't click on any page then show page 1 $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; // we start from 0 so we put -1 to start from 0 // page 1: 0-1 * 6 = 0 // page 2: 2-1 * 6 = 6 $start = ($page - 1) * $per_page; $query = $db->query("SELECT `bancuri`.`categorie` FROM `bancuri` LIMIT $start, $per_page"); $pages_query = $query->rowCount(); $results = ceil($pages_query / $per_page); echo $results; $query->execute(); while ($row = $query->fetch(PDO::FETCH_ASSOC)) { echo '

' . $row['categorie'] . '

'; } if ($results >= 1) { for($x = 1; $x <= $results; $x++) { echo '' . $x . ' '; } } echo '

Returned ' . $query->rowCount() . ' results


Does it always show you a specific page regardless of what the value of $_GET[‘page’] is, if so, which page does it show you? If you echo $start and $page before the query, what values do you get? And what value does it show for $results?

echo $page // shows 1
echo $start //shows 0
echo $result // shows 1

you can put this script in a page change the select and you can see it.
it is a simple select, selecting the categories from a table.

I’m confused at the $query->execute() line there - isn’t that just for a prepared statement? I don’t know what it would do here when you’ve already executed the query further up.

I know I can copy/paste it locally, but by the time I’ve changed it to use a table and database on my system I’m not likely to get the same results. What, exactly, do you get when you run the code and how does it differ from what you expect? I’m not clear on that, you said “it shows only one page in the end” but I don’t know which page it shows you, or do you mean it only shows one row rather than one page?

(You also want a check for $page being in range at some point, but that’s a minor thing).

OK, I’ve tried something similar on a test database and can’t see a problem - it returns six results, and if I add ?page=2 on the end it returns a different set of six results using the limit clause. I can’t see what the idea of the for loop at the bottom is for - as $pages_query will never be larger than $per_page (because your query limits it to six results), won’t this always only display a single value, if anything?

I want to see in my page the pagination where I put this line

echo ‘’ . $x . ' ';

but doesn’t show up pagination. Instead I get only 1 page. I want to see 1 2 3 etc.
execute I forgot to delete it :smile:
I know that if I put in the link ?page=1 or ?page=2 is gonna show me diff values, but i want to see this in numbers where I put that echo for $x.

I see. The problem there is that because your query only selects six rows (because of the LIMIT clause), the maximum value for $pages_query will be six, the maximum value for $results will be 1 because 6/6 is 1. You need to do a query first to count all the categories, use the rowcount from that to divide your per_page variable into for pagination.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.