Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9065399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:29:33+00:00 2026-06-16T16:29:33+00:00

I am trying to build a basic search but am getting a small code

  • 0

I am trying to build a basic search but am getting a small code issue here, i want to paginate my searched result set and this is what i have
1) an input field named “input”, a and within a form of post method
2) when user submits the form it calls search.php with the follow code bellow.

The problem

the pagination returns a properly paginated result set but when i move to next page all records in the search table are returned including those that do not meet the criteria in the where clause.

Here is the code: Let me know if it’s too much code here. I can paste somewhere else and provide a link;
Thanks guys

<?php

    $input = $_POST['input'];
    $categories = $_POST['category'];
    $state = $_POST['state'];

    $targetpage = "search.php";
    $limit = 3;

    //This query checks for data
    $qq = " SELECT * FROM classified where confirm='0' ";
    $qq = $db->prepare($qq);
    $qq->execute();

    $total_pages =$qq->rowCount();

    $stages = 3;
    $page = ($_GET['page']);
    if ($page){
        $start = ($page - 1) * $limit;
    } else {
        $start = 0;
    }

    $rows = $qq->fetchAll();
    if ($rows > 0){
        $q = " SELECT * FROM classified where confirm='0' ";
        if (!empty( $input)) {
            $q .= "AND title LIKE '%".$input."%' ";
        }
        if (!empty($_POST['search_category']) ) {
            $q .= "AND id_cat = ".$categories." ";
        }

        if (!empty($_POST['state']) ) {
            $q .= "AND id_state = ".$state." ";
        }
        $q .= "ORDER BY date DESC LIMIT $start, $limit ";
    }
    $q = $db->prepare($q);
    $q->execute();
    //// Just for testing purposes to see what's coming out
    print_r($q);

    /*** echo number of columns ***/
    $resultt = $q->fetchAll();
    // Initial page num setup
    if ($page == 0){
        $page = 1;
    }
    $prev = $page - 1;
    $next = $page + 1;

    $lastpage = ceil($total_pages/$limit);
    $LastPagem1 = $lastpage - 1;

    $paginate = '';
    if ($lastpage > 1) {
        $paginate .= "<div class='paginate'>";
        // Previous
        if ($page > 1){
            $paginate.= "<a href='$targetpage?page=$prev'>Previous</a>";
        } else {
            $paginate.= "<span class='disabled'>Previous</span>";
        }
        // Pages
        if ($lastpage < 7 + ($stages * 2)) { // Not enough pages to breaking it up
            for ($counter = 1; $counter <= $lastpage; $counter++) {
                if ($counter == $page) {
                $paginate.= "<span class='current'>$counter</span>";
                } else {
                    $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                }
            }
        } else if ($lastpage > 5 + ($stages * 2)) {// Enough pages to hide a few?
            // Beginning only hide later pages
            if ($page < 1 + ($stages * 2)) {
                for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) {
                    if ($counter == $page) {
                        $paginate.= "<span class='current'>$counter</span>";
                    } else {
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
            } else if ($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) { // Middle hide some front and some back
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) {
                    if ($counter == $page) {
                        $paginate.= "<span class='current'>$counter</span>";
                    } else {
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
                $paginate.= "...";
                $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
                $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
            } else { // End only hide early pages
                $paginate.= "<a href='$targetpage?page=1'>1</a>";
                $paginate.= "<a href='$targetpage?page=2'>2</a>";
                $paginate.= "...";
                for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) {
                    if ($counter == $page) {
                        $paginate.= "<span class='current'>$counter</span>";
                    } else {
                        $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";
                    }
                }
            }
        }
        // Next
        if ($page < $counter - 1) {
            $paginate.= "<a href='$targetpage?page=$next'>Next</a>";
        } else {
            $paginate.= "<span class='disabled'>Next</span>";
        }
        $paginate.= "</div>";
    }
    // pagination
    echo $total_pages.' Results';
    echo $paginate;
    ////////
    if (count($resultt) !== 0) {
        foreach ($resultt as $row) {
            echo $row['title'];
            echo $row['categories'];
            echo $row['state'];
        }
    } else {
        echo "No data available";
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T16:29:34+00:00Added an answer on June 16, 2026 at 4:29 pm

    It’s certainly because when you go to a next page, you don’t send again these values :

    $input = $_POST['input'];
    $categories = $_POST['category'];
    $state = $_POST['state'];
    

    which correspond to your filters values. Surely they become empty.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a basic POP3 mail client in C/++, but I've run
I'm trying to build a basic servlet with scala, but I can't seem to
Im trying to build a extremely basic content management system. I want to do
I'm trying to build a basic website, but that doesn't limit my ability to
I'm trying to build code from the nVidia 9.5 SDK but I get the
I am trying to build a lucene index through hibernate search FullTextSession.createIndexer().startAndWait() but even
I am trying to build a very basic fulltext search app in php. I
I'm trying to build a simple .Net wrapper around some basic C++ code. The
Im getting used to Visual Basic currently and im trying to build an application
I have been trying to build a basic search script. Having a lot of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.