Jump to content

Help with pagination code


gibbo1715

Recommended Posts


All

 

Firstly i know there is a load of stuff here about pagination but i ve still not quite got my head round it as i would like so sorry for asking another question on the subject

 

below is the simplist pagination script i ve managed to find and get working so far

 

<?php

mysql_connect("localhost","root","");
mysql_select_db("shareddb");

$perpage = 10;
$lynx = $html = "";
$startat = $_REQUEST

 * $perpage;

$q = mysql_query("select count(id) from countries");
$row = mysql_fetch_array($q);
$pages = ($row[0] + $perpage - 1) / $perpage;

$q = mysql_query("select * from countries order by id desc limit $startat,$perpage");

while ($row = mysql_fetch_assoc($q)) {
        $text = strip_tags($row[name]);
        $text = substr($text,0,300);
        $html .= "<dt>$row[id] - <a href=/$row[id]_.html>$row[name]</a></dt>";
        $html .= "<dd>$text ....<br><br></dd>";
        };

for ($k=0; $k<$pages; $k++) {
        if ($k != $_REQUEST

) {
         $lynx .= " <a href=$PHP_SELF"."?page=$k>".($k+1)."</a>";
        } else {
         $lynx .= " <b>--[".($k+1)."]--</b>";
        }
}
?>

<html><head>
<title>Showing entries</title>
<body><h2>Here are the entries you selected - page [<?= $_REQUEST

+1 ?>]:</h2><br>
<?= $html ?>
Please choose the next page you want to view:
[<?= $lynx ?>]
</body> 

 

 

this is great for adding a number as the pages increase but how would i get it to only show say Prev.. [1] 2 3 4 5 ..next

 

thanks

 

there are a lot of good examples out there but i cant figure out how to include my content with the pagination

 

thanks

 

gibbo

 

 

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

Download my php pagination class from the link at the bottom of the page and save it as pagination.class.php in the same folder as this php file. Then change your code to the following

<?php

mysql_connect("localhost","root","");
mysql_select_db("shareddb");
//Inlcude the class
include('pagination.class.php');

$query = "SELECT * FROM countries ORDER BY id DESC";
$page = isset($_GET['page']) ? $_GET['page'] : '1';

$paginator = new pagination($page, $query);
$paginator->results_per_page = 10;
$paginator->padding(3);
$paginator->link_prefix = '?page=';
$paginator->link_suffix = '';
$paginator->page_nums_separator = ' | ';

$lynx = $paginator->paginate();

while ($row = mysql_fetch_assoc($paginator->resource())) {
        $text = strip_tags($row[name]);
        $text = substr($text,0,300);
        $html .= "<dt>$row[id] - <a href=/$row[id]_.html>$row[name]</a></dt>";
        $html .= "<dd>$text ....<br><br></dd>";
        };
?>

<html><head>
<title>Showing entries</title>
<body><h2>Here are the entries you selected - page [<?= $page ?>]:</h2><br>
<?= $html ?>
Please choose the next page you want to view:
[<?= $lynx ?>]
</body>

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.