-1

I need help with my website regarding its pagination.

Here's how it work, I search a query, my page gives me its results. Everything is fine so far, but whenever I click on the next page, example page 2, It won't direct me to my next page but instead it will give me an error of:

Notice: Undefined variable: valueToSearch in C:\xampp\htdocs\intern\view-paginated.php on line 72

My first search query works, but only if I click on the next page, suddenly it won't recognize my searched query, basically my pagination just won't work well.

== Source codes ==

Get search query:

    if(isset($_POST['search']))
    {
        $valueToSearch = $_POST['valueToSearch'];
        // search in all table columns
        // using concat mysql function
        $query = "SELECT * FROM `orders` WHERE CONCAT(`KeyAccount`, `BatchNumber`, `Product`, `Quantity`, `PO`, `DateRequested`, `DateDelivered`, `Status`) LIKE '%".$valueToSearch."%'";
        $search_result = filterTable($query);

    }
     else {
        $query = "SELECT * FROM `orders`";
        $search_result = filterTable($query);
    }

    // function to connect and execute the query
    function filterTable($query)
    {
        $connect = mysqli_connect("localhost", "root", "", "test");
        $filter_Result = mysqli_query($connect, $query);
        return $filter_Result;

Defining variables:

$result = mysql_query("SELECT * FROM `orders` WHERE CONCAT(`KeyAccount`, `BatchNumber`, `Product`, `Quantity`, `PO`, `DateRequested`, `DateDelivered`, `Status`) LIKE '%".$valueToSearch."%'");

    $total_results = mysqli_num_rows($search_result);

    $total_pages = ceil($total_results / $per_page);

Most importantly, Display pagination:

echo "<b>View Page:</b> ";

    for ($i = 1; $i <= $total_pages; $i++)

    {

    echo "<a href='view-paginated.php?page=$i'>$i</a> ";

    }

Displaying of results:

echo "<tr>";

echo '<td>' . mysql_result($result, $i, 'KeyAccount') . '</td>';

echo '<td>'. mysql_result($result, $i, 'BatchNumber'). '</td>';

echo '<td>' . mysql_result($result, $i, 'Product') . '</td>';
echo '<td>' . mysql_result($result, $i, 'Quantity') . '</td>';
echo '<td>' . mysql_result($result, $i, 'PO') . '</td>';
echo '<td>' . mysql_result($result, $i, 'DateRequested') . '</td>';
echo '<td>' . mysql_result($result, $i, 'DateDelivered') . '</td>';
echo '<td>' . mysql_result($result, $i, 'Status') . '</td>';
echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';

echo "</tr>";

Thank you in advanced.

John Dale
  • 29
  • 3
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – JustBaron Jul 02 '18 at 09:16
  • Your code is weak to sql injections. Where do you send data handled by search query ? – Zyigh Jul 02 '18 at 09:17
  • POST variables are not set when using `$i` – brombeer Jul 02 '18 at 09:18

1 Answers1

1

It is because you are using POST to fetch variable values, whereas you are using GET to send them like this:

 if(isset($_POST['search']))

Also, you have to send the search parameter, while generating the links for pagination, like this:

    for ($i = 1; $i <= $total_pages; $i++)

{

echo "<a href='view-paginated.php?page=$i".$i."&search=".$search.;

}
Penguine
  • 519
  • 6
  • 19