1

Pagination problem in search result

I am trying to paginate the search results, Here is the code that i'm using to look up for the search queries from user's form. I am getting search results paginated, but when i click 2 nd page of results it shows undefined index for those item posted...While gone through tutorials i have seen to use $_GET instead of $_POST ,after doing that chane also no improvement in results

As i am new to this php code, can you guys help or guide me in the right direction? /////////////test.php//////////////////

    mysqli_report(MYSQLI_REPORT_ERROR);


    // number of results to show per page
    $per_page = 2;

    // figure out the total pages in the database
    if ($result = $mysqli->query("SELECT * FROM bridegroom where Gender like '%$Name%' and Castest like'%$caste%' and Location like '%$location%' order by AGE"))
    {
        if ($result->num_rows != 0){
            $total_results = $result->num_rows;
            // ceil() returns the next highest    integer value by rounding up value if necessary
            $total_pages = ceil($total_results / $per_page); 
            if (isset($_GET['page']) && is_numeric($_GET['page']))
            {
                $show_page = $_GET['page'];
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                   $start = ($show_page -1) * $per_page;
                   $end = $start + $per_page; 
                }
                else
                {
                    $start = 0;
                    $end = $per_page; 
                }               
            }
            else
            {
                $start = 0;
                $end = $per_page; 
            }

            // display pagination
            echo "<p> <b>View search results:</b> ";
            for ($i = 1; $i <= $total_pages; $i++)
            {
                if (isset($_GET['page']) && $_GET['page'] == $i)
                {
                     echo $i . " ";
                }
                else
                {
                    echo "<a href='test.php?page=$i'>$i</a> ";
                }
            }
            echo "</p>";

            // display data in table

            // loop through results of database query, displaying them in the table 
            for ($i = $start; $i < $end; $i++)
            {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // find specific row
                $result->data_seek($i);
                $row = $result->fetch_row();
                // echo out the contents of each row into a table
                echo ("Name:$row[1]<br><span class=\"old\"> Age:$row[4]</span><br><span        class=\"sold\">Caste:$row[5]</span><br><span class=\"old\">Location:</span><span class=\"sold\">$row[6]</span><br><br>");

            }

            // close table>

        }         
        else
        {
            echo "No results to display!";
        } 
    }

    // error with the query
    else
    {
        echo "Error: " . $mysqli->error;
    }

    // close database connection
    $mysqli->close();

    // display pagination
    echo "<p> <b>Your search results:</b> ";
    for ($i = 1; $i <= $total_pages; $i++)
    {
        if (isset($_GET['page']) && $_GET['page'] == $i)
        {
            echo $i . " ";
        }
        else
        {
            echo "<a href='test.php?page=$i'>$i</a> ";
        }
    }

    echo "</p>"; 
?>

anya
  • 37
  • 1
  • 8
  • Undefined index simply means it's not getting the GET method, I'm taking a quick guess here but maybe you need to make the "next" page link into a get form? – A. Mo Oct 31 '13 at 08:36
  • i have tried using get..still not...problem is in $_GET[page]--->link – anya Oct 31 '13 at 09:00
  • if i use user get form in that link it will redirect to that search page – anya Oct 31 '13 at 09:03
  • I try this code and no error on my side, first Try to print if((isset($_GET["page"]))){ echo $_GET["page"]; } if its print and still giving error then write this on top of your page – Waqas Ghouri Oct 31 '13 at 11:48
  • @Waqas Ahmed..I have tried as you said,it returns result without error but not which i needed..On first page i got result as per my search if i move over next page it display random data from database.. – anya Nov 01 '13 at 09:10
  • whats problem occurs in displaying random data? Is it showing data from your current query table? Remember it showing record from specific row not from unique id. Kindly describe more briefly what record you want? – Waqas Ghouri Nov 01 '13 at 10:26
  • @ Waqas Ahmed my query is as follow :Gender like '%$Name%' and Castest like'%$caste%' and Location like '%$location%.......and based on this query results are displayed only on first page.on second page result is not related to what i have searched – anya Nov 01 '13 at 10:48

0 Answers0