How to make pagination in php

Learn how to create pagination in PHP with an example. Understand how to set up your code to divide content into multiple pages.

Pagination in PHP

Pagination is an important technique used to split a large amount of content into smaller and easier to digest chunks. It is a very useful tool for web developers and webmasters, as it makes it easier for visitors to navigate through a website. In this tutorial, we will learn how to create a pagination system in PHP.

Step 1: Setting Up the Database

Before we can begin creating our pagination system, we need to set up a database. For this example, we will be using a MySQL database.

CREATE TABLE posts (
	id INT AUTO_INCREMENT,
	title VARCHAR(50),
	content TEXT,
	PRIMARY KEY (id)
);
This query will create a table called “posts” with the following columns:
  • id – The id of the post.
  • title – The title of the post.
  • content – The content of the post.
Once the table has been created, we can insert some data into it. For this example, we will be inserting 10 posts into the database.

INSERT INTO posts (title, content) VALUES 
('Post 1', 'This is the content of post 1'),
('Post 2', 'This is the content of post 2'),
('Post 3', 'This is the content of post 3'),
('Post 4', 'This is the content of post 4'),
('Post 5', 'This is the content of post 5'),
('Post 6', 'This is the content of post 6'),
('Post 7', 'This is the content of post 7'),
('Post 8', 'This is the content of post 8'),
('Post 9', 'This is the content of post 9'),
('Post 10', 'This is the content of post 10');

Step 2: Creating the Pagination Function

Now that we have our database set up, we can begin creating our pagination function. The function will accept two parameters: the page number and the number of posts per page.

function get_posts($page, $posts_per_page) {
	// Connect to the database
	$conn = mysqli_connect('host', 'username', 'password', 'database');

	// Calculate the offset
	$offset = ($page - 1) * $posts_per_page;

	// Query the database
	$sql = "SELECT * FROM posts LIMIT $offset, $posts_per_page";
	$result = mysqli_query($conn, $sql);

	// Fetch the posts as an array
	$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);

	// Free the result
	mysqli_free_result($result);

	// Close the connection
	mysqli_close($conn);

	return $posts;
}
This function will return an array of posts that are limited to the specified page number and number of posts per page.

Step 3: Displaying the Posts

Now that we have our pagination function, we can use it to display the posts. We can do this by looping through the array returned by our function and displaying the posts.

$posts = get_posts(1, 10);

foreach($posts as $post) {
	echo '

' . $post['title'] . '

'; echo '

' . $post['content'] . '

'; }

Step 4: Creating the Pagination Links

The last step is to create the pagination links. We can do this by creating a function that calculates the total number of pages and generates the links.

function generate_links($page, $posts_per_page) {
	// Connect to the database
	$conn = mysqli_connect('host', 'username', 'password', 'database');

	// Query the database
	$sql = "SELECT COUNT(*) AS num_posts FROM posts";
	$result = mysqli_query($conn, $sql);

	// Fetch the result
	$row = mysqli_fetch_assoc($result);

	// Calculate the number of pages
	$num_pages = ceil($row['num_posts'] / $posts_per_page);

	// Generate the links
	$html = '';
	for($i = 1; $i <= $num_pages; $i++) {
		if($i == $page) {
			$html .= '' . $i . '';
		} else {
			$html .= '' . $i . '';
		}
	}

	// Free the result
	mysqli_free_result($result);

	// Close the connection
	mysqli_close($conn);

	return $html;
}
This function will return a string of HTML links for the pagination. We can then display the links in our page.

echo generate_links(1, 10);

Conclusion

In this tutorial, we learned how to create a pagination system in PHP. We set up a database and created a function to retrieve the posts from the database. We also created a function to generate the pagination links. With this system, we can easily paginate our content and make it easier for visitors to navigate our website.

Answers (0)