Pagination with a search query array php

Codes here !

Moderators: egami, macek, gesf

Post Reply
Dewies12
New php-forum User
New php-forum User
Posts: 1
Joined: Wed Aug 05, 2020 12:15 pm

Wed Aug 05, 2020 12:19 pm

I want to include pagination on my results page so that the amount of items is reduced on a page. Although I receive my search criteria $_POST['submit'] button from my index page and then build up a query according to the search inputs to another page where the items are displayed. Thus I want to display a certain amount of items, and continue to the next bunch when I click the page-link. Here is my code.

Code: Select all

<section class="page-section categories-page">
        <div class="container">
            <div class="row">
                <?php 
                    if(isset($_POST["search"]))
                    {
                        $output = '';
                        $limit = 4;
                        $page = 1; 

                        $item1= htmlspecialchars ($_POST["item1"]);
                        $item2= htmlspecialchars ($_POST["item2"]);
                        $sql = "";
                        $sql .= "SELECT table1.*, table2.* FROM table1,table2 WHERE table1.id = table2_id";

                        if(isset($_POST['item1'])) 
                        {
                            if($item1== '' || empty($item1)) 
                            {
                                $sql .= " AND item1 != ' '";
                            }
                            else
                            {
                                $sql .= " AND item1 LIKE '".$item1."%'";
                            }
                            
                        }
                        if(isset($_POST['item2'])) 
                        {
                            if($item2== '0'|| empty($item2)) 
                            {
                                $sql .= " AND item2 != '0'";
                            }
                            else
                            {
                                $sql .= " AND item2 LIKE '".$item2."%'";
                            }
                            
                        }

/* HERE IS THE REST OF THE $sql . = Queries the same idea as above */


                        if(isset($_GET['page']) && $_GET['page']!="") 
                        {
                            $page = $_GET['page'];
                        } 
                        else 
                        {
                            $page = 1;
                        }
                        $start = ($page - 1) * $limit;  
                        
                        $sql .= " GROUP BY table1.id LIMIT $start,$limit";

                        $result =mysqli_query($db,$sql);
                        
                        if(mysqli_num_rows($result) > 0)  
                        {   
                            $output .= '
                            <div class="col-xl-12">
                                <div class="section_title mb-40 text-center">
                                    <h4>
                                        '.mysqli_num_rows($result).'Found
                                    </h4>
                                    </br></br>
                                </div>
                            </div>';
                            while($row = mysqli_fetch_array($result))  
                            {  
                                $id = ltrim($row['id'],'#');
                                $output .= '
                                <div class="col-lg-4 col-md-6">
                                    <!-- feature -->
                                    <div class="feature-item">
                                            <div class="feature-pic set-bg" data-setbg="img/property/'.$row["photoitem"].'"">                                               
                                                <div class="feature-pic set-bg" data-setbg="img/property/'.$row["photoitem"].'"">                                       
                                                    '.$item.'
                                                </div>
                                            </div>
                                        </a>
                                        <div class="feature-text">
                                            <div class="text-center feature-title">
                                                <h5>'.$row["item"].'</h5>
                                                <p><i class="fa fa-map-marker"></i>'.$row["item"].','.$row["item"].','.$row["item"].'</p>
                                            </div>
                                            <div class="room-info-warp">
                                                <div class="room-info">
                                                    <div class="rf-left">
                                                        <p><i class="fa fa-th-large"></i> '.$row["item"].' m2</p>
                                                        <p><i class="fa fa-bed"></i> '.$row["item"].'</p>
                                                    </div>
                                                    <div class="rf-right">
                                                        <p><i class="fa fa-car"></i> '.$row["item"].'</p>
                                                        <p><i class="fa fa-bath"></i> '.$row["item"].' </p>
                                                    </div>  
                                                </div>
                                                <div class="room-info">
                                                    <div class="rf-left">
                                                        <p><i class="fa fa-user"></i>'.$row["id"].'</p>
                                                    </div>
                                                    <div class="rf-right">
                                                        <p><i class="fa fa-clock-o"></i>'.$row["item"].'</p>
                                                    </div>  
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    </div> 
                            ';
                                
                            
                            }
                            
                        }
                        
                        else  
                        {  
                            $output .= '<div class="section_title mb-40 text-center">
                                            <h3>Data not Found</h3>
                                        </div>';  
                        }  
                        $output .= '<br/><div align="center"> ';
                        
                        
                        echo $output;
                    }
                    
            ?>
            
            
            <?php
                include_once 'php/config.php';
                $page_sql = "SELECT table1.* , table2.* FROM table1, table2 WHERE table1.id = table.id GROUP by table1.id DESC";
                $page_result = mysqli_query($db,$page_sql);
                $total_records  = mysqli_num_rows($page_result);
                $total_pages = ceil($total_records/$limit);   
                for($i = 1; $i <= $total_pages; $i++)
                {
                    echo "<div class='pagination'>
                            <a class='page-link' href='page.php?page=".$i."' id='".$i."'>".$i."</a>
                            ";
                }
            ?>
            </div>
            
        </div>
    </section>

How can I go to the next page when I click on a page-link? I have tried Jquery as well, although it has to load the same values again according to the search on the results page.How do I send the query search results to the page each time I click the page-link Thank you.
Post Reply