Jump to content

[SOLVED] How To Limit Visible Pagination Numbers?


phpQuestioner

Recommended Posts

[code=php:0]<?
$sql = mysql_query("select * from `table`");
$num = mysql_num_rows($sql);
if(mysql_num_rows($sql)){
$page_number =(!$_GET['p'])? 1:$_GET['p'];
$limit_v =(($page_number * $limit) - $limit));
$sql_2 = mysql_query("select * from `table` order by `date` desc limit $limit_v, $limit");

while($data = mysql_fetch_array($sql_2)){
	# data here
}

$prev =($page_number>1)? "[ <a href=\"page.php?p=".($page_number - 1)."\">Prev</a> ] ": "[ Prev ]";

$numofpages = ceil($num/ $limit);

for($i=$page_number;$i<($page_number + 5);$i++){
	$pages.= ($i == $page_number)? "[ $i ] ":"[ <a href=\"page.php?p=".$i."\">$i</a> ]";
}

$next =($page_number<$num)? "[ Next ]":"[ <a href=\"page.php?p=".($page_number + 1)."\">Next</a> ]";

if($num>$limit) print $page_prev . $pages . $page_next;
}else{
print "no results";
}
?>

[/code]

xyn, I tried your code; but it just gave me a blank page - this is how I did it:

 

<?php
mysql_connect("localhost","username","password");
mysql_select_db("Statistics");
$sql = mysql_query("select * from vistiors");
$num = mysql_num_rows($sql);
if(mysql_num_rows($sql)){
$page_number =(!$_GET['p'])? 1:$_GET['p'];
$limit_v =(($page_number * $limit) - $limit));
$sql_2 = mysql_query("select * from visitors limit $limit_v, $limit");

while($data = mysql_fetch_array($sql_2)){
	// data here
                $test=$data["mycount"];
                echo "$test<br>";
}

$prev =($page_number>1)? "[ <a href=\"11-26-2007.php?p=".($page_number - 1)."\">Prev</a> ] ": "[ Prev ]";

$numofpages = ceil($num/ $limit);

for($i=$page_number;$i<($page_number + 5);$i++){
	$pages.= ($i == $page_number)? "[ $i ] ":"[ <a href=\"11-26-2007.php?p=".$i."\">$i</a> ]";
}

$next =($page_number<$num)? "[ Next ]":"[ <a href=\"11-26-2007.php?p=".($page_number + 1)."\">Next</a> ]";

if($num>$limit) print $page_prev . $pages . $page_next;
}else{
print "no results";
}
?>

 

 

cooldude832 - I am not quit sure what you mean. Right now I am using $pganteqty to for the number pagination. It is in a for loop and the $pganteqty variable is set to the maximum number of page results per page * $i. how would I do what your saying with my code below?

 

This is the code I have right now:

 

<?php

// connect 
// select db

$thispage = $_SERVER['PHP_SELF'];

$amount = $_GET['skip'];

$next = $amount + $display;

$back = $amount - $display;

$results = mysql_query("select * from $mydt LIMIT $amount, $display");

$maxed_out = mysql_num_rows(mysql_query("select * from $mydt"));

$total_amount_of_pgs = $maxed_out / $display;

$currentpg = $amount / $display;

$tp = ceil($total_amount_of_pgs);

if ($currentpg > $total_amount_of_pgs)
{
$currentpg = $total_amount_of_pgs;
}

while($this=mysql_fetch_array($results)) {

// declare variables
$myfield=$this["field1"];

// display content
echo "$myfield<br>";

}

// Pagination

echo "<br><center>";

echo "<a href=\"$thispage?skip=$back\">Previous<a>"; 

for ($i = 0; $i < $tp; $i++)
{
$pganteqty = $display * $i;
$ci = $i + 1;
  echo " <a href=\"$thispage?skip=$pganteqty\">$ci</a> ";
}

echo "<a href=\"$thispage?skip=$next\">Next<a>";

echo "<br><br>";

echo "Viewing Page ". floor($currentpg + 1) ."  of  ". ceil($total_amount_of_pgs) ."";

echo "</center>";

?>

ok - here is what I have come up with so far; but it still is not displaying just 5 pagination number at a time. what it does is display 1-5 and then adds an additional pagination number/url, for the current page, to the end of the pagination (between 1-5 and "Next"). Anyone have any suggestions on how I can tweak this to display a pagination set of only 5 numbers/urls at one time? I know this is advance pagination, but I know it can be done.

 

<?php

// connect 
// select db

$thispage = $_SERVER['PHP_SELF'];

$amount = $_GET['skip'];

$next = $amount + $display;

$back = $amount - $display;

$results = mysql_query("select * from $mydt LIMIT $amount, $display");

$maxed_out = mysql_num_rows(mysql_query("select * from $mydt"));

$total_amount_of_pgs = $maxed_out / $display;

$currentpg = $amount / $display;

$tp = ceil($total_amount_of_pgs);

if ($currentpg > $total_amount_of_pgs)
{
$currentpg = $total_amount_of_pgs;
}

while($this=mysql_fetch_array($results)) {

// declare variables
$myfield=$this["field1"];

// display content
echo "$myfield<br>";

}

// Pagination

echo "<br><center>";

echo "<a href=\"$thispage?skip=$back\">Previous<a>"; 

$MAX_LINKS = 5;
$counter = 0; 

for ($i = 0; $i < $tp; $i++)
{
$pganteqty = $display * $i;
$ci = $i + 1;
if ($pganteqty == $amount) {
  echo " <b>$ci</b> ";
}
else if ($counter < $MAX_LINKS) {
  echo " <a href=\"$thispage?skip=$pganteqty\">$ci</a> ";
}
}

echo "<a href=\"$thispage?skip=$next\">Next<a>";

echo "<br><br>";

echo "Viewing Page ". floor($currentpg + 1) ."  of  ". ceil($total_amount_of_pgs) ."";

echo "</center>";

?>

<?
$totalpage = 100;
$page =1;// ppages comming from url
$pagelimit=3;//3row
$p=(int)($page/$pagelimit)+1;
$pagedisplay =$p*$pagelimit; 
$pages = $pagedisplay - 3+1 ;
for ($x = $pages;$x<=$pagedisplay;$x++){
echo $x;
}
?>

just the logic of paging....sorry im lazy to do it with your code  nay way you can have a good start out of that

 

 

teng84 - I incorporated your code with my code; but the paginations numbers do not change nor does the pagination number's urls. The pagination stays at 1 threw 3 and the urls still are the same; even when I am on pagination 4 or higher.

 

Here Is What I Did:

 

<?php

// connect variables

$mydt="visitors"; // data table
$display="15"; // total amount of rows to display per page

$thispage = $_SERVER['PHP_SELF'];

//connect
mysql_connect($mylocalhost,$myusername,$mypassword);
//select db
mysql_select_db($mydb);

$amount = $_GET['skip'];

$next = $amount + $display;

$back = $amount - $display;

if ($amount <= 0)
{
$amount="0";
$back="0";
}

$results = mysql_query("select * from $mydt LIMIT $amount, $display");

$maxresults = mysql_query("select * from $mydt");

$maxed_out = mysql_num_rows($maxresults);

if ($next >= $maxed_out)
{
$next = $amount;
}

if ($skip >= $maxed_out)
{
header("Location: ". $_SERVER[php_SELF] ."");
}

$total_amount_of_pgs = $maxed_out / $display;

$currentpg = $amount / $display;

$tp = ceil($total_amount_of_pgs);

if ($currentpg > $total_amount_of_pgs)
{
$currentpg = $total_amount_of_pgs;
}

while($this=mysql_fetch_array($results)) {

// declare variable here
$myfield=$this["field1"];

// display content here
echo "$myfield<br>";

}

// MySQL Pagination Navigation

echo "<span id=\"paige\"><br><center>";

if ($skip > "0")
{
echo "<a href=\"$thispage?skip=$back\">Previous</a>"; 
}

$totalpage="100";
$page="1";// ppages comming from url
$pagelimit="3";//3row
$p=(int)($page/$pagelimit)+1;
$pagedisplay= $p * $pagelimit; 
$pages = $pagedisplay - 3+1;
for ($x = $pages;$x<=$pagedisplay;$x++){
echo " <a href=\"". $_SERVER['PHP_SELF'] ."?page=". ($display*$x) ."\">$x</a> ";
}

if ($test != $maxed_out)
{
echo "<a href=\"$thispage?skip=$next\" style=\"padding:5px 15px 5px 15px\">Next</a>";
}

echo "</span><br><br>";

echo "Viewing Page ". floor($currentpg + 1) ."  of  ". ceil($total_amount_of_pgs) ."";

echo "</center>";

?>

 

also, I actually think my original code is pretty cool; it just needs to be tuned-up a little bit. you don't see pagination with a mysql limit very often; I though it was a pretty good and different idea.

 

 

  Quote
nay way you can have a good start out of that

 

see this three lines

 

$totalpage = 100;

$page =1;// ppages coming from url

$pagelimit=3;//3row

 

those are the lines you should change there might be a little error but for sure you can fix that..

 

i tried that using those static value and i get

123  456 789 etc....

well I finally got this bad boy under control and working the way I wanted it to; after 2 days of playing around with this script and trying to do things the hard way; to make this work; it just came to me about 30 minutes ago what to do, to make this work and I was quit happy about that. ;D

 

Special Thanks Go Out To Every One Who Help Me On This Script And Especially teng84 For Helping Me With That Last Bit of Code - Below Is My Final Code:

 

<?php

$mylocalhost=""; // localhost
$myusername=""; // username
$mypassword=""; // password
$mydb=""; // database
$mydt=""; // data table
$display="15"; // total amount of rows to display per page

/*
******************************************************************
- Do Not Edit Below This Point
******************************************************************
*/

$thispage = $_SERVER['PHP_SELF'];

//connect
mysql_connect($mylocalhost,$myusername,$mypassword);
//select db
mysql_select_db($mydb);

$amount = $_GET['skip'];

$paige = $amount * $display;

if ($amount <= 0)
{
$amount="0";
$back="0";
}

$results = mysql_query("select * from $mydt LIMIT $paige, $display");

$maxresults = mysql_query("select * from $mydt");

$maxed_out = mysql_num_rows($maxresults);

if ($next >= $maxed_out)
{
$next = $amount;
}

if ($skip >= $maxed_out)
{
header("Location: ". $_SERVER[php_SELF] ."");
}

$total_amount_of_pgs = $maxed_out / $display;

$currentpg = $amount / $display;

$tp = ceil($total_amount_of_pgs);

if ($currentpg > $total_amount_of_pgs)
{
$currentpg = $total_amount_of_pgs;
}

while($this=mysql_fetch_array($results)) {

/*
******************************************************************
- Do Not Edit Above This Point
******************************************************************
*/

// declare variable here
$myfield=$this["hit_number"];

// display content here
echo "$myfield<br>";

}

// MySQL Pagination Navigation

echo "<span id=\"paige\"><br><center>";

if ($skip > "0")
{
echo "<a href=\"$thispage?skip=". ($skip-1) ."\">Previous</a>"; 
}

// Pagination Process

$pagelimit="3";// 3 Rows
$p=(int)($skip/$pagelimit)+1;
$pagedisplay= $p * $pagelimit; 
$skip2 = $pagedisplay - 3+1;
for ($x = $skip2;$x<=$pagedisplay;$x++){
if ($myfield != $maxed_out) {
echo " <a href=\"". $_SERVER['PHP_SELF'] ."?skip=". ($x-1) ."\">". $x ."</a> ";
}
else {
echo " <a href=\"". $_SERVER['PHP_SELF'] ."?skip=". ($x-3) ."\">". ($x-2) ."</a> ";
}
}

if ($myfield != $maxed_out)
{
echo "<a href=\"$thispage?skip=". ($skip+1) ."\">Next</a>";
}

echo "</span><br><br>";

$skipper = $skip + 1;

if ($skipper < 1) {
$skipper="1";
}

echo "Viewing Page ".$skipper."  of  ". ceil($total_amount_of_pgs) ."";

echo "</center>";

?>

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.