Hi,
I recently found a great bit of code on the net for pagination. The only problem is, this bit of code worked well in a fixed setup. I had to modify it for my needs, and it's not doing what I'd hoped it would do.
Basically, when I run a search query across my table (covering multiple fields) the code gives me the page results ie. <<previous 1 2 3 next>> (great!). But as soon as I click on Next - even more pages appear ie <<previous 1 2 3 4 5 6 7 8 9 10 next>> (not so great).
Now, I'm sure that I have over looked something. If you can spot it, please let me know.
Here's the code.
form.php
<form name="form" action="search1.php" method="get">
<input class="formblock" type="text" name="q" size="20">
<input type="submit" name="Submit" value="Search"></form>
search1.php
<?php
// file to be included
include 'config.php';
$gensearch = $_GET['general'] ;
//Check if the page number is defined else set it to one. Standard Procedure. 🙂
if(!isset($GET['page'])){
$page = 1;}
else {$page = $GET['page'];
}
//Calclate the offsets
$from = (($page * $max_results) - $max_results);
// Creat the query to be performed and then perform it.
$sql = mysql_query("$query_list LIMIT $from, $max_results");
// pre_result output
echo $pre_result;
while($row = mysql_fetch_array($sql)){ // loop through the result of the page.
include("template.inc");
}
//post_result outpur
echo $post_result;
// Calculate the total number of results.
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM countries WHERE county like \"%$gensearch%\" OR accom like \"%$gensearch%\" OR postcode like \"%$gensearch%\" "),0);
// Calculate the total number of pages. ceil() is used to round the page number to the higher integer.
$total_pages = ceil($total_results / $max_results);
// The real stuff. Creats the Navigation Menu
//Set font style for navigation area
echo "<div class=\"text\"><center>$nav_title<br>";
// Create the Previous link.
if($page > 1){
$prev = ($page - 1);
echo "<a class=\"nav\" href=\"".$SERVER['PHP_SELF']."?$httpvar=$prev&county=$gensearch&accom=$gensearch&postcode=$gensearch\">$pre_style</a> ";
}
//Creat the navigation page numbers.
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){ // make sure the link is not given to the page being viewed
echo "$i ";
} else {
echo "<a class=\"nav\" href=\"".$SERVER['PHP_SELF']."?$httpvar=$i&county=$gensearch&accom=$gensearch&postcode=$gensearch\">$i</a> ";
}
}
// Create the next link.
if($page < $total_pages){
$next = ($page + 1);
echo "<a class=\"nav\" href=\"".$_SERVER['PHP_SELF']."?$httpvar=$next&county=$gensearch&accom=$gensearch&postcode=$gensearch\">$next_style</a>";
}
echo "</center></div>";
echo $post_nav;
?>
config.php
<?
$gensearch = $_GET['general'] ;
//General Configuration Variables
$max_results = 20; // Define the number of results per page
$next_style = 'Next'; // The look for the next button
$pre_style = 'Previous';//The look for the previous button
$extra_var =''; //end with & if used. this is the variable you will pass if required.
//WARNING do not modify the $httpvar.
$httpvar = $extra_var.'page';
$pre_result='';
$post_result='';
$post_nav='';
$nav_title='Pages Available';
//Database configuration; // please change the variables below as required// a must
$user="shmootcase_co_u";
$pass="*";
$host="localhost";
$db="*";
//Database connection. Do not modify below this.
$conn = mysql_connect($host,$user,$pass);
mysql_select_db($db,$conn);
// mysql query to be performed
$query_list = "SELECT * FROM countries WHERE county like \"%$gensearch%\" OR accom like \"%$gensearch%\" OR postcode like \"%$gensearch%\" ";
$numresults=mysql_query($query_list);
$numrows=mysql_num_rows($numresults);
if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search returned no results</p>";
}
?>
template.inc
<? echo " <a href=\"../details.php?id=".$row['id']."\">MORE"; ?><br>
Name:<? echo $row["holsite"]; ?><br>
img src="../../secure/ul/<? echo $row["thumb"]; ?>" border="1"><br>
Town:<? echo $row["town"]; ?><br>
County:<? echo $row["county"]; ?><br>
Thanks, Regards James M.