Use PHP for Article page count - "Pages 1 2 3 4 5"
-
Hi, I need a PHP script for showing "Pages 1 2 3 4" or whatever along the bottom of my articles when they are pulled from the database. So if you click on a headline and it brings up the article then I only show the first 3 paragraphs then i want down the bottom a "Pages 1 2 3 4" so people can click thru to the next page or straight to page 4 etc. The paragraph count is done by exploding the stored text at the "<br /><br />" and then counting the array. $article = explode("<br /><br />", $content); $countparagraphs = sizeof($article); To keep things tidy I want the count to check if there is just one more paragraph for the next page then make the current page show four paragraphs rather than have three paragraphs and have to click one more page that would just be the last page of a four paragraph article. So it must check that if the remainder is only one then just add that to the last page. So if the paragraph count was seven you would not have 3 pages with 3 paragraphs, 3 paragraphs and 1 paragraphs you would have 3 paragraphs and 4 paragraphs (eg two pages) and so on. // Set number of paragraphs per page $num = 3; Also the current page number should not be a link...so if current page is two then Page 2 should be unlinked as it is the current page. Think it is simple but the extra paragraph thing is getting me bad :))
-
Answer:
Hello Srv, Here's the index.php: ----- <? if ( !isSet($currentPage) ) { $currentPage = 1; } $currentPage--; $content = ""; $content .= "This is paragraph 1<br /><br />"; $content .= "This is paragraph 2<br /><br />"; $content .= "This is paragraph 3<br /><br />"; $content .= "This is paragraph 4<br /><br />"; $content .= "This is paragraph 5<br /><br />"; $content .= "This is paragraph 6<br /><br />"; $content .= "This is paragraph 7<br /><br />"; $content .= "This is paragraph 8<br /><br />"; $content .= "This is paragraph 9<br /><br />"; $content .= "This is paragraph 10<br /><br />"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>Pages</title> </head> <body> <div class="content"> <? $pPerPage = 3; $pages = split("<br /><br />", $content); if ( count($pages) > $pPerPage * 2 ) { $pageMax = 0; $pCount = 0; $thisContent = array(); for ($i = 0; $i < count($pages); $i++) { if ( ++$pCount == 1 && $i + $pPerPage < count($pages) ) { $thisContent[++$pageMax] = ""; } else if ($pCount == $pPerPage) { $pCount = 0; } if ($pages[$i] != "") { $thisContent[$pageMax] .= "<p>" . $pages[$i] . "</p>"; } } echo $thisContent[$currentPage + 1]; ?> </div> <p class="footer">Page | <? for ($i = 0; $i < $pageMax; $i++) { if ($i == $currentPage) { ?><span><?=$i + 1?></span> | <? } else { ?><a href="index.php?currentPage=<?=$i + 1?>"><?=$i + 1?></a> | <? } } ?> </p> <? } else { echo $content; } ?> </body> </html> ----- (Some longer lines might break up and need to be repaired in above PHP code.) Hope that helps!
srv-ga at Google Answers Visit the source
Related Q & A:
- Can we install medical softwares that run on Windows mobile 3 or 5 on WM6.1?Best solution by Yahoo! Answers
- What short 4-5 sentence story for 3 year olds can you think of?Best solution by answers.yahoo.com
- Is there any way to connect USB in computers to Playstation 1/2/3's controller?Best solution by Yahoo! Answers
- Should I upgrade to OS 3.1.2?Best solution by answers.yahoo.com
- How do you multiple 1 2/3 by 3 1/8?Best solution by themathpage.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.