Jump to content

Create pagination links and function


Lykos22

Recommended Posts

Hi, I'd like some help please. I'm trying to create a pagination that will display the links of the pages like this:<a href="#">First Page</a><a href="#">Previous Page</a><a href="#">Next Page</a><a href="#">Last Page</a>Not showing numbers Here's my code so far:

<?php$per_page = 20;$page = (isset($_GET['page']) === true) ? (int)$_GET['page'] : 0;$start = $page * $per_page; $query = "SELECT `users`.`user_id` AS `id`,	`users`.`first_name` AS `firstname` ,	`users`.`last_name` AS `lastname`,	`users`.`email` AS `email`,   FROM `users` ORDER BY `users`.`user_id` DESC"; $query_limit = sprintf("%s LIMIT %d, %d", $query, $start, $per_page);$sql = mysqli_query($connection, $query_limit) or die(mysqli_error());$row_sql = mysqli_fetch_assoc($sql); $all = mysqli_query($connection, $query);$total_rows = mysqli_num_rows($all); $total_pages = ceil($total_rows/$per_page)-1;//$previous_page = ($page > 0) ? $page - 1 : 0;//$next_page = $page + 1;?><html>....<?php do { ?><td><?php echo $row_sql['firstname']; ?></td><td><?php echo $row_sql['lastname']; ?></td><td><?php echo $row_sql['email']; ?></td><?php } while ($row_sql = mysqli_fetch_assoc($sql)); ?>...<!--- pagination links go here  ---> 

Is it also possible to put it and inside a function where I can pass the $query and the $per_page so that I can re-use it???

Link to comment
Share on other sites

You can put it in a function if you want, there's no reason why you wouldn't be able to. You can also print your links however you want, you just need to send the page number in the URL. The text for the links can be whatever you want it to be.

Link to comment
Share on other sites

I have made some changes to my code, so it looks like this:

<?phpfunction total_rows_db($db_table, $db_param){    global $connection;        $query = "SELECT COUNT(`{$db_param}`) AS `total` FROM `{$db_table}` ";    $sql = mysqli_query($connection, $query) or die(mysqli_error());    $total_rows = mysqli_fetch_assoc($sql)['total'];    return $total_rows;}$per_page = 3;$page = (isset($_GET['page']) && $_GET['page'] ) ? (int)$_GET['page'] : 1;$start = ($page - 1) * $per_page; //offset $total_rows = total_rows_db('users','user_id'); $query = "SELECT `users`.`user_id` AS `id`, `users`.`first_name` AS `firstname` , `users`.`last_name` AS `lastname`, `users`.`email` AS `email`, FROM `users` ORDER BY `users`.`user_id` DESC ";$query .= " LIMIT {$start}, {$per_page}";$sql = mysqli_query($connection, $query) or die(mysqli_error($connection));$row_sql = mysqli_fetch_assoc($sql);?><html>....<?php if($total_rows > 0): ?><?php do { ?><p><?php echo $row_sql['firstname']; ?></p><?php } while ($row_sql = mysqli_fetch_assoc($sql)); ?><?php endif; ?>

I tried though to use a single query for this, like this:

$query = "SELECT COUNT(`users`.`user_id`) AS `total`, `users`.`user_id` AS `id`, `users`.`first_name` AS `firstname` , `users`.`last_name` AS `lastname`, `users`.`email` AS `email`, FROM `users` ORDER BY `users`.`user_id` DESC ";

so not to use the function. Although the query runs (no syntax errors) the count in this case returns a different number which is not correct, e.g if i have 1 user in my database, the count returns 4 as total.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...