Jump to content

Problem with pagination code


MATLAB

Recommended Posts

Can somebody please help me sort this code out I have wirtten/got from a tutorial from phpfreaks - http://www.phpfreaks.com/tutorial/basic-pagination.

 

The code correctly knows the number of pages needed in order to display the records from the database, but it displays all records on each page.

 

I have 4 records and want 2 records to be shown on each page, but the code below displays all 4 records on both pages.

 

Can someone please shed some light onto how I can display the first 2 records on the first page, and the second 2 records on the second page?

 

code below:

<?php

//Connect to the database
$user="username"; 
$password="password";
$database="database";
$con = mysql_connect("localhost",$user,$password) or die ('Could not connect: ' . mysql_error());
mysql_select_db($database, $con) or die( "Unable to select database");


// Determine number of rows in database
$query = mysql_query("SELECT * FROM vids");
$numrows=mysql_num_rows($query);

// number of rows to show per page
$rowsperpage = 2;

// 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 {
   $currentpage = 1;
}

// 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;
} 

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

// get the info from the db 
$result = mysql_query("SELECT * FROM vids");
while($row = mysql_fetch_array($result))
  {
  
$id = $row['id'];  
$url = $row['url'];
$page_url = $row['page_url'];;
$title = $row['title'];
$desc = $row['desc'];
$date_add = $row['date_add'];
$date_rec = $row['date_rec'];
$place = $row['place'];
$altitude = $row['altitude'];
$jump_no = $row['jump_no'];

//Display video
echo "<div class='sky_cont'> 
    <div class='sky_vid'><a class='video' href=\"$url\"><img src='http://www.netlinksurveyors.co.uk/test/images/lgo.jpg' alt=\"$title\" Border='0' /></a></div>
<p><h4><a href=\"$page_url\" target='_blank'>$title</a></h4>$desc</p>
    </div>";
}


/******  build the pagination links ******/
// 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> ";
} 

// range of num links to show
$range = 3;

// 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> ";
      }
   } 
}

// 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 ******/


?>


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

The query that retrieves the rows to be displayed on any page, very specifically used a LIMIT clause to accomplish getting the correct rows -

 

$sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";

 

Your query would need to do the same.

 

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.