Jump to content
Lykos22

Create pagination links and function

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???

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

If you're using a function like count, either don't return any other columns or group by every other column.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...