SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Need help with pagination.
-
Jun 19, 2009, 07:32 #1
- Join Date
- Jan 2008
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Need help with pagination.
Hello I have the following code for pagination. buts when i click on the next or last button its showing no record found. can any1 help me pls??
<?php
include "dbconnect.php";
$link=dbconnect();
$pc=$_POST['pc'];
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$query=("select *from doctors WHERE pc='$pc';");
$resultt=mysql_query($query);
$rows = mysql_num_rows($resultt);
if(!$rows)
{
echo"<script>alert (\"Sorry No Record Found!!!\");history.go(-1)</script>";
break;
}
else
{
$page_rows = 3;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$result=mysql_query("select *from doctors WHERE pc='$pc' $max;");
while ($row=mysql_fetch_array($result))
{
echo $row["title"]." "." ". $row["name"];
}
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
-
Jun 19, 2009, 07:38 #2
1) when you click on the next or last button, no 'pc' value is passed, so $_POST['pc'] will not be set, and your first query won't find any rows.
2) you pass 'pagenum' through the url, but never use $_GET['pagenum'] anywhere. $pagenum does not automatically contain the passed value (at least it won't if register_globals is OFF, as it should be).Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Jun 19, 2009, 07:51 #3
- Join Date
- Jan 2008
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 19, 2009, 08:05 #4Code PHP:
<?php include "dbconnect.php"; $link=dbconnect(); $pc = ''; if (isset($_POST['pc'])) { $pc = $_POST['pc']; } else { if (isset($_GET['pc'])) { $pc = $_GET['pc']; } } if (isset($_GET['pagenum'] && ((int)$_GET['pagenum'] > 0)) { $pagenum = (int)$_GET['pagenum']; } else { $pagenum = 1; } $query=("select *from doctors WHERE pc='$pc'") or die(mysql_error()); $resultt=mysql_query($query); $rows = mysql_num_rows($resultt); if(!$rows) { echo"<script>alert (\"Sorry No Record Found!!!\");history.go(-1)</script>"; break; } else { $page_rows = 3; $last = ceil($rows/$page_rows); if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; } $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; $result=mysql_query("select *from doctors WHERE pc='$pc' $max;") or die(mysql_error()); while ($row=mysql_fetch_array($result)) { echo $row["title"]." "." ". $row["name"]; } echo " --Page $pagenum of $last-- <p>"; if ($pagenum != 1) { echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> "; echo " "; $previous = $pagenum-1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> "; } echo " ---- "; if ($pagenum != $last) { $next = $pagenum+1; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> "; echo " "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> "; } } ?>
Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
-
Jun 19, 2009, 08:46 #5
- Join Date
- Jan 2008
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Bookmarks