Google Answers Logo
View Question
 
Q: Use PHP for Article page count - "Pages 1 2 3 4 5" ( Answered,   0 Comments )
Question  
Subject: Use PHP for Article page count - "Pages 1 2 3 4 5"
Category: Computers > Programming
Asked by: srv-ga
List Price: $10.00
Posted: 13 Jul 2002 06:04 PDT
Expires: 12 Aug 2002 06:04 PDT
Question ID: 39220
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  
Subject: Re: Use PHP for Article page count - "Pages 1 2 3 4 5"
Answered By: j_philipp-ga on 13 Jul 2002 08:41 PDT
 
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!

Clarification of Answer by j_philipp-ga on 13 Jul 2002 08:53 PDT
Hell Srv,

the above actually tests if the new page would contain at least the
number of paragraphs per page required.
To alter it to show a new page only if it contains more than a single
paragraph, you can replace these lines:

    if ( count($pages) > $pPerPage * 2 )
        ...
        if ( ++$pCount == 1 && $i + $pPerPage < count($pages) )

by those:

    if ( count($pages) > $pPerPage + 2 )
        ...
        if ( ++$pCount == 1 && $i + 2 < count($pages) )


Again, I hope that helps.

Request for Answer Clarification by srv-ga on 13 Jul 2002 20:37 PDT
Hi Mate
Thanks for the answer and almost there..probably a bit more to explain
:))
Okay all works fine..had to fiddle a bit to integrate it all but it is
there and working..problem is with page numbers and links. I tried to
do it myself but couldn;t figure out what the <?=$i + 1?> meant...have
never seen = in front of a variable?

The URL for our articles page is
URL/view_articles.php/56/1/57/0/28/28/1/ - The last number at the end
is the current page number eg 1. The URL is called in the page as <a
href=\"$searchlink\"> which is called from an include just before the
line - include("./includes/searchlink.php"); So essentially each page
number needs to read as -

Pages |<a href="URL/view_articles.php/56/1/57/0/28/28/1/">  1 </a> |
<a href="URL/view_articles.php/56/1/57/0/28/28/2/"> 2 </a> | <a
href="URL/view_articles.php/56/1/57/0/28/28/3/">  3 </a>

But still not a link if on current page...

The $searchlink include contains
<?
if (!isset($week)) {
$week = date ("W",time());
}
if (!isset($page)) {
$page = "1";
}

$searchterm = urlencode($term);
$endbit = "$ArtTourID/$ArtCat/$ArtID/$ArtBruce/$week/$ArtOld/$page/$searchterm";
$searchlink="/view_articles.php/".$endbit;
?>

So the page number is included in the include as $page.

Also can I have the code as though it is within <??> and so the HTML
is echoed rather than outside the PHP...too many opening and closing
of <??> and the spot where I am putting the code is within PHP. Can I
also not have the <P> would rather just re-add the <br /><br /> that
were taken out in the split...Thanks mate.

If I need to pay more for the answer just let me know...hoepfully I
have explained it enough :))

regards
SRV

Clarification of Answer by j_philipp-ga on 14 Jul 2002 05:15 PDT
Hello again Srv,

The code below implements the following requested changes:
- easy reconfiguration of link setup (see topmost 2 variables)*
- "<br /><br />" instead of "<p></p>"
- "echo" instead of "<?= ?>" (the "=" sign is a shortcut for "echo")
- renaming of "$currentPage" to "$page" to better integrate with your
existing code
- no HTML start and end declarations

* I don't know exactly how the link has to be for you, but it seems
this would be it:
$urlBeforePageNumber =
"$ArtTourID/$ArtCat/$ArtID/$ArtBruce/$week/$ArtOld/";
$urlAfterPageNumber = "/$searchterm";
(As you can see, the page variable will not be inserted here as it's
handled dynamically deeper inside the code.)

----
$urlBeforePageNumber = "index.php?page=";
$urlAfterPageNumber = "";

if ( !isSet($page) )
{
    $page = 1;
}

$page--;
$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] .= $pages[$i] . "<br /><br />";
        }
    }

    echo $thisContent[$page + 1];

    echo "<p class=\"footer\">Page | ";
    for ($i = 0; $i < $pageMax; $i++)
    {
        $pageNumber = ($i + 1);
        if ($i == $page)
        {
            echo "<span>" . $pageNumber . "</span> | ";
        }
        else
        {
            $url = $urlBeforePageNumber . $pageNumber .
$urlAfterPageNumber;
            echo "<a href=\"" . $url . "\">" . ($i + 1) . "</a> | ";
        }
    }
    echo "</p>";
} 
else
{
    echo $content;
}
----

Hope this helps.

Clarification of Answer by j_philipp-ga on 14 Jul 2002 05:20 PDT
And, not to forget, the following line:

    if ( ++$pCount == 1 && $i + $pPerPage < count($pages) )

Should read:

    if ( ++$pCount == 1 && $i + 2 < count($pages) )

(That is, if you want only a single paragraph to not be on its own
page -- see my first clarification on how to switch back and forth
between the two.)

Request for Answer Clarification by srv-ga on 14 Jul 2002 05:58 PDT
Hi Mate,
Sorry but the code you sent me just won't work as needed. The url is
currently stored in one place in an include (searchlink.php) so that I
only have to make any changes in one place hence not wanting to break
up the URL as you have done. It would certainly work but not on our
site as that article URL may change with new variable being added on
to the end or anywhere so the page number must be pulled out and
called in each loop.

So essentially each page
number needs to read as -
 
Pages |<a href="URL/view_articles.php/56/1/57/0/28/28/1/">  1 </a> |
<a href="URL/view_articles.php/56/1/57/0/28/28/2/"> 2 </a> | <a
href="URL/view_articles.php/56/1/57/0/28/28/3/">  3 </a>

Thanks and hope to hear from you soon. The thing to remember is this
will be part of a large dynamic site so things have to be done in one
place if possible hence pulling the url form the include.

Cya
SRV

Clarification of Answer by j_philipp-ga on 14 Jul 2002 07:44 PDT
Hello Srv,

Since you wanted me to deliver an algorithm to automatically create
URLs, how the URL is built up must somehow be known by the code I
deliver (or else we would have a theoretically unsolvable situation).
So, if you want to keep it in one place, which is always a good
approach, I suggest moving those two variables I introduced
($urlBeforePageNumber and $urlAfterPageNumber) out of the code
provided by me, and into the included "searchlink.php" file.

Hope this helps.

Request for Answer Clarification by srv-ga on 14 Jul 2002 15:44 PDT
Hi Mate,
thanks for the reply...
I don't know why the URL has to be split as if the page number is
coming from in the loop and is called $Pagenum perhaps...then the
include grabs that page number then the URL is called and it inserts
$pagenum in the right place in the URL. There is no need to split the
URL as you have done...It just needs to loop around "include" the
searchlink.php file and the URL is always <a href=$searchlink>...

$searchlink always equals

the only thing that changes is the page number as it loops around so
you end up with -

Pages |<a href="URL/view_articles.php/56/1/57/0/28/28/1/">  1 </a> |
<a href="URL/view_articles.php/56/1/57/0/28/28/2/"> 2 </a> | <a
href="URL/view_articles.php/56/1/57/0/28/28/3/">  3 </a>

Thansk mate

Clarification of Answer by j_philipp-ga on 14 Jul 2002 16:47 PDT
Hello Srv,

the basic theoretical problem, as I tried to point out:
- you say the "article URL may change with new variable being added on
to the end or anywhere"
- you say you need "URL/view_articles.php/56/1/57/0/28/28/2/", etc.

The first is dynamic, the second is static. The second is no problem,
but you seem to really want to keep the first option, so that URLs may
be changed over time. And for that request, as I mentioned, the
algorithm needs a way to know how exactly the URLs are built to
generate more of them dynamically. But how could I know which part in
"URL/view_articles.php/56/1/57/0/28/28/1/" is the page number? I could
deduce it by comparing two URLs, but in the actual code you only pass
a single URL. In other words: there is no way for the script to know
which one is the page-number that needs to be adapted, unless you
somehow pass a variable to communicate that. That's why I introduced
the $urlBeforePageNumber and $urlAfterPageNumber variables. You could
also pass it in a single string if you prefer, e.g.
"URL/view_articles.php/56/1/57/0/28/28/[page]/", and then have the
[page] be replaced in the loop.

Hope this helps.

Request for Answer Clarification by srv-ga on 14 Jul 2002 19:34 PDT
Hi mate,
Okay....think I need to explain it perhaps clearer..always hard over
the net.
The variable will always be $page in the URL, it doesn;t matter where
it is in the URL as it is pulled in the loop - $page = 2 then when the
URL is printed out it replaces the $page with a 2 then the loop goes
around again and this time $page = 3 it then puts a three in place of
$page within the URL -
URL/view_articles.php/56/1/57/0/28/28/$page/.....you almost have the
whole thing right....

When the page goes to screen an include is pulled at the top of the
script - fixurl.php

<?php
if (ereg("^\/view\_(.*.)php\/",$PHP_SELF)) {
$urls = "/view_articles.php/";
} elseif (ereg("^\/tour\_(.*.)php\/",$PHP_SELF)) {
$urls = "/tour_articles.php/";
} elseif (ereg("^\/print\_(.*.)php\/",$PHP_SELF)) {
$urls = "/print_articles.php/";
} elseif (ereg("^\/rate\_(.*.)php\/",$PHP_SELF)) {
$urls = "/rate_articles.php/";
}
$res = explode($urls, $_SERVER["REQUEST_URI"]);
If (count($res) > 1) { 
    $vars = explode("/", $res[1]); 
    for ($i=0; $i < count($vars); $i++) { 
        If (substr($vars[$i],strlen($vars[$i])-2,2) == "[]") { 
            $temp = substr($vars[$i],0,strlen($vars [$i])-2); 
            ${$temp}[] = $vars[$i+1]; 
        } else { 
            If (isset($vars[$i+1])) ${$vars[$i]} = 
$vars[$i+1]; 
        } 
    $i++; 
} 
}

$ArtTourID = $vars[0];//ID for a specific tournament eg 17 = BellSouth
Classic 2002
$ArtCat = $vars[1];//ID for Tours eg 1 = US PGA Tour
$ArtID = $vars[2];//Unique ID given to every article
$ArtBruce = $vars[3];//Who wrote the article
$week = $vars[4];//Code used for the weekly tournaments list
$ArtOld = $vars[5];//Code used for getting last years article ID
$page = $vars[6];//Page numbers for page breaks on articles
$term = $vars[7];//Search term from any search page

?>

So as you can see all that has to happen is $page be pulled in the
loop and it should all be fine

Hear from you soon.
SRV

Clarification of Answer by j_philipp-ga on 15 Jul 2002 02:07 PDT
Hello Srv,

If the URL is built up with this static mechanism (again, it is not
possible to allow for a dynamic changing of say, order of variables in
the URL, unless you change them at this second place too), you can
replace the else-clause at the bottom of the script with:

    else 
    { 
        $url = "$ArtTourID/$ArtCat/$ArtID/$ArtBruce/$week/$ArtOld/$pageNumber/$searchterm";
        echo "<a href=\"" . $url . "\">" . $pageNumber . "</a> | "; 
    } 


Hope this works for you.
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy