Stuck with next and previous pagination problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jamesmiddz
Forum Newbie
Posts: 18
Joined: Wed Jun 23, 2004 11:48 am
Location: UK

Stuck with next and previous pagination problems

Post by jamesmiddz »

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

Code: Select all

&lt;form name="form" action="search1.php" method="get"&gt;
&lt;input class="formblock" type="text" name="q" size="20"&gt;
&lt;input type="submit" name="Submit" value="Search"&gt;&lt;/form&gt;

search1.php

Code: Select all

<?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>&nbsp;"; 
} 
//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&nbsp;"; 
        } else { 
            echo "<a class="nav" href="".$_SERVER['PHP_SELF']."?$httpvar=$i&county=$gensearch&accom=$gensearch&postcode=$gensearch">$i</a>&nbsp;"; 
    } 
} 
// 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

Code: Select all

<?
$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

Code: Select all

<? 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.


feyd | Please use

Code: Select all

and

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Put your code inside [syntax=php][/syntax] tags for much easier reading.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply