Jump to content

[SOLVED] Pagination HELP!!!


almystersv

Recommended Posts

Hi Guys,

 

So far I have come up with this code which does display the first 10 products in my database but the links to the Next 10, PREV etc are not showing up as links.

 

Here is my code

<?php 
require "connect.php";

    $limit          = 10;                
    $query_count    = "SELECT count(*) FROM product";     
    $result_count   = mysql_query($query_count);     
    $totalrows      = mysql_num_rows($result_count);  

    if(empty($page)){ 
        $page = 1; 
    } 
         

    $limitvalue = $page * $limit - ($limit);  
    $query  = "SELECT * FROM product LIMIT $limitvalue, $limit";         
    $result = mysql_query($query) or die("Error: " . mysql_error());  

    if(mysql_num_rows($result) == 0){ 
        echo("Nothing to Display!"); 
    } 

    $bgcolor = "#E0E0E0"; // light gray 

?>
<table width ="100%" border="1">
<?php 
     
    while($row = mysql_fetch_array($result)){ 
        if ($bgcolor == "#E0E0E0"){ 
            $bgcolor = "#FFFFFF"; 
        }else{ 
            $bgcolor = "#E0E0E0"; 
        } 

    echo("<tr bgcolor=".$bgcolor.">\n<td>"); 
    echo($row["URN"]); 
    echo("</td>\n<td>"); 
    echo($row["productName"]); 
    echo("</td>\n</tr>"); 
    }

    echo("</table>"); 

    if($page != 1)
{  
        $pageprev = $page-1;  
        ?>
	<a href="StationaryOrdersTEST.php&page=$pageprev">PREV".$limit."</a>
<?php  }
else
{ 
        echo("PREV".$limit." "); 
    } 

    $numofpages = $totalrows / $limit;  
     
    for($i = 1; $i <= $numofpages; $i++){ 
        if($i == $page){ 
            echo($i." "); 
        }else{ 
         ?>
	 <a href="StationaryOrdersTEST.php?page=$i">$i</a>
	 <?php 
        } 
    } 


    if(($totalrows % $limit) != 0){ 
        if($i == $page)
	{ 
            echo($i." "); 
        }
	else{ 
	?>
	 <a href="StationaryOrdersTEST.php?page=$i">$i</a>
	 <?php
        } 
    } 

    if(($totalrows - ($limit * $page)) > 0){ 
        $pagenext = ++$page; 
          
        ?>
	<a href="StationaryOrdersTEST.php?page=$pagenext">NEXT".$limit."</a>
	<?php
    }else{ 
        echo("NEXT".$limit);  
    } 
     
    mysql_free_result($result); 

?> 

 

Any help would be grand.

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/90069-solved-pagination-help/
Share on other sites

There is a bug in the PHPfreaks pagination tutorial, here is a revised version of the tutorial

 

<?php

$limit          = 10;
$query_count    = "SELECT count(*) FROM table";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);

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

$offset = ($page - 1) * $limit;
$query  = "SELECT * FROM table LIMIT $offset, $limit";

echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

while ($row = mysql_fetch_array($result)) {
    echo $row['col']."</br>";
}


//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"index.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"index.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"index.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

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.