Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

Pagination and search box in blog using PHP and MYSQL | Part 12


Pagination and search box in blog using PHP

Here we are learning a complete blog CMS system from the beginning tutorial "how to create a blog from scratch in PHP". In this tutorial, we will create Pagination and a search box using PHP PDO. Let us first understand what is Pagination and search system.

What is pagination?

Pagination is a method by which the data of the database is displayed on different pages, which increases the page speed significantly. The retrieval of all data from the MYSQL database on separate pages is called pagination.
In Pagination, buttons are created for different pages, by which the data is fetched on all the pages within a limit. Pagination in the blog is used to display blog posts within a limit.

What is the search box?

The search box is used to search for keywords. Just like any keyword is accessed by searching on Google.com, a search system is made by using PHP and MySQL.

In the blog, the search system is used to find out the desired content. In this tutorial, we will implement the pagination and search system simultaneously.

define("PER_PAGE_LIMIT",2); //Set blog posts limit 


In the code line above -
Let us first set the limit of the page using the define function. Here, we are not using a variable but creating constantly. The define function is not required to declare a variable, but the define function is used to declare the constant. Learn more about Constant.

$search_query = 'SELECT * FROM  techno_blog WHERE articleTitle LIKE :keyword OR articleDescrip LIKE :keyword OR articleTags LIKE :keyword OR articleContent LIKE :keyword ORDER BY articleId DESC '; 


This is the next step to fetch all data from the blog table and perform the search query using the WHERE clause.
In the code above, we create a search query to find out the related keyword. Here we are using title, description, tags, and content. Whenever a user searches a keyword, this query will display the content related to that keyword.
The search query will check that keyword in title, description, tags, and content.

How to create pagination in PHP?

 $per_page_item = '';
    $page = 1;
    $start=0;

In the code lines above, we declare and define the three variables. These variables will be used in pagination.

Let’s discuss the main points only .

if($page_count>1) {
            for($i=1;$i<=$page_count;$i++){
                if($i==$page){
                    $per_page_item .= '';
                } else {
                    $per_page_item .= '';
                }
            }
        }
 

In the code above , we create a condition using if condition that complete rowcount is less than 1 then execute the block of code .
We create two buttons. The first button for the currently active button and a second button for normal button with the page number.


The number of the button will be displayed with a unique number. If a user clicks one button, then that button will be activated.
It is a working process of pagination in the blog. Every page fetches the next blog posts within the limit.

else{ 
    echo "No results found for ". $searching;
    }
 

This is the best part of this searching system in PHP. If a user searches for some keywords and finds related content from the keyword, then the related content is displayed. If there is no content match with the searched keywords, then other code lines are executed and display the message " No results found for searched keywords".

Implement the complete pagination and search system in the blog .
Open your index.php file and change the complete code to the code below.

Complete PHP pagination and search system source code –

Let's implement the complete pagination and search system in blog using PHP and MYSQL.Use the complete source code below.

blog/index.php

 
<?php 
//connection File 
require_once('includes/config.php'); ?>
<?php 
//include head file for language preference 
include("head.php");  ?>
	Techno Smarter Blog
    

	<?php 
//header content //navbar 
    include("header.php");  ?>

<?php define("PER_PAGE_LIMIT",2); //Set blog posts limit $searching = ''; if(!empty($_POST['search']['keyword'])) { $searching = $_POST['search']['keyword']; } /* PHP Blog Search*/ $search_query = 'SELECT * FROM techno_blog WHERE articleTitle LIKE :keyword OR articleDescrip LIKE :keyword OR articleTags LIKE :keyword OR articleContent LIKE :keyword ORDER BY articleId DESC '; /* PHP Blog Pagination*/ $per_page_item = ''; $page = 1; $start=0; if(!empty($_POST["page"])) { $page = $_POST["page"]; $start=($page-1) * PER_PAGE_LIMIT; } $limit=" limit " . $start . "," . PER_PAGE_LIMIT; $pagination_stmt = $db->prepare($search_query); $pagination_stmt->bindValue(':keyword', '%' . $searching . '%', PDO::PARAM_STR); $pagination_stmt->execute(); $row_count = $pagination_stmt->rowCount(); if(!empty($row_count)){ $per_page_item .= '
'; $page_count=ceil($row_count/PER_PAGE_LIMIT); if($page_count>1) { for($i=1;$i<=$page_count;$i++){ if($i==$page){ $per_page_item .= ''; } else { $per_page_item .= ''; } } } $per_page_item .= "
"; } $query = $search_query.$limit; $pdo_stmt = $db->prepare($query); $pdo_stmt->bindValue(':keyword', '%' . $searching . '%', PDO::PARAM_STR); $pdo_stmt->execute(); $result = $pdo_stmt->fetchAll(); ?>
<?php if(!empty($result)) { foreach($result as $row) { ?> <?php //Blog Title echo '

'.$row['articleTitle'].'

'; echo '
'; //Blog post Date and Time echo '

Posted on '.date('jS M Y H:i:s', strtotime($row['articleDate'])).' in '; $stmt2 = $db->prepare('SELECT categoryName, categorySlug FROM techno_category, techno_cat_links WHERE techno_category.categoryId = techno_cat_links.categoryId AND techno_cat_links.articleId = :articleId'); $stmt2->execute(array(':articleId' => $row['articleId'])); $catRow = $stmt2->fetchAll(PDO::FETCH_ASSOC); $links = array(); foreach ($catRow as $cat){ $links[] = "<a href='category/".$cat['categorySlug']."'>".$cat['categoryName']."</a>"; } echo implode(", ", $links); echo '

'; echo '

Tagged as: '; $links = array(); $parts = explode(',', $row['articleTags']); foreach ($parts as $tags) { $links[] = "<a href='tag/".$tags."'>".$tags."</a>"; } echo implode(", ", $links); echo '

'; echo '
'; echo '

'.$row['articleDescrip'].'

'; echo '

'; ?> <?php } } else{ echo "No results found for ". $searching; } ?>
<?php echo $per_page_item; ?>
<?php //Sidebar Content include("sidebar.php"); ?>
<?php //footer content include("footer.php"); ?>

In this way, you can use pagination and search box for other pages.


Please Share

Recommended Posts:-