You are here: Pagination Problem « All PHP « IT news, forums, messages
Pagination Problem

Posted by Chris H on 03/25/06 02:25

I am having a problem with pagination, basically the problem is happening in
the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue
the increment and decrement operators aren't functioning or the $page
variable isnt working in that part of the code... Below is the link to the
working but broken page.. as well as the main part of my code... Hopefully
someone can explain why the operators arent working or maybe see what i am
overlooking.

LINK: http://movies.evilacid.com

Note: most pagination code will be between
########
//comment
CODE
#######

PHP Code:
==================================
<?php

require ('config.php');

if (!function_exists('connect_to_mysql')){

function connect_to_mysql(){

global $db_location,$db_username,$db_password,$db_databas
e;

$conn =
mysql_connect("$db_location","$db_username","$db_password");
if (!$conn) die("Error: " . mysql_error());
mysql_select_db($db_database,$conn) or die("Error: " .
mysql_error());

return;
}
}

if (!function_exists('error_message')){

function error_message($text){

echo"<p align='left' class='errortext'><br><b><span
style='background-color: #FF6666'>Error:</span></b><br>$text</p>";

return;

}

}
##################################################
##############################

function list_all($cat,$sortby,$char,$page,$pageprev){
global $title;
###############################
// part of pagination code
$limit = 25;
if(!$page){
$page = 1;
}
################################

$how_many = mysql_num_rows(mysql_query("SELECT * FROM moviedb"));

echo "<table width=\"820\" border=\"0\" cellspacing=\"0\"
cellpadding=\"0\">";
echo "<tr><td><center><font size=\"6\">$title</font></center></td></tr>";
echo "<tr><td><center>Currently $how_many movies in
database</center></td></tr>";
echo "<tr><td><center><br>";
echo quick_links();
echo "</center></td></tr>";
echo "</table>\n";
echo "<table width=\"820\" border=\"1\" cellspacing=\"0\"
cellpadding=\"0\">";
echo "<tr>";
echo "<th width=\"35\" bgcolor=\"66ccff\"></th>";
echo "<th width=\"400\" bgcolor=\"66ccff\"><strong><a
href=\"index.php?t=&sortby=title\">Movie Title</a> </strong></th>";
echo "<th width=\"260\" bgcolor=\"66ccff\"><strong><a
href=\"index.php?t=&sortby=rating\">Rating</a></strong></th>";
echo "<th width=\"125\" bgcolor=\"66ccff\"><strong><a
href=\"index.php?t=&sortby=cat\">Genre</a></strong></th>";
echo "</tr>";

connect_to_mysql();

##################################
//pagination code
$limitvalue = $page * $limit - ($limit);
##################################
if ($sortby == "") {
$sql = mysql_query("SELECT * FROM moviedb ORDER BY title ASC LIMIT
$limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb");
$totalrows = mysql_num_rows($q_count);
}
elseif ($sortby == "recent") {
$sql = mysql_query("SELECT * FROM moviedb ORDER BY date DESC LIMIT
$limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb");
$totalrows = mysql_num_rows($q_count);
}
elseif ($sortby == "alpha") {
$sql = mysql_query("SELECT * FROM moviedb WHERE alpha LIKE '$char' ORDER
BY title ASC LIMIT $limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb WHERE alpha LIKE
'$char'");
$totalrows = mysql_num_rows($q_count);
}
elseif ($sortby == "genre") {
$sql = mysql_query("SELECT * FROM moviedb WHERE cat LIKE '$cat' ORDER BY
title ASC LIMIT $limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb WHERE cat LIKE '$cat'");
$totalrows = mysql_num_rows($q_count);
}
elseif ($sortby == "title") {
$sql = mysql_query("SELECT * FROM moviedb ORDER BY title ASC LIMIT
$limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb");
$totalrows = mysql_num_rows($q_count);
}
elseif ($sortby == "rating") {
$sql = mysql_query("SELECT * FROM moviedb ORDER BY rating DESC LIMIT
$limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb");
$totalrows = mysql_num_rows($q_count);
}
elseif ($sortby == "cat") {
$sql = mysql_query("SELECT * FROM moviedb ORDER BY cat ASC LIMIT
$limitvalue, $limit");
$q_count = mysql_query("SELECT * FROM moviedb");
$totalrows = mysql_num_rows($q_count);
}

$bgcolor = "#E0E0E0"; // light gray
$result = $sql;
$z = 1;
while($rs = mysql_fetch_array($result)) {
if ($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}
$alpha = $rs['alpha'];
$title = $rs['title'];
$link = $rs['link'];
$rating = $rs['rating'];
$ratingf = format_rating($rating);
$category = $rs['cat'];
$stars = get_stars($rating);

echo "<tr>";
echo "<td width=\"35\" bgcolor=\"$bgcolor\"><center>$z</center></td>";
echo "<td width=\"400\" bgcolor=\"$bgcolor\"><a
href=\"$link\">$title</a></td>";
echo "<td width=\"260\" bgcolor=\"$bgcolor\">$stars ($ratingf)</td>";
echo "<td width=\"125\"
bgcolor=\"$bgcolor\"><center>$category</center></td>";
echo "</tr>\n";
$z++;
}
echo "</table>\n";
################################################## #
//PAGINATION LINKS STARTS HERE
if($page != 1){
$pageprev = $page--;

echo("<a href=\"index.php?t=&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"index.php?t=&page=$i\">$i</a> ");
}
}


if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"index.php?t=&page=$i\">$i</a> ");
}
}

if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page--;

echo("<a href=\"index.php?t=&page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
##############################################

return;

}

//was browse_by_cat()
function quick_links(){

print <<<EOF
<center>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse:
collapse" bordercolor="#111111" width="820" id="AutoNumber1">
<tr>
<td width="100">
<form action='$_SERVER[PHP_SELF]' method='get' name="form">
<input type='hidden' name='t' value=''>
<input type='hidden' name='sortby' value='genre'>
<select name="cat" size="1" id="cat" onchange="this.form.submit();">
<option value="" selected>Browse By Genre</option>
<option value="%">All Movies</option>
<option value="action">Action</option>
<option value="adventure">Adventure</option>
<option value="animation">Animation</option>
<option value="biography">Biography</option>
<option value="comedy">Comedy</option>
<option value="crime">Crime</option>
<option value="documentary">Documentary</option>
<option value="drama">Drama</option>
<option value="educational">Educational</option>
<option value="family">Family</option>
<option value="fantasy">Fantasy</option>
<option value="horror">Horror</option>
<option value="music">Music</option>
<option value="musical">Musical</option>
<option value="mystery">Mystery</option>
<option value="romance">Romance</option>
<option value="scifi">Sci-Fi</option>
<option value="short">Short</option>
<option value="sport">Sport</option>
<option value="tv">Television</option>
<option value="thriller">Thriller</option>
<option value="war">War</option>
<option value="western">Western</option>
</select>
</form>
</td>
<td width="150">
<form action='$_SERVER[PHP_SELF]' method='get'>
<input type='hidden' name='t' value=''>
<input type='hidden' name='sortby' value='alpha'>
<select name="char" size="1" onchange="this.form.submit();" id="char" >
<option value="" selected>Browse Alphabetically</option>
<option value="%" >List All</option>
<option value="#" >Number</option>
<option value="a" >A</option>
<option value="b" >B</option>
<option value="c" >C</option>
<option value="d" >D</option>
<option value="e" >E</option>
<option value="f" >F</option>
<option value="g" >G</option>
<option value="h" >H</option>
<option value="i" >I</option>
<option value="j" >J</option>
<option value="k" >K</option>
<option value="l" >L</option>
<option value="m" >M</option>
<option value="n" >N</option>
<option value="o" >O</option>
<option value="p" >P</option>
<option value="q" >Q</option>
<option value="r" >R</option>
<option value="s" >S</option>
<option value="t" >T</option>
<option value="u" >U</option>
<option value="v" >V</option>
<option value="w" >W</option>
<option value="x" >X</option>
<option value="y" >Y</option>
<option value="z" >Z</option>
</select>
</form>
</td width="300">
<td><form action='$_SERVER[PHP_SELF]' method='get'>
<input type='hidden' name='t' value=''>
<select name="sortby" size="1" onchange="this.form.submit();" id="char"
>
<option value="" selected>Sort By ----</option>
<option value="" >Show all titles </option>
<option value="recent" >Sort by recently added titles</option>
</select>
</form></td>
<td>&nbsp;</td>
</tr>
</table>
</center>
EOF;

return;
}

function format_rating($rating){

if ($rating == "01") { $ratingf= "1"; }
elseif ($rating == "02") { $ratingf= "2"; }
elseif ($rating == "03") { $ratingf= "3"; }
elseif ($rating == "04") { $ratingf= "4"; }
elseif ($rating == "05") { $ratingf= "5"; }
elseif ($rating == "06") { $ratingf= "6"; }
elseif ($rating == "07") { $ratingf= "7"; }
elseif ($rating == "08") { $ratingf= "8"; }
elseif ($rating == "09") { $ratingf= "9"; }
elseif ($rating == "10") { $ratingf= "10"; }
return $ratingf;
}

function get_stars($rating){
global $star, $hstar;

if ($rating == 1) { $stars="<img src=$star>"; }
elseif ($rating == 2) { $stars="<img src=$star><img src=$star>"; }
elseif ($rating == 3) { $stars="<img src=$star><img src=$star><img
src=$star>"; }
elseif ($rating == 4) { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star>"; }
elseif ($rating == '5') { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star>"; }
elseif ($rating == '6') { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star><img src=$star>"; }
elseif ($rating == '7') { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star><img src=$star><img src=$star>"; }
elseif ($rating == '8') { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star><img src=$star><img src=$star><img
src=$star>"; }
elseif ($rating == '9') { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star><img src=$star><img src=$star><img
src=$star><img src=$star>"; }
elseif ($rating == '10') { $stars="<img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star><img src=$star><img src=$star><img
src=$star><img src=$star><img src=$star>"; }
return $stars;
}



##################################################
##############################


?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация