Professional Documents
Culture Documents
Utila PHP
Utila PHP
Code: 260,741. lines Jobs: 102. postings How to support the site
Sponsored by:
o o o o o o o o o o o o o o o o o o o o o o o o o o o o
.Net (VB and C#) PHP Javascript Java C/C++ SQL Perl ASP classic VB classic Delphi Cold Fusion
Code/Articles
Newest Browse categories Search ----------------------Upload code / articles Open letter from moderators ----------------------Articles & tutorials Recommended reading
Top Code
Coding contest Coding contest leader board Coding contest 'All-Time Hall of Fame' ----------------------Code of the day newsletter
Community
Ask a pro discussion forum Games Code of the day newsletter
Jobs
Search for a job Post a job
Other
My profile How to link to us Awards/Reviews/Raves! Advertising/Media kit Feedback About the site
Goto
PHP home Site home Other sites
SortPages () function
Print Email
Level: Advanced User Rating: By 1 Users Compatibility:PHP 4.0 Users have accessed this article 3855 times.
This function will output a page indicator with links, see screenshot. This article has accompanying files
Terms of Agreement: By using this article, you agree to the following terms... 1. 2. 3. 4. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws. You may link to this article from another website, but ONLY if it is not wrapped in a frame. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
<?php //Connect to your database $db = mysql_connect ("localhost", "########","########") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("########", $db); //Get the number of posts in your guestbook $total_entries = mysql_num_rows (mysql_query ("SELECT * FROM guestbooks_entries", $db)); /* Call the SortPages () function
Syntax: SortPages ([number of posts], [current page number], [posts per page], [name of the page])
Ex. of a name of the page: Guestbook.php?, index.php?id=20&, ... */ $arPages = SortPages ($total_entries, $page, 20, $PHP_SELF . "?"); /* Call another query. posts. This one will be the one you use to display your
Start is from which row it must show the results, the SortPages () function gives this value to you. Rows is the amount of rows to show the results of = the amount of rows after Start. This should be the same value that you gave for posts per page! */ $res = mysql_query ("SELECT * FROM guestbooks_entries LIMIT " . $arPages ["start"] . ", 20", $db); //If there is only one page, the value returned will be null, nothing. Therefore we must see if the string in not empty. if ($arPages ["link"] != "") print ("<div style="textalign: right">page: " . $arPages ["link"] . "</div>"); //From this point onwards it's your code, this is just an example. if (mysql_num_rows ($res) == 0) { print ("There are no posts in this guestbook.<hr style="height: 1px; color: #ff8818" />"); } else { while ($ligne_entry = mysql_fetch_object ($res)) { // YOUR CODE
} }
function SortPages ($totalentries, $currentpage, $entriespage, $pagenam e) { // If this is true then there is only 1 page, no need to calculate anything then. if ($totalentries <= $entriespage) { $pagesinfo ["link"] = ""; $pagesinfo ["start"] = 0;
return $pagesinfo; } // If there is no page specified, we'll make it page 1. if ($currentpage == "") $currentpage = 1; // Calculate the total number of pages. $pages = round ($totalentries / $entriespage, 0); /* In the above statement, we rounded to no decimals (the page cannot be 5.2).
If the amount of pages with decimals is larger than the one without, that means that we need to add a page.
Ex.: the amount of pages is 5.2, this will become 5. 6, on the 6th page the 0.2 left over will be shown. If we don't do this, the extra 0.2 will be lost. */
We turn it into
if (($total / $entriespage) > $pages) $pages = $pages + 1; // This is to prevent errors, if the current page is larger than the amount of pages, we set the current page to the last page. if ($currentpage > $pages) $currentpage = $pages;
//
This is for the MySQL query, this calculates the starting row. $start = ($currentpage - 1) * $entriespage;
/* Here comes the whole page calculating story. It's actually very simple if you look at it. The fact that it's so long is because there are many special cases that require special attention.
I'm not going to explain it, it's very straight forward. */ if ($pages <= 1) { $tmp_pages = ""; } else { $previous = $currentpage - 1; $next = $currentpage + 1;
if ($pages <= 9) { for ($i = 2; $i <= $pages; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; }
} } else { if (($currentpage == 1) || ($currentpage == $pages) || ($currentp age == 2) || ($currentpage == $pages - 1)) { for ($i = 2; $i <= 3; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } }
for ($i = $pages - 2; $i <= $pages; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } } } else { if (($currentpage == 3) || ($currentpage == 4) || ($currentpage == 5)) { for ($i = 2; $i <= $currentpage + 1; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; }
for ($i = $pages - 2; $i <= $pages; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } } } else { if (($currentpage == $pages - 4) || ($currentpage == $pages 3) || ($currentpage == $pages - 2)) { for ($i = 2; $i <= 3; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } }
for ($i = $currentpage - 1; $i <= $pages; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; }
} } else { for ($i = 2; $i <= 3; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } }
for ($i = $currentpage - 1; $i <= $currentpage + 1; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } }
for ($i = $pages - 2; $i <= $pages; ++$i) { if ($i == $currentpage) { $tmp_pages .= ", [$i]"; } else { $tmp_pages .= ", <a class="pagelink" href="" . $pagename . "page=$i">$i</a>"; } }
} } } }
Is this code not quite what you want? Click here for custom coding.
Download article
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzipto decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.For your own safety, please: 1. 2. Re-scan downloaded files using your personal virus checker before using it. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com
Terms of Agreement: By using this article, you agree to the following terms... 1. 2. 3. 4. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws. You may link to this article from another website, but ONLY if it is not wrapped in a frame. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Your Vote!
What do you think of this article(in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent Good Average Below Average was not quite what you wanted, then click here forcustom coding.
Poor
Rate It!
PHP
Search
Click here to put this ticker on your site! Add to Active Desktop Add this ticker to your desktop!
This Site on CD
Over 7,000 submissions on a super fast CD!
Open letter from moderators About the site Feedback Link to the Site Awards Advertising Privacy
Code/Articles Newest Browse categories Search Upload code / articles Open letter from moderators Articles & tutorials Recommended reading Top Code
Coding contest Coding contest leader board Coding contest 'All-Time Hall of Fame' Code of the day newsletter Community Ask a pro discussion forum Games Code of the day newsletter Jobs Search for a job Post a job Other My profile How to link to us Awards/Reviews/Raves! Advertising/Media kit Feedback About the site Goto PHP home Site home Other sites Copyright 1997-2012 by Exhedra Solutions, Inc. All Rights Reserved. By using this site you agree to its Terms and Conditions. Planet Source Code and the phrase "Dream It. Code It" are trademarks of Exhedra Solutions, Inc.