Forum Moderators: coopster

Message Too Old, No Replies

Problems with paging. Limit the numbers displayed

         

fintan

10:17 am on Aug 12, 2005 (gmt 0)

10+ Year Member



I'm having a problem with paging. I can do the paging but I want to limit the numbering. So if I had pages 1 to 30 I want 1 to 10 displayed and a next button beside the 10.

Here's what I got.

$total = total database results;
$limit = 10;
isset($_GET['page'])? $page = $_GET['page'] : $page = 1;

$numPages = ceil($total / $limit);

$offset = ($page - 1) * $limit;

$output = array_slice($database_rslts, $offset, $limit);

$value = $offset+10;
$value2 = $total-$offset;
$numbering = ceil($total / $numPages);
$numtotal = $numbering+$offset;

for($i = 1; $i <= $numPages; $i++){
if($total < $limit){}
else{
($i==1)? print "" : print " , ";
if($i == $page){echo $i;}
else{echo "<a href=\"euroauth.php?page=$i\">$i</a>";}

}
}

Any hints? Thanks

fintan.

omoutop

10:27 am on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Example of what u want....
Works fine for me, I am sending u the simplest mode...

////////////////CODE/////////////////////////////
<?php
// Set some variables for script
// To show each page by number set to Y. Set to N for prev-next links only

$showpages = "N";

// Set number of records to be displayed per page $limit=somenumber
$limit=5;

if (empty($offset))
{
$offset=0;
}

$query = 'select * from Gallery';

$result = mysql_query($query);

$numrows = mysql_num_rows($result);

if ($numrows>0)
{
$query2 = 'select * from Gallery '

."limit $offset,$limit";
$result2 = mysql_query($query2);

$numrows2 = mysql_num_rows($result2);

echo ("<h1>Image Gallery</h1>\n");
echo ("<br />\n");
echo ("<div id='gallery'>\n");
echo "<a href='Gallery.htm'>Go Back</a><p>";
while ($row=mysql_fetch_assoc($result2)){

$id = $row['image_id'];
$large = $row['large'];
$small = $row['small'];
$description = $row['description'];

echo "&nbsp;";
echo "&nbsp;";
echo "<table width='50%' border='3' bordercolor='#3300FF'>";
echo "<tr>";
echo "<td width='15%'>";
echo '<a href="';
echo $url;
echo $large;
echo '"';
echo ' target=top>';
echo '<img src="';
echo $url;
echo $small;
echo '"';
echo 'alt="';
echo $description;
echo '"';
echo '>';
echo "</a>";
echo "</td>";
echo "&nbsp;";
echo "<td>";
echo $description;
echo "&nbsp;";
echo "</td>";
echo "</tr>";
echo "</table>";
};

//determine number of pages needed

$pages=intval($numrows/$limit);

//if number of records isn't evenly divisible by the number of pages above, add another page.

if ($numrows%$limit)

{

$pages++;

}

//set value of first record to be displayed

$first_record = $offset + 1;

echo "<br />";

//see if we're on the last page

if (!((($offset)/$limit)+1==$pages))

{
$last_record = $offset + $limit;

//is last page

echo " <strong>$last_record</strong> of <strong>$numrows</strong> image(s). Click on image to enlarge<br />";

}

else

{
//is NOT last page - send this to browser

echo " <strong>$numrows</strong> of <strong>$numrows</strong> image(s). Click on image to enlarge<br>";

}

echo "<br />\n";

//prepare and display links to other results in browser

if ($offset!= 0)

{

$prevoffset=$offset-$limit;

//create a previous link - if we're not on the first page

echo "<a class='nav' href='$PHP_SELF?offset=$prevoffset'>&lt;&lt;&nbsp;Prev</a> &nbsp; \n";

}

//construct that shows just prev-next or all page numbers

if ($showpages == Y)
{
if ( $pages!= 1 )

{
for ($i=1;$i<=$pages;$i++)
{
$newoffset=$limit*($i-1);

if ( ((($offset)/$limit)==($i-1)) )
{

echo "$i &nbsp; \n";

}

else

{

echo "<a class='nav' href='$PHP_SELF?offset=$newoffset'>$i</a> &nbsp; \n";

}

}

}

}

if (!((($offset)/$limit)+1==$pages) && $pages!=1)

{

$newoffset=$offset+$limit;

//create a next link - if we're not on the last page

echo ("<a class='nav' href='$PHP_SELF?offset=$newoffset'>Next&nbsp;&gt;&gt;</a><p>\n");

}
}

else

{echo "No more records to display!";

}

echo ("</div>\n");

}

?>

fintan

10:49 am on Aug 12, 2005 (gmt 0)

10+ Year Member



Thanks omoutop, How do you calculate if your on page 10 and to display page 10 to 20

omoutop

10:56 am on Aug 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if u use my script then u set
$limit=10;
and u get 10 results per page...

example:

alnalwnlawlanlwnxal;xna;wxnklnklnklwanxkawnxla
laknxlkawnlawnlxknwlknxlaknwklaxnakxkalxnklnxla
lanlxkanxlanwlkanlkxnalknlaknakxnwalnlanxalknxl

10 of 32 Reviews(s).
1 2 3 Next Page >>

if u goto next page u get:

alnalwnlawlanlwnxal;xna;wxnklnklnklwanxkawnxla
laknxlkawnlawnlxknwlknxlaknwklaxnakxkalxnklnxla
lanlxkanxlanwlkanlkxnalknlaknakxnwalnlanxalknxl

20 of 32 Reviews(s).
<< Previous Page 1 2 3 Next Page >>

Etc....
thats they way it works

fintan

11:08 am on Aug 12, 2005 (gmt 0)

10+ Year Member



Thanks again tried it and it worked perfectly. Thanks