...

View Full Version : Advance Dynamic Paging



celestine
12-23-2003, 03:10 AM
Greetings all,

I have been using a simple dynamic paging script to print out the pages for my application. it works fine until I have over 15 pages to scroll and cannot fit them nicely in a line. I saw on certain websites, they have page navigation like:

prev < 2 ... 10 > next

I was wondering how I can amend my code to do that?

The code I have is as follows:-




$result = mysql_query($sql);
$rows_per_page=15;
$total_records=mysql_num_rows($result);
$pages = ceil($total_records / $rows_per_page);

echo "<p>$total_records records in database.</p>";
if (!isset($screen))
$screen=0;
$start = $screen * $rows_per_page;
$sql .= "LIMIT $start, $rows_per_page";
$result = mysql_query($sql);

while($myrow=mysql_fetch_array($result))
{
extract($myrow);
// print out data here.
}
// create the dynamic links
if ($screen > 0) {
$j = $screen - 1;
$url = "$PHP_SELF?action=list&type=$design_id&screen=$j";
echo "<a href=\"$url\">Prev</a>";
}

// page numbering links now

for ($i = 0; $i < $pages; $i++) {
$url = "$PHP_SELF?action=list&type=$design_id&screen=" . $i;
$j = $i + 1;
echo " | <a href=\"$url\">$j</a> | ";
}

if ($screen < $pages-1) {
$j = $screen + 1;
$url = "$PHP_SELF?action=list&type=$design_id&screen=$j";
echo "<a href=\"$url\">Next</a>";
}


The above code display my pages like:-
1 | 2 | 3 | 4 | next etc,

raf
12-23-2003, 04:10 PM
Change :

for_($i = 0;_$i_<_$pages;_$i++)____{

________$url_=_"$PHP_SELF?action=list&type=$design_id&screen="_._$i;

________$j_=_$i_+_1;

________echo_"_| <a_href=\"$url\">$j</a> |_";

____}


into


for_($i_=_0;_$i_<=_10;_$i++)____{

________$url_=_"$PHP_SELF?action=list&type=$design_id&screen="_._$i;

________$j_=_$i_+_1;

________echo_"_| <a_href=\"$url\">$j</a> |_";

____}
If ($pages > 10) {
$url_=_"$PHP_SELF?action=list&type=$design_id&screen=11";

________echo_"_| <a_href=\"$url\">$j</a> |_";
echo
}


well, it'll need some extra's bcause i can imagen you would like to get the next 10 links (previous - 11-20 - next) so you'll have a few extra conditions like


if ($screen > 10) {
echo 'previous .....
}

and you then can't set the screen to $i but you need to have another incrementing variable inside the for-loop like


$lowest = 10;
for_($i_=_0;_$i_<=_10;_$i++)____{

_______ $url_=_"$PHP_SELF?action=list&type=$design_id&screen="_._$lowest;

________$lowest ++;
$j_=_$i_+_1;
________echo_"_| <a_href=\"$url\">$j</a> |_";

____}


end so on

<edit>ignore the underscores, i don't know how they get added)</edit>

celestine
12-26-2003, 08:46 AM
thank you. raf.. I'll give it a shot later. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum