My Search result aint displaying on the second page

Good day all programmers in the house. please I need help in the following area. The code posted below works perfectly for search and paginating my result as I used the same code for other pagination like product listing which doesn’t involve any search but the challenge I have now is that after displaying the result on the search page (The page is self processing page), clicking the link to page 2 or 3 or other pages comes up with a blank page. I thought its because I used $_GET instead of $_POST but using $_POST doesn’t solve the problem. Please what do I do to get the pagination links to work.


//This does the actual searching
if (isset($_GET['action']) and $_GET['action'] == 'search')
{

//connect to the database
    if(include_once ('DatabaseManager.php'));
	$conn = DatabaseManager::getConnection();
    //get the function
    include_once ('function.php');

    	$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
    	$limit = 1;
    	$startpoint = ($page * $limit) - $limit;
   	

if ($_GET['product'] != '')
{
   $product = trim($_GET['product']);
}

if ($_GET['purchaseDateFrom'] != '')
{
   $purchaseDateFrom = trim($_GET['purchaseDateFrom']);
}

if ($_GET['purchaseDateTo'] != '')
{
   $purchaseDateTo = trim($_GET['purchaseDateTo']);
}

if ($_GET['manufacturer'] != '')
{
   $manufacturer = trim($_GET['manufacturer']);
}

   		
$statement = "assets WHERE ";

if(isset($product)){
                   $statement .= "product LIKE '%" . $product .  "%' AND ";
                }

if(isset($manufacturer)){
                   $statement .= "manufacturer LIKE '%" . $manufacturer .  "%' AND ";
                }
if(isset($purchaseDateFrom) && isset($purchaseDateTo)){
                   $statement .= "(purchaseDate >=  '$purchaseDateFrom' AND purchaseDate <= '$purchaseDateTo') AND ";
                }
$statement .= '1';

 $query = $conn->query("SELECT * FROM {$statement} ORDER BY id DESC LIMIT {$startpoint} , {$limit}");
 $assetRegister = $conn->query("SELECT * FROM {$statement}");
 $asset = $assetRegister->rowCount();

}

The included function is below


function pagination($query, $per_page = 10,$page = 1, $url = '?'){
   include_once ('DatabaseManager.php');
   $conn = DatabaseManager::getConnection();
	
    	$query = "SELECT COUNT(*) as `num` FROM {$query}";
		foreach($conn-&gt;query($query) as $row);
		$total = $row['num'];
        $adjacents = "2";

    	$page = ($page == 0 ? 1 : $page);
    	$start = ($page - 1) * $per_page;								
		
    	$prev = $page - 1;							
    	$next = $page + 1;
        $lastpage = ceil($total/$per_page);
    	$lpm1 = $lastpage - 1;
    	
    	$pagination = "";
    	if($lastpage &gt; 1)
    	{	
    		$pagination .= "&lt;ul class='pagination'&gt;";
                    $pagination .= "&lt;li class='details'&gt;Page $page of $lastpage&lt;/li&gt;";
    		if ($lastpage &lt; 7 + ($adjacents * 2))
    		{	
    			for ($counter = 1; $counter &lt;= $lastpage; $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
    				else
    					$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";					
    			}
    		}
    		elseif($lastpage &gt; 5 + ($adjacents * 2))
    		{
    			if($page &lt; 1 + ($adjacents * 2))		
    			{
    				for ($counter = 1; $counter &lt; 4 + ($adjacents * 2); $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
    					else
    						$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";					
    				}
    				$pagination.= "&lt;li class='dot'&gt;...&lt;/li&gt;";
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lpm1'&gt;$lpm1&lt;/a&gt;&lt;/li&gt;";
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lastpage'&gt;$lastpage&lt;/a&gt;&lt;/li&gt;";		
    			}
    			elseif($lastpage - ($adjacents * 2) &gt; $page && $page &gt; ($adjacents * 2))
    			{
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=1'&gt;1&lt;/a&gt;&lt;/li&gt;";
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=2'&gt;2&lt;/a&gt;&lt;/li&gt;";
    				$pagination.= "&lt;li class='dot'&gt;...&lt;/li&gt;";
    				for ($counter = $page - $adjacents; $counter &lt;= $page + $adjacents; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
    					else
    						$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";					
    				}
    				$pagination.= "&lt;li class='dot'&gt;..&lt;/li&gt;";
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lpm1'&gt;$lpm1&lt;/a&gt;&lt;/li&gt;";
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lastpage'&gt;$lastpage&lt;/a&gt;&lt;/li&gt;";		
    			}
    			else
    			{
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=1'&gt;1&lt;/a&gt;&lt;/li&gt;";
    				$pagination.= "&lt;li&gt;&lt;a href='{$url}page=2'&gt;2&lt;/a&gt;&lt;/li&gt;";
    				$pagination.= "&lt;li class='dot'&gt;..&lt;/li&gt;";
    				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter &lt;= $lastpage; $counter++)
    				{
    					if ($counter == $page)
    						$pagination.= "&lt;li&gt;&lt;a class='current'&gt;$counter&lt;/a&gt;&lt;/li&gt;";
    					else
    						$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$counter'&gt;$counter&lt;/a&gt;&lt;/li&gt;";					
    				}
    			}
    		}
    		
    		if ($page &lt; $counter - 1){
    			$pagination.= "&lt;li&gt;&lt;a href='{$url}page=$next'&gt;Next&lt;/a&gt;&lt;/li&gt;";
                $pagination.= "&lt;li&gt;&lt;a href='{$url}page=$lastpage'&gt;Last&lt;/a&gt;&lt;/li&gt;";
    		}else{
    			$pagination.= "&lt;li&gt;&lt;a class='current'&gt;Next&lt;/a&gt;&lt;/li&gt;";
                $pagination.= "&lt;li&gt;&lt;a class='current'&gt;Last&lt;/a&gt;&lt;/li&gt;";
            }
    		$pagination.= "&lt;/ul&gt;\
";		
    	}


        return $pagination;
    }

Seems, that you have to pass your search parameters to your pagination function, so that correct links would get generated.

Please how do I do that? Just an example will let me know how to go by it.

Before calling pagination() you can add variables to $url as needed.
This is just an example just adds all existing $_GET variables, except $page, which is added to $url in the function itself.


$url = '?';
foreach($_GET as $k => $v) {
    if($k != 'page') {
        $url .= "{$k}={$v}&";
    }
}

...

pagination($query, $per_page, $page, $url);

Ronald, you’re too brilliant, you’re a genius man. thanks, it works very well. Thanks man.

That code is also a major security threat and should not be used under any conditions whatsoever.

Also, your pagination code is a complete mess.

Please elaborate this further. :smiley:

Of course, the code should not be copied and used as is, I was showing just a principle of getting the variables from $_GET array, in real world situation those should be escaped before outputting to page.

You should always explicitly grab the variables from $_GET and $_POST superglobals, because doing automatic stuff like this means you leave yourself open to any number of potential attacks. If you are explicit, then only the values and variables you want to enter into your program can, whereas if you try to be too clever like this, you can end up with the potential for sql injection and xss exploits, depending on the context. It’s just not a good idea, and it’s not a good thing to recommend.

In terms of why the pagination class is a mess - primarily because all the presentation logic is tied into the class itself. The class should just handle the logic and should not contain any html. The html build should be called outside of this, and should only reference the class to work out which position it’s in and which links should come next etc. Sticking the presentation logic into the class itself makes it very hard to change the design at a later date, and would mean you wouldn’t be able to use the same class on multiple projects, which you would be able to do if it was more carefully designed.

Please guys, I need more help with this question. I want a situation whereby the user of my application can choose how many record set should be displayed per page by making a selection from a drop down menu I will incorporate into the search page. How do I accomplish this?