Hi Guys,
I am trying to solve a pagination problem for my friend. He
runs an antiques business, and wants to be able to select different
items from a dropdown menu, which when listed are paginated so that
they do not appear all on one page. I have done a bit of pagination
before so I said that I'd help him. I have attached the code, for
which I have set up a small example database based on nationality
of people. If you click on British, only British names will appear
etc. This seems to work well, but the problem with the pagination
is that when I click on the link for say page 2, 3, 4, or the final
page then it just takes me back to the radio button menu again and
doesn't display the rest of the results.
If anyone can give me idea as to why this is going wrong then
I'd be very grateful as I need to meet him first thing tomorrow to
show him what I have done.
Thanks
Stuart
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
// Assign connection data to variables
$host = "***********";
$user = "***********";
$pass = "***********";
$db = "comments";
// Connect to MySQL
$connection = mysql_connect($host, $user, $pass) or
die('Error: Could not connect you to MySQL');
// Connect to database
mysql_select_db($db) or die('Error: Could not connect you to
the database');
// **** Pagination Variables **** \\
// set total records per page
$total = 1;
// set URL variable
// Set the default value of $urlVar (the current page) to 1.
$urlVar = (!isset($_GET['page'])) ? 1 : $_GET['page'];
// ???????????????? Confused!!
$from = (($urlVar*$total)-$total);
// defines the LIMIT for each page, which can be passed
through the mysql_query function.
// This informs MySQL as to how many records are allowed on a
page.
$limit = " LIMIT ".$from.",".$total;
// Check to see if the submit button has been clicked or not
if(isset($_POST['submit']) &&
!empty($_POST['submit'])){
if(isset($_POST['nationality'])){
// Switch statement
switch($_POST['nationality']){
case 'Brit':
$query = "SELECT * FROM siri WHERE nationality='British'";
break;
case 'Amer':
$query = "SELECT * FROM siri WHERE nationality='American'";
break;
case 'Nadian':
$query = "SELECT * FROM siri WHERE nationality='Canadian'";
break;
// end of switch statement
}
$query_rs = mysql_query($query.$limit) or die('Error:
'.mysql_error());
// Convert the results of $query_rs into rows using
mysql_num_rows()
$num_rows = mysql_num_rows($query_rs);
if(mysql_num_rows($query_rs) > 0){
echo "Number of rows is: $num_rows";
echo "<table>";
echo "<tr>";
echo '<th scope="col">Id</th>';
echo '<th scope="col">Name</th>';
echo '<th scope="col">Nationality</th>';
echo "</tr>";
while($row = mysql_fetch_assoc($query_rs)){
echo "<tr>";
echo "<td>".$row['id_no']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['nationality']."</td>";
echo "</tr>";
}
echo "</table>";
}
myPagination($query, $total, $urlVar);
// end of if statement checking to see if nationality radio
button has been selected
} else {
echo "Please choose a nationality";
}
}
?>
<form method="post" name="find_names">
<p><input type="radio" name="nationality"
value="Brit" /> British</p>
<p><input type="radio" name="nationality"
value="Amer" /> American</p>
<p><input type="radio" name="nationality"
value="Nadian" /> Canadian</p>
<p><input type="submit" name="submit"
value="Submit" /></p>
</form>
</body>
</html>
<?php
function myPagination($query, $totalPerPage, $urlVar){
// assign the results of $query to variable $db_result
$db_result = mysql_query($query);
$totalrecords = mysql_num_rows($db_result);
// below code works out how many pages there will be. For
example if there are
// 20 records / 10 total per page = 2 pages.
$totalpages = ceil($totalrecords / $totalPerPage);
// define $prev variable. This is used later to allow the
user to visit the previous page.
$prev = ($urlVar - 1);
// define $next variable. This is used later to allow the
user to visit the following page.
$next = ($urlVar + 1);
$pagination = '<div class="pagination">'."\n";
// displays the page number the user is viewing and the
total number of pages.
$pagination .= '<p>You are on page '.$urlVar.' of
'.$totalpages.'</p>'."\n";
$pagination .= '<ul>'."\n";
$pagination .= 'Page: [ ';
if($urlVar > 1){
// if current page is greater than 1 then display
'<<.' This allows user to go back to
// latest comments.
$pagination .= '<li><a
href="'.$_SERVER['PHP_SELF'].'">«</a></li>'."\n";
if($urlVar != 2) {
// if current page is not the value '2' then display '<',
which allows the user to revisit the
// previous page.
$pagination .= '<li><a
href="'.$_SERVER['PHP_SELF'].'?page='.$prev.'">‹</a></li>'."\n";
}
}
// start the counter at 2, iterate through the for loop
until you reach the total number
// of pages.
for($counter=2; $counter <= $totalpages; $counter++){
if($counter != $totalpages){
if($urlVar == $counter){
// if the current page is equal to the counter, do not
display the $urlVal as a link
// highlight it in bold so that it identifies which page you
are on.
$pagination .=
'<li><strong>'.$counter.'</strong></li>'."\n";
} else {
// if not current page then display each page as a hyperlink
with the appropriate page number.
$pagination .= '<li><a
href="'.$_SERVER['PHP_SELF'].'?page='
.$counter.'">'.$counter.'</a></li>'."\n";
}
}
}
if($urlVar < $totalpages){
if($urlVar < $totalpages -1){
// Display the '>' hyperlink if the current page is less
than the 2nd from final page. Allows user
// to jump to the next page.
$pagination .= '<li><a
href="'.$_SERVER['PHP_SELF'].'?page='.$next.'">></a></li>'."\n";
}
// Display '>>' to allow user to jump to the final
page.
$pagination .= '<li><a
href="'.$_SERVER['PHP_SELF'].'?page='.$totalpages.'">»</a></li>'."\n";
}
// below code closes up the unordered list and div. Displays
the list of page numbers.
$pagination .= ' ]'."\n";
$pagination .= '</ul>'."\n";
$pagination .= '</div>'."\n";
echo $pagination;
}
?>