Jump to content

[SOLVED] Need a bit of help with Pagination


Twister1004

Recommended Posts

Hey everyone! I just got done reading about the Pagination tutorial here on PHP FREAKS! Yay go me! I actually wasn't distracted easily and finished it =D.

 

Objective: I'm getting quite an error, view it here. It's giving me an error, I, myself, have no idea what I'm supposed to do.

 

This is kinda like a copy of the tutorial so that way I can play around with it and start changing what I want and need to.

<?php
$conn = mysql_connect('localhost','root','root') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('test',$conn) or trigger_error("SQL", E_USER_ERROR);

$sql = "SELECT COUNT(*) FROM `page`";
$result = mysql_query($sql);
$getrows = mysql_fetch_row($result);
$numrows = $getrows;

//To find the total pages we have,Take the number  of rows ($numrows) and divide that by the rows per page ($rowsonpage)
//ceil will appearenly round up. (Check out what it does later!)

//number of rows shown on page
$rowsonpage = 10;
//find out total pages
$totalpages = ceil($numrows / $rowsonpage); //The error IS HERE, however I don't know what ceil is, so I have no idea how to solve it.

//get the current page
if(isset($_GET['pagenum']) && is_numeric($_GET['pagenum'])){
//This variable will be announced as a NUMBER or int
$currentpage = (int) $_GET['pagenum'];
}
else{
//This will set the DEFAULT page number
$currentpage = 1;
}
//If the current page is greater than the total amount of pages needed, Set it to LAST PAGE
if($currentpage > $totalpages){
$currentpage = $totalpages;
}
//If the page is less than the First page, Set it to the FIRST PAGE
if($currentpage < 1){
$currentpage = 1;
}
// The page offset of the list is based on the current page (Figure out later)
$offset = ($currentpage - 1) * $rowsonpage;

//Get the info from the database
$sql = "SELECT `id`, `number` FROM `page` LIMIT $offset, $rowsonpage";
$result = mysql_query($sql);

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

//-------------------------------------BUILD PAGE LINKS! --------------------------------------------
//if not on page 1 don't show the 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 numbers
$prevpage = $currentpage - 1;
//Show  < Link to go back one page
echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> < </a>";
}
// Make a range of number links (pages) to show rather than showing 500 links at once
$range = 3;
// Loop to show links to range of pages around the current one
for($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++){
// If it is a valid page number
if(($x > 0) && ($x <= $totalpages)){
	//If we are on the current page
	if($x == $currentpage){
		// "Highlight" the current page number but do NOT make  alink for it.
		echo "[<br>$x</b>]";
		//If not the current page then make it a link
	}
	else{
		echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a>";
	}
}
}
//If you are not on the last oage, show the next page and the last page
if($currentpahe != $totalpages){
//get the NEXT page
$nextpage = $currentpage + 1;
//echo the next page ( > )
echo "<a href ='{$_SERVER['PHP_SELF']}?current=$nextpage'> > </a>";
// echo the last page button
echo "<a href ='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a>";
}
//------------------------END PAGE BUILDING LINKS---------------------------
?>

 

I'm not doing plagiarism, this is so I may learn off it.

 

The problem I'm having is listed in the code i provided and in the website provided as well.

 

Any hints, tips, posts, etc. is appreciated. Thanks for your time!

Use code tags :P

<?php
$conn = mysql_connect('localhost','root','root') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('test',$conn) or trigger_error("SQL", E_USER_ERROR);

$sql = "SELECT COUNT(*) FROM `page`";
$result = mysql_query($sql);
$getrows = mysql_fetch_row($result);
$numrows = $getrows;

//To find the total pages we have,Take the number  of rows ($numrows) and divide that by the rows per page ($rowsonpage)
//ceil will appearenly round up. (Check out what it does later!)

//number of rows shown on page
$rowsonpage = 10;
//find out total pages
$totalpages = ceil($numrows / $rowsonpage); //The error IS HERE, however I don't know what ceil is, so I have no idea how to solve it.

//get the current page
if(isset($_GET['pagenum']) && is_numeric($_GET['pagenum'])){
   //This variable will be announced as a NUMBER or int
   $currentpage = (int) $_GET['pagenum'];
}
else{
   //This will set the DEFAULT page number
   $currentpage = 1;
}
//If the current page is greater than the total amount of pages needed, Set it to LAST PAGE
if($currentpage > $totalpages){
   $currentpage = $totalpages;
}
//If the page is less than the First page, Set it to the FIRST PAGE
if($currentpage < 1){
   $currentpage = 1;
}
// The page offset of the list is based on the current page (Figure out later)
$offset = ($currentpage - 1) * $rowsonpage;

//Get the info from the database
$sql = "SELECT `id`, `number` FROM `page` LIMIT $offset, $rowsonpage";
$result = mysql_query($sql);

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

//-------------------------------------BUILD PAGE LINKS! --------------------------------------------
//if not on page 1 don't show the 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 numbers
   $prevpage = $currentpage - 1;
   //Show  < Link to go back one page
   echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> < </a>";
}
// Make a range of number links (pages) to show rather than showing 500 links at once
$range = 3;
// Loop to show links to range of pages around the current one
for($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++){
// If it is a valid page number
   if(($x > 0) && ($x <= $totalpages)){
      //If we are on the current page
      if($x == $currentpage){
         // "Highlight" the current page number but do NOT make  alink for it.
         echo "[<br>$x</b>]";
         //If not the current page then make it a link
      }
      else{
         echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a>";
      }
   }
}
//If you are not on the last oage, show the next page and the last page
if($currentpahe != $totalpages){
   //get the NEXT page
   $nextpage = $currentpage + 1;
   //echo the next page ( > )
   echo "<a href ='{$_SERVER['PHP_SELF']}?current=$nextpage'> > </a>";
   // echo the last page button
   echo "<a href ='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a>";
}
//------------------------END PAGE BUILDING LINKS---------------------------
?>

Try

 

<?php
$conn = mysql_connect('localhost','root','root') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('test',$conn) or trigger_error("SQL", E_USER_ERROR);

$sql = "SELECT COUNT(*) FROM `page`";
$result = mysql_query($sql);
$getrows = mysql_fetch_row($result);
$numrows = $getrows;

//To find the total pages we have,Take the number  of rows ($numrows) and divide that by the rows per page ($rowsonpage)
//ceil will appearenly round up. (Check out what it does later!)

//number of rows shown on page
$rowsonpage = 10;
//find out total pages
$totalpages = ceil($numrows[0] / $rowsonpage); //The error IS HERE, however I don't know what ceil is, so I have no idea how to solve it.

//get the current page
if(isset($_GET['pagenum']) && is_numeric($_GET['pagenum'])){
   //This variable will be announced as a NUMBER or int
   $currentpage = (int) $_GET['pagenum'];
}
else{
   //This will set the DEFAULT page number
   $currentpage = 1;
}
//If the current page is greater than the total amount of pages needed, Set it to LAST PAGE
if($currentpage > $totalpages){
   $currentpage = $totalpages;
}
//If the page is less than the First page, Set it to the FIRST PAGE
if($currentpage < 1){
   $currentpage = 1;
}
// The page offset of the list is based on the current page (Figure out later)
$offset = ($currentpage - 1) * $rowsonpage;

//Get the info from the database
$sql = "SELECT `id`, `number` FROM `page` LIMIT $offset, $rowsonpage";
$result = mysql_query($sql);

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

//-------------------------------------BUILD PAGE LINKS! --------------------------------------------
//if not on page 1 don't show the 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 numbers
   $prevpage = $currentpage - 1;
   //Show  < Link to go back one page
   echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'> < </a>";
}
// Make a range of number links (pages) to show rather than showing 500 links at once
$range = 3;
// Loop to show links to range of pages around the current one
for($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++){
// If it is a valid page number
   if(($x > 0) && ($x <= $totalpages)){
      //If we are on the current page
      if($x == $currentpage){
         // "Highlight" the current page number but do NOT make  alink for it.
         echo "[<br>$x</b>]";
         //If not the current page then make it a link
      }
      else{
         echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a>";
      }
   }
}
//If you are not on the last oage, show the next page and the last page
if($currentpahe != $totalpages){
   //get the NEXT page
   $nextpage = $currentpage + 1;
   //echo the next page ( > )
   echo "<a href ='{$_SERVER['PHP_SELF']}?current=$nextpage'> > </a>";
   // echo the last page button
   echo "<a href ='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'> >> </a>";
}
//------------------------END PAGE BUILDING LINKS---------------------------
?>

$getrows = mysql_fetch_row($result);
$numrows = $getrows;

//To find the total pages we have,Take the number  of rows ($numrows) and divide that by the rows per page ($rowsonpage)
//ceil will appearenly round up. (Check out what it does later!)

//number of rows shown on page
$rowsonpage = 10;
//find out total pages
$totalpages = ceil($numrows[0] / $rowsonpage); //The error IS HERE, however I don't know what ceil is, so I have no idea how to solve it.

 

$numrows was an array. Since you were returning only one value from the db, that value was located at $numrows[0].

What you were trying to do was divide an array by $rowsonpage.

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.