It’s been a month and am really messed up trying to integrate a php pagination code to my search script. Referred to most of the tutorials Googling, but in vain. Any help would be much appreciated. Here I go…
<?php
ob_start();
session_start();
$_GET['term'] = trim($_GET['term']);
$output = preg_replace('!\s+!', ' ', $_GET['term']);
if(empty($_GET['term'])|| preg_match("/^[@!#\$\^%&*()+=\-\[\]\\\';,\.\/\{\}\|\":<>\?\ _ ]+$/i", $_GET['term']) || $output== ' ' || $_GET['term']== "%24_GET%5B%27term%27%5D")
{
echo "<BR>";
echo "<BR>";
echo("Please enter a Valid Search term");
}
else
{
mysql_connect("localhost", "root", "root");
mysql_select_db("search");
$_GET['term'] = explode(' ', $_GET['term']);
foreach($_GET['term'] AS $_GET['term'])
{
$_GET['term'] = trim($_GET['term']);
$sql = mysql_query("SELECT DISTINCT * FROM searchengine WHERE pagecontent LIKE '%" . str_replace(' ', "%' AND pagecontent LIKE '%", $_GET['term'])."%' LIMIT 0,10");
while($ser = mysql_fetch_array($sql)) {
echo "<BR>";
echo "<b><u><a href='$ser[pageurl]'>$ser[title]</a></u></b>";
echo "<BR>";
echo("<span class='style_block'>{$ser['pagecontent']}</span>");
echo "<BR>";
echo ("<a href='$ser[pageurl]'>$ser[pageurl]</a>");
echo "<BR>";
echo "<BR>";
}
}
$count=mysql_num_rows($sql);
if($count==0)
{
echo "<BR>";
echo "<BR>";
echo "Sorry, No News material was found... Please refine your search criteria and try again.";
}
}
?>
Apart from the problems Luc M has mentioned in his comment (which you should certainly resolve before moving forward), you are almost there.
You need to consider a couple of points, really: How many records to display per page, and what page you are on. These will dictate the records you need to retrieve and display. So, how do you go about this?
The first point is covered in your code already through use of the
LIMITclause in your SQL query. The second point is a tiny bit more complex to start with. You need a way of identifying the page you are on. This is probably easiest to identify through aGETvariable, for examplehttp://site.com/search.php?page=2. Now, for implementing this, you want something along these lines:Then, for your SQL query, you want to build something like this:
The
OFFSETclause of SQL along withLIMITbasically says “Select this many records, starting from result number x”. You offset on$currentPage - 1because the first page doesn’t want an offset, and the second page only wants an offset of however many records were shown on the first page, so on and so forth.To create navigation for the paginated data, you want to find out how many records are in your result set, which can be done through the
count($array)function of PHP. Then, to find the number of pages, simply use something like:$numPages = ceil(count($array)/$recordsPerPage);
Where
$arrayis your dataset from the SQL query. Theceil()function rounds the result up to the next integer.Once you have this result, you simply need to output links to each page, which can be done simply with a
forloop:To create first, previous, next and last page links, you need to do something like:
These values can then be put into your generated links.
Again, I will refer you to Luc M’s comment… These need to be fixed, take a look at
mysqlifunctions instead of the now-deprecatedmysql_*()functions you’re currently using, make sure you clean any user-inputted data before using it, and consider looking at the MVC design pattern.Hopefully, this will help you out.