Jump to content

problem with paginate


dayang

Recommended Posts

i have problem with my coding where i have several data to paginate.The first page the data will display but after i click to second page it just view empty page.can anyone help me..

this is my coding

 

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

$max_results = 10;

$from = (($page * $max_results) - $max_results);

$sql = mysql_query("SELECT * FROM inbox LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){

echo $row['title']."<br/>";
}


$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM inbox"),0);
echo $total_results;


$total_pages = ceil($total_results / $max_results);

echo "<center>Pilih halaman<br/>";

if($page < 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Sebelum</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}


if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Lagi</a> ";
}
echo "</center>";
?>

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

Your code is wrong. Please look at this tutorial: http://www.phpfreaks.com/tutorial/basic-pagination

 

LOL! Well this is helpful, now isn't it!

 

Yes, yes it is. Should we re-invent an inferior and less comprehensive tutorial just for the sake of this thread? He won't learn half as much as he would from CV's introductory tutorial on it.

Your code is wrong. Please look at this tutorial: http://www.phpfreaks.com/tutorial/basic-pagination

 

LOL! Well this is helpful, now isn't it!

 

Yes, yes it is. Should we re-invent an inferior and less comprehensive tutorial just for the sake of this thread? He won't learn half as much as he would from CV's introductory tutorial on it.

 

Did you actually look at his code? It's clear that he understands the logic behind pagination. He simply has a minor error in this particular instance of practice.

 

Reading a 10-page tutorial clearly is not going to help this user.

<?php   
//connection info   
  
// find out how many rows are in the table    
$sql = "SELECT COUNT(*) FROM inbox";   
$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 inbox 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      
} // 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 ******/  
?>  

i have problem with my coding where i have several data to paginate.The first page the data will display but after i click to second page it just view empty page.can anyone help me..

this is my coding

 

if(!isset($_GET['page'])){
$page = 1;
} else {
$page = $_GET['page'];
}

$max_results = 10;

$from = (($page * $max_results) - $max_results);

$sql = mysql_query("SELECT * FROM inbox LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){

echo $row['title']."<br/>";
}


$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM inbox"),0);
echo $total_results;


$total_pages = ceil($total_results / $max_results);

echo "<center>Pilih halaman<br/>";

if($page < 1){
$prev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Sebelum</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "$i ";
} else {
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}


if($page < $total_pages){
$next = ($page + 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Lagi</a> ";
}
echo "</center>";
?>

 

 

You should set $page OUTSIDE of your if statements, otherwise variable scope will bend you over the table.

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.