PDA

View Full Version : Pagination code not behaving.



DigitalWallfare
November 29th, 2009, 10:50 AM
Hi all,

I've written code to enable pagination of my search results, but it isnt behaving how I expect it to. It produces 5 results per page, no matter how many I tell it to display, and will not produce the navigation links at all (there are 6 test examples in my database).

Is anyone able to look over my code and tell me where i'm going wrong?

I have error reporting on, but it wont report user error :drool:

Here is my code:


$connection = mysql_connect($hostname, $username, $password);
if (!$connection) {
die("A connection to the server could not be established");
}
/*Select Database */
mysql_select_db($database) or die("Database could not be selected!");

/*set variables*/
if (isset($_POST['MerchantType']) && isset($_POST['County'])){
$MerchantType = $_POST["MerchantType"];
$County = $_POST["County"];
echo $County . " " . $MerchantType;
}


/* Set current, prev and next page */
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
$prev = ($page - 1);
$next = ($page + 1);

/* Max results per page */
$max_results = 2;

/* Calculate the offset */
$from = (($page * $max_results) - $max_results);
/*Query */
$result = mysql_query("SELECT * FROM $tablename WHERE County = '$County' AND MerchantType = '$MerchantType'") or die(mysql_error());

$total_results = mysql_num_rows($result);

$total_pages = ceil($total_results / $max_results);

$pagination = '';

/* Create a PREV link if there is one */
if($page > 1)
{
$pagination .= '<a href="index.php?page='.$prev.'">Previous</a> ';
}

/* Loop through the total pages */
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="index.php?page='.$i.'">$i</a>';
}
}

/* Print NEXT link if there is one */
if($page < $total_pages)
{
$pagination .= '<a href="index.php?page='.$next.'">Next</a>';
}




while ($i = mysql_fetch_array($result))



while ($row_details = mysql_fetch_array($result))

Hopefully someone might be able to help.

Thanks,

Sam

icio
November 29th, 2009, 08:05 PM
Is that the complete script? There isn't any code outputting any row entries, let alone 5.

charplus01
December 6th, 2009, 06:48 PM
shouldnt you 'while' loop start at 0 instead of 1? while($i=0)