Jump to content

Almost there!! Problems with search engine and pagination page...


rodhow

Recommended Posts

Hello,

 

I adapted the pagination script from the tutorial (great instruction and detail) from this site and it works perfectly. However, I added a search engine to it and it works like it should. The problem comes in with the following SQL query:

$sql = "SELECT COUNT(*) FROM qa";

 

If i modify it to fit my query, the number at the bottom of the page disappears and the > >> links go to blank pages. Also all queried rows show on one page and doesn't stop at ten. How do i modify my pagination script with a search engine so people can query their results without 'breaking' the pagination script??

 

 

Link to comment
Share on other sites

okay here is the entire code with the part in question near the top. This works perfectly:

 

$sql = "SELECT COUNT(*) FROM qa";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);$numrows = $r[0];

$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
$currentpage = (int) $_GET['currentpage'];

} else { 
$currentpage = 1;
} 

if ($currentpage > $totalpages) { 
$currentpage = $totalpages;
}

if ($currentpage < 1) { 
$currentpage = 1;
} 

$offset = ($currentpage - 1) * $rowsperpage;

$sql = "SELECT link FROM qa LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

while ($list = mysql_fetch_assoc($result)) {   
       echo $list['link'] . "  " . $list['number'] . "<br />";
} 

$range = 3;


if ($currentpage > 1) {
      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; 
      $prevpage = $currentpage - 1;
      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  

         if (($x > 0) && ($x <= $totalpages)) {  

         if ($x == $currentpage) {
             echo " [<b>$x</b>] ";
} else {

             echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; 
          }
     }
}


if ($currentpage != $totalpages) { 
$nextpage = $currentpage + 1;

       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; 

      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}

?>

 

Now this is the part where I added the search engine, some error checks and changed the sql query which seems to break the pagination. I fixed the number count by keeping the SELECT COUNT in the first query and the variables in the second query. The only problem now is that if I have more than one page (11 records or more) the next pages as well as the > >> links return blank pages. I guess its now how can I access the remaining records:

 

<form name="form" action="pagination2.php" method="get">
  <input type="text" name="keywords" />
  <input type="submit" name="Submit" value="Search" />
</form>
<?php
$var = @$_GET['keywords'] ;
  $trimmed = trim($var);

if ($trimmed == "")  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }


$sql = $sql = "SELECT COUNT(*) FROM qa WHERE tags LIKE '%$trimmed%'";   
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);$numrows = $r[0];

echo "<p>You searched for: "" . $var . ""</p>";


$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);

if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {  
$currentpage = (int) $_GET['currentpage'];

} else { 
$currentpage = 1;
} 

if ($currentpage > $totalpages) { 
$currentpage = $totalpages;
}

if ($currentpage < 1) { 
$currentpage = 1;
} 

$offset = ($currentpage - 1) * $rowsperpage;

$sql = "SELECT link FROM qa WHERE tags LIKE '%$trimmed%' LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

echo "<table>";
while ($list = mysql_fetch_assoc($result)) {   
       echo "<tr>";
       echo "<td>" . $list['link'] . "</td>";  
       echo "</tr>";

} 
  echo "</table>"; 


$range = 3;


if ($currentpage > 1) {
      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; 
      $prevpage = $currentpage - 1;
      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}

for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {  

         if (($x > 0) && ($x <= $totalpages)) {  

         if ($x == $currentpage) {
             echo " [<b>$x</b>] ";
} else {

             echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; 
          }
     }
}


if ($currentpage != $totalpages) { 
$nextpage = $currentpage + 1;

       echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; 

      echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}
?>


Link to comment
Share on other sites

I'm not sure is this is the only reason for a blank page (or do you mean just empty data/no data), but you must build the links with the $_GET['keywords'] value on the end of the URL so that each page knows what to search for.

 

If you replace your code, starting at the $range = 3 statement, with the following, it should work -

<?php
$range = 3;
$links = ''; // build links in a string (output it later in your actual content on the page)

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
$_GET['currentpage'] = 1; // set/replace the pagination GET parameter (all other GET parameters unchanged)
$links .= " <a href='?" . http_build_query($_GET, '', '&') . "'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
$_GET['currentpage'] = $prevpage; // set/replace the pagination GET parameter (all other GET parameters unchanged)
$links .= " <a href='?" . http_build_query($_GET, '', '&') . "'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
	// if we're on current page...
	if ($x == $currentpage) {
		// 'highlight' it but don't make a link
		$links .= " [<b>$x</b>] ";
	// if not current page...
	} else {
		// make it a link
		$_GET['currentpage'] = $x; // set/replace the pagination GET parameter (all other GET parameters unchanged)
		$links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>$x</a> ";
	} // end else
} // end if 
} // end for
                 
// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
$_GET['currentpage'] = $nextpage; // set/replace the pagination GET parameter (all other GET parameters unchanged)
$links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>></a> ";
// echo forward link for lastpage
$_GET['currentpage'] = $totalpages; // set/replace the pagination GET parameter (all other GET parameters unchanged)
$links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>>></a> ";
} // end if


echo $links; // echo the links wherever you want in the content on your page

 

The above code only modifies/sets the $_GET['currentpage'] value when building the links, but leaves any other $_GET value as is. It builds the navigation links in a variable so that you can output them anywhere on your page, for instance if you wanted to put links above and below the resultant display of the data. It also corrects the use of < and > characters in the navigation in favor of html entities that won't break the html on the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.