Jump to content

Problem with pagination


keituu

Recommended Posts

Hi, I new to php and I'm having problem with pagination. the problem is If I clicked next link or last page or any page link, it goes back to the home page and I've used PHP_SELF to establish the link. All I know something is wrong in my pagination link. May be there is pre defined value for PHP_SELF to be recorgnized or certain value is missing in my pagination link.

Here is  my code

 

include("conn.php");
if(($_POST["orga_name"]) && empty($_POST["title"])) {
$sql = "select * from client_info where Name LIKE '%".$_POST["orga_name"]."%'";
$result = mysql_query($sql);
$r = mysql_num_rows($result);
include("form_search.php");
if ($r == 0 ){
echo "No records found";
}
else { 
//pagination inaanzia hapa
// how many rows to show per page
$rowsPerPage = 5;
// by default we show first page
$pageNum = 1;
// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}
// how many pages we have when using paging
$maxPage = ceil($r/$rowsPerPage);
$offset = ($pageNum - 1) * $rowsPerPage;
//pagination inaishia hapa
$sql = "select * from client_info where Name LIKE '%".$_POST["orga_name"]."%' LIMIT $offset,$rowsPerPage";
$result = mysql_query($sql);   
//echo $r;
echo "<table>";
echo "<tr><td class='title'>"."Name"."</td><td> </td><td class='title'>"."Box No"."</td><td> </td><td class='title'>"."Town/City"."<td></td><td> </td><td class='title'>"."Phone Number"."</td><td> </td><td class='title'>"."Email"."</td><td> </td><td class='title'>"."Website"."</td><td> </td></tr>";         
       $i=1;
         
            while($row=mysql_fetch_array($result))
         { 
         $name = $row["Name"];
         $address = $row["POBoxNo"];
         $town = $row["TownCity"];
         $phonenumber = $row["OfficePhoneNumber"];
         $email = $row["Email"];
         $web = $row["Web"];
         
   if($i%2){
         echo "<tr><td class='odd'>"."<br>".$name."<br>"."&nbsp"."</td><td> </td><td class='odd'>".$address."</td><td> </td><td class='odd'>".$town."<td></td><td> </td><td class='odd'>".$phonenumber."</td><td> </td><td class='odd'>".$email."</td><td>&nbsp</td><td class='odd'>".$web."</td></tr>";   
          }
        else{   
         echo "<tr><td class='even'>"."<br>".$name."<br>"."&nbsp"."</td><td> </td><td class='even'>".$address."</td><td> </td><td class='even'>".$town."<td></td><td> </td><td class='even'>".$phonenumber."</td><td> </td><td class='even'>".$email."</td><td>&nbsp</td><td class='even'>".$web."</td></tr>";
         }         
         $i++;
      }      
  echo "</table>";
  // print the link to access each page
$self = htmlentities($_SERVER['PHP_SELF']);
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{
$nav .= " <a href=\"$self?page=$page\">$page</a> ";
}
}
// creating previous and next link
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?page=$page\">[Prev]</a> ";
$first = " <a href=\"$self?page=1\">[First Page]</a> ";
}
else
{
$prev = ' '; // we're on page one, don't print previous link
$first = ' '; // nor the first page link
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?page=$page\">[Next]</a> ";
$last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
}
else
{
$next = ' '; // we're on the last page, don't print next link
$last = ' '; // nor the last page link
}
//echo $nav . "<br />";
echo $next . "  " . $prev ; //"<br />";
echo $first . "  " . $last;
}

 

Any help will be highly appreciated.

 

MOD EDIT:

 . . . 

BBCode tags added.

Link to comment
https://forums.phpfreaks.com/topic/234368-problem-with-pagination/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.