0

i'm trying to make my own pagination system ignores the new data if someone trying to open the next page:

example:

i have this table :

1. apple
2. orange 
3. banana
4. burger
5. pizza 
6. spaghetti

and i only show 3 rows for each page.

mysql query:

// $page = 0 for first page
$sql = "SELECT * FROM mytable ORDER BY id DESC LIMIT $page, 3";

the result is like this

page #1 :

6. spaghetti
5. pizza
4. burger

page #2 :

3. banana
2. orange
1. apple

let's say right now I'm in page #1 and someone else added 3 new rows:

water
juice
coffee

what will happen now is if i go to page #2 i will get a repeated page !

now page #2 is gonna be :

6. spaghetti
5. pizza
4. burger

the reason of that is because page #1 is showing the new data:

 9. water
 8. juice
 7. coffee

how can i stop this to happen if the user didn't refresh the first page and only wanted to view the second page ?

it's a problem especially if you're trying to make your pagination looks just like twitter!

Loving Android
  • 253
  • 5
  • 15

3 Answers3

0
// find out how many rows are in the table 
$sql = "SELECT COUNT(*) FROM mytable";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   // cast var as int
   $currentpage = (int) $_GET['currentpage'];
} else {
   // default page num
   $currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
   // set current page to last page
   $currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
   // set current page to first page
   $currentpage = 1;
} // end if

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db 
$sql = "SELECT * FROM mytable ORDER BY id DESC LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
   // echo data
   echo $list['id'] . " : " . $list['number'] . "<br />";
} // end while

/******  build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
   // show << link to go back to page 1
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if 

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   // if it's a valid page number...
   if (($x > 0) && ($x <= $totalpages)) {
      // if we're on current page...
      if ($x == $currentpage) {
         // 'highlight' it but don't make a link
         echo " [<b>$x</b>] ";
      // if not current page...
      } else {
         // make it a link
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      } // end else
   } // end if 
} // end for

// if not on last page, show forward and last page links        
if ($currentpage != $totalpages) {
   // get next page
   $nextpage = $currentpage + 1;
    // echo forward link for next page 
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   // echo forward link for lastpage
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} // end if
/****** end build pagination links ******/
?>
Md Nazrul Islam
  • 363
  • 4
  • 13
  • that's how i do it but still gonna to be updated if new rows added and then page # 2 will be just like page #1 , read my example again please , thank you bro – Loving Android Mar 27 '18 at 04:47
  • every time you will pick data order by id desc and it is it's solution. you will get descending order and display desc order id is enough... – Md Nazrul Islam Mar 27 '18 at 04:50
  • the result on page 1 will be pushed to page 2 when other user insert data . i want this only happen if the user refreshed page 1 . if he didn't , he can go to page 2 without seeing the pushed rows . – Loving Android Mar 27 '18 at 07:04
0

You can use something like this to get proper pagination for your table items. You can remove ORDER BY & DESC to know how the sql query result changes.

$page; // $page is current page
$result = 3; // As you need 3 results per page.
$offset = ($page * $result) - $result;

$sql = "SELECT * FROM mytable ORDER BY id DESC LIMIT $result OFFSET $offset";
  • bro, i know all that!, what i'm talking about , if i was in page 1 and didn't refresh it, then someone added 3 new rows , what will happen is when i go to page 2 , i will see the previous rows not the next rows , because new data added and become on page 1 – Loving Android Mar 27 '18 at 06:11
  • i think i will need SESSION or cache to do that , thanks for trying – Loving Android Mar 27 '18 at 06:12
  • the result on page 1 will be pushed to page 2 when other user insert data . i want this only happen if the user refreshed page 1 . if he didn't , he can go to page 2 without seeing the pushed rows . – Loving Android Mar 27 '18 at 07:04
0

i had to use session to make it work as i want it , and here's how i solved it:

$count = $PDO->query("SELECT id FROM mytable")->rowCount(); //count rows
if(!isset($_SESSION['rowCount']))
{
 $_SESSION['rowCount'] = $count; // save row counts 
}

if(isset($_GET['page']))
{
   $offset = ($_GET['page'] -1) * 3 + $count - $_SESSION['rowCount'];
}
else
  $offset = 0;
  $sql = $PDO->prepare("SELECT * FROM mytable ORDER BY id DESC LIMIT $offset, 3");
  $sql->execute();

also i unset the session when user comes back to page 1.

Loving Android
  • 253
  • 5
  • 15