/    Sign up×
Community /Pin to ProfileBookmark

Hi !!

I am having a problem with the page results.

My database has a lot of data, so in my page results it is showing over 30 pages but I would like to show the first 10 page numbers and then just add one more as I move to the next page number.

Maybe my script will make it clear:

[code=php]
<?
$total_results = mysql_result(mysql_query(“SELECT COUNT(*) as Num FROM accommodation”),0);

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

echo “<strong><font color=’#003366′ size=’2′>Pages: </font></strong>”;

if($page > 1){
$prev = ($page – 1);
echo “<a href=””.$_SERVER[‘PHP_SELF’].”?page=$prev&area=$area&acc_category_id=$category”><strong><font size=’2′>Previous</font></strong></a>&nbsp;”;
}
for($i=1; $i <= $total_pages; $i++ ){
if(($page) == $i){
echo “<font color=’#FF6633′ size=’2′><strong>$i&nbsp;</font></strong>”;
} else {
echo “<a href=””.$_SERVER[‘PHP_SELF’].”?page=$i&area=$area&acc_category_id=$category”><strong><font size=’2′>$i<br></font></strong></a>&nbsp;”;
}
}
if($page < $total_pages){
$next = ($page + 1);
echo “<a href=””.$_SERVER[‘PHP_SELF’].”?page=$next&area=$area&acc_category_id=$category”><strong><font size=’2′>Next>></font></strong></a>”;
}
echo “</center>”;
?>[/code]

As you can see above it keeps adding page numbers. How can I show only 10 page numbers just like google result page.

Thanks for any help

😮

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsApr 03.2005 — example:[code=php]<?php
$pg = $_GET['pg'];
$ppg = 10; // items per page
$total = 100;

if($total > $ppg)
{
for($i = 1; $i <= ceil($total / $ppg); $i++)
{
if($i <= 10 && $i >= 10)
{
// output link to pages in range
}
if($i >= 10)
{
break; // no need to continue looping
}
}
}
?>[/code]
Copy linkTweet thisAlerts:
@sandro27authorApr 03.2005 — Thanks for the reply.

One more thing.

The following code shows me I am in the current page:
[code=php]
if(($page) == $i){
echo "<font color='#FF6633' size='2'><strong>$i&nbsp;</font></strong>";
}
[/code]


How can I use this line of code along with the line you provided ?

The entire code :
[code=php]<?
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM accommodation"),0);

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

echo "<strong><font color='#003366' size='2'>Pages: </font></strong>";

if($page > 1){
$prev = ($page - 1);
echo "<a href="".$_SERVER['PHP_SELF']."?page=$prev&area=$area&acc_category_id=$category"><strong><font size='2'>Previous</font></strong></a>&nbsp;";
}
for($i=1; $i <= $total_pages; $i++ ){

if($i <= 10 && $i >= 10){
echo "<font color='#FF6633' size='2'><strong>$i&nbsp;</font></strong>";
}
if($i >= 10)
{
break; // no need to continue looping
}
else {
echo "<a href="".$_SERVER['PHP_SELF']."?page=$i&area=$area&acc_category_id=$category"><strong><font size='2'>$i</font></strong></a>&nbsp;";
}
}
if($page < $total_pages){
$next = ($page + 1);
echo "<a href="".$_SERVER['PHP_SELF']."?page=$next&area=$area&acc_category_id=$category"><strong><font size='2'>Next>></font></strong></a>";
}
echo "</center>";
?>[/code]


If you look into it , you will see the current page will always be 10.

?
Copy linkTweet thisAlerts:
@ShrineDesignsApr 03.2005 — [code=php]<?php
$pg = $_GET['pg'];
$ppg = 10; // items per page
$total = 100;

if($total > $ppg)
{
for($i = 1; $i <= ceil($total / $ppg); $i++)
{
if($i <= ($pg + 10) && $i >= ($pg + 10))
{
// output link to pages in range
echo ($i == $pg) ? "<b>$i</b>n" : "<a href="page.php?pg=$i">$i</a>n";
}
if($i >= ($pg + 10))
{
break; // no need to continue looping
}
}
}
?> [/code]
×

Success!

Help @sandro27 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...