Jump to content

[SOLVED] Need Help Altering Pagination Script


phpQuestioner

Recommended Posts

I want to be able to disable the pagination link for the current page I am on. I also would like to add "<<Previous" and "Next>>" to this pagination. I have done some searching on this site and others and found many pagination script; but I kind of understand this one, that I found on this site. Could someone help me transform this script or tell me how I could transform it to include "<<Previous" and "Next>>" links and to disable/remove the link to the pagination number of the current page I am on.

 

Here Is The Code I Found On PHPFreaks Sample Codes

 

<?php


      $host = "localhost"; //your sql host, usually 'localhost'

      $user = "username"; //username to connect to database

      $pass = "password"; //password to connect to database

      $db = "List"; //the name of the database

      mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());

      mysql_select_db($db) or die("ERROR DB:".mysql_error());


      $max = 1; //amount of data table rows per page. change to the maximum amount of rows you want to display per page

      $page = $_GET['page'];

      if(empty($page))

      {

      $page = 1;

      }

      $limits = ($page - 1) * $max;

      //this section is where you set the page layout for the an individual data table row to be viewed

      if(isset($_GET['act']) && $_GET['act'] == "view")

      {

      $id = $_GET['id'];

      $sql = mysql_query("SELECT * FROM dataTable1 WHERE id = '$id'");

      while($r = mysql_fetch_array($sql))

      {

      $year = $r['year'];

      $make = $r['make'];

      $model = $r['model'];

      $series = $r['series'];

      //echo "$year $make $model $series";
      header("Location: http://www.domain.com/description.php?id=$id");

      }

      }else{


      //this would be where you would view the pagination

      $sql = mysql_query("SELECT * FROM dataTable1 order by make, year asc LIMIT ".$limits.",$max") or die(mysql_error());

      //the total rows in the table

      $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM dataTable1"),0); 

      //the total number of pages (calculated result), math stuff...

      $totalpages = ceil($totalres / $max);


      while($r = mysql_fetch_array($sql))

      {

      $id = $r['id'];

      $year = $r['year'];

      $make = $r['make'];

      $model = $r['model'];

      $series = $r['series'];


      echo "<a href='Pagination-Example.php?act=view&id=$id'>$year $make $model $series</a>";

      }

      echo "<br><br><br><br><br>";

      echo "<center>";

      for($i = 1; $i <= $totalpages; $i++){

      //this is the pagination link
      
      echo "<a href='Pagination-Example.php?page=$i'>$i</a>   ";

      }

      echo "</center>";

      }

?>

 

 

Link to comment
Share on other sites

ok - so I have been playing around with this code and I finally figured out how to create my "Next" and "Previous" links, but I still cannot figure out how to remove the pagination link for the page I am currently on. Can some one at least tell me how to do that.

 

 

Example

 

Pagination URL: PHP-Pagination-Beta-Test.php?page=2

 

Pagination Links: 1 2 3

 

 

Here Is My Updated Code

 

<?php


      $host = "localhost"; //your sql host, usually 'localhost'

      $user = "username"; //username to connect to database

      $pass = "password"; //password to connect to database

       $db = "List"; //the name of the database

      mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());

      mysql_select_db($db) or die("ERROR DB:".mysql_error());


      $max = 1; //amount of data table rows per page. change to what to want

      $page = $_GET['page'];

      if(empty($page))

      {

      $page = 1;

      }

      $limits = ($page - 1) * $max;

      //this section is where you set the page layout for the an individual data table row to be viewed

      if(isset($_GET['act']) && $_GET['act'] == "view")

      {

      $id = $_GET['id'];

      $sql = mysql_query("SELECT * FROM dataTable1 WHERE id = '$id'");

      while($r = mysql_fetch_array($sql))

      {

      $year = $r['year'];

      $make = $r['make'];

      $model = $r['model'];

      $series = $r['series'];

      }

      }else{


      //this would be where you would view the pagination

      $sql = mysql_query("SELECT * FROM dataTable1 order by make, year asc LIMIT ".$limits.",$max") or die(mysql_error());

      //the total rows in the table

      $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM dataTable1"),0); 

      //the total number of pages (calculated result), math stuff...

      $totalpages = ceil($totalres / $max);


      while($r = mysql_fetch_array($sql))

      {

      $id = $r['id'];

      $year = $r['year'];

      $make = $r['make'];

      $model = $r['model'];

      $series = $r['series'];


      echo "<a href='Pagination-Example.php?act=view&id=$id'>$year $make $model $series</a>";

      }

      echo "<br><br><br><br><br>";

      echo "<center>";

      $back = $page - 1;

if ($page > 1) {

      $startlink="<a href='PHP-Pagination-Beta-Test.php?page=$back'>Prevoius";
      $endlink="</a>";
}
else {

      $startlink="";
      $endlink="";
}

      echo "$startlink$endlink ";

      for($i = 1; $i <= $totalpages; $i++){

      //this is the pagination link
      
      echo "<a href='PHP-Pagination-Beta-Test.php?page=$i'>$i</a>   ";

      }

$stopoint = $totalres - 1;

if ($page <= $stopoint) {
      $next = $page + 1;

      $startlink2="<a href='PHP-Pagination-Beta-Test.php?page=$next'>Next";
      $endlink2="</a>";
}
else {
      $startlink2="";
      $endlink2="";
}

      echo "$startlink2$endlink2";

      echo "</center>";

      }

?>

Link to comment
Share on other sites

Well since no one seems to know how to do this; I solved the script on my own. The PHP pagination now has: "Previous" link, "Next" link, and Numeric links that automatically remove the link of the corresponding numeric pagination; for the current page you are on. I hope someone can profit and/or save time with this script; that is why I have posted the final code below.

 

Here Is The Final PHP Pagination Script

 

<?php


      // Title: PHP Pagination (Previous, Next, & Numerical Style Pagination Links)
      // Note: This file would be named "PHP-Pagination-Beta-Test.php" (rename as needed)


      $host = "localhost"; //your sql host, usually 'localhost'

      $user = "username"; //username to connect to database

      $pass = "password"; //password to connect to database

       $db = "database_name"; //the name of the database

      mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());

      mysql_select_db($db) or die("ERROR DB:".mysql_error());


      $max = 1; // This is the amount of data table rows you want to display per page (change as you desire)

      $page = $_GET['page'];

      if(empty($page))

      {

      $page = 1;

      }

      $limits = ($page - 1) * $max;

      // This section is where you set the page layout for the an individual data table row to be viewed

      if(isset($_GET['act']) && $_GET['act'] == "view")

      {

      $id = $_GET['id'];

      $sql = mysql_query("SELECT * FROM data_table_name WHERE id = '$id'");

      while($r = mysql_fetch_array($sql))

      {

      $products = $r['mystuff'];

      }

      }else{


      // This would be where you would view the pagination

      $sql = mysql_query("SELECT * FROM data_table_name order by make, year asc LIMIT ".$limits.",$max") or die(mysql_error());

      // The total rows in the table

      $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM data_table_name"),0); 

      // The total number of pages (calculated result), math stuff...

      $totalpages = ceil($totalres / $max);


      while($r = mysql_fetch_array($sql))

      {

      $products = $r['mystuff'];


      echo "$products";

      }

      echo "<br><br><br><br><br>";

      echo "<center>";

      $back = $page - 1;

      // This checks to see if the "Previous" link should be displayed

      if ($page > 1) {

      	$startlink="<a href='PHP-Pagination-Beta-Test.php?page=$back'>Prevoius";
      	$endlink="</a>";
      }
      else {

      $startlink="";
      $endlink="";
      }

      echo "$startlink$endlink ";

      for($i = 1; $i <= $totalpages; $i++){

      // This removes link for the corresponding pagination number of the current page you are on
      
      if ($page == $i) {
      	$plb="";
      	$ple="";
      }
      else {
      	$plb="<a href='PHP-Pagination-Beta-Test.php?page=$i'>";
        $ple="</a>";
      }

      // This is the pagination link
      
      echo "$plb$i$ple   ";

      }

      $stopoint = $totalres - 1;

      // This checks to see if the "Next" link should be displayed

      if ($page <= $stopoint) {
      	$next = $page + 1;
$startlink2="<a href='PHP-Pagination-Beta-Test.php?page=$next'>Next";
      	$endlink2="</a>";
      }
      else {
      	$startlink2="";
      	$endlink2="";
      }

      echo "$startlink2$endlink2";

      echo "</center>";

      }


?>

 

 

PS: Thanks for all your help on this script PHPFreaks Forum Helpers - LOL!!! - Yeah Right !!! (I did this all on my own - so what you think about that teng84 ?!!!).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.