I have a search form that searches a mysql database and return results. I also have a searchmodel.php file that is supposed to paginate results.However, the pagination does not return results for page 2 and other pages. The search form looks like this:
<?php
?>
<html>
<form action = "searchmodel.php" method = "GET">
<select name = "choice">
<!--<option value = "<?php echo $_GET['title']; ?>" name = "title">title</option>-->
<option value = "title" name = "title">title</option>
<option value = "author" name = "author"> author</option>
<option value = "scc" name = "scc"> SCC_NO</option>
</select>
<input name= "term" type ="text" size="65" maxlength = "88" style = "display:inline">
<input name = "mysearch" type="submit" style = "display:inline">
</form>
</html>
The searchmodel.php file looks like this:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
error_reporting(E_ERROR | E_PARSE);
include "data.php";
$con = dbConnect();
$perpage = 20;
echo ($pagenumber = isset($_GET['page']) ? $_GET['page'] : 1) . '<br />';
$offset = ($pagenumber-1) * $perpage;
$term = isset($_GET['term']) ? $_GET['term'] : null;
$choice = isset($_GET['choice']) ? $_GET['choice'] : '';
if(empty($term) && ($choice == 'title' || $choice == 'author')){
header ('Location: ' . 'search.php');
}
if($choice == 'title') {
$sth = $con->prepare("SELECT SQL_CALC_FOUND_ROWS * FROM manuscript WHERE title
LIKE '%$term%' LIMIT {$offset},{$perpage}");
$sth->execute();
//$results = $sth->rowCount();
//echo '<b>' . $results . '</b>'. '<br />';
$results = $sth->fetchAll();
$x=1;
foreach($results as $key => $value){
echo $x++ . ':' .$value['title'] . '<br />';
}
}
$total = $con->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
var_dump($total);
$pages = ceil($total/$perpage);
echo $pages . '<br />';
for ($x=1; $x<=$pages; $x++){
//echo $x;
echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
//echo "<a href='?pagenumber=$x'>$x</a>";
}
I have searched this site(stackoverflow) for a question similar to mine and have found these two. This one and this one. However, the answers provided seem not to work for my case. My question is, what might i be doing wrong?? Is it the $_GET['choice'] variable causing the problem?Thanking you in advance
` in the source of the page? – chris85 Apr 15 '18 at 15:08
`. Has it got to do with the url e.g., when i conduct a search the resultant url looks like `http://localhost/searchmodel.php?choice=title&term=malaria&mysearch=Submit+Query` and when i click on a page number the url looks like this `http://localhost/searchmodel.php?page=2` – Alex Maina Apr 15 '18 at 15:20
';` though so you should always be getting at least a `
` unless there is a fatal error – chris85 Apr 15 '18 at 15:25
` in the source code?? I don't think your `blank page` description is accurate. – chris85 Apr 15 '18 at 15:39
' because it is concactenated as part of the echo. What do you suggest i do to solve my problem? – Alex Maina Apr 15 '18 at 15:47