Jump to content

Question about $input->pageNum


Zahari M.
 Share

Recommended Posts

Hi guys!

Hope someone can advise me with this!

Very very new teritory for me this one... :)

Ok...

So I have a template that is used as a parent to display children...

As there will be many children, results will be limited to 10 per page i.e. $posts = $page->children("limit=10,sort=-sort");

We will want to see the next set of 10 results of course so we call on our pager to advance to the next page...

So $pagination = $posts->renderPager() sorts this out for us.

So far so good.

Ok.. so in my sites <head> document I want it to look like this....

<link rel="canonical" href="http://site.com/parent/page3/" />
<link rel="prev" href="http://site.com/parent/page2/" />
<link rel="next" href="http://site.com/parent/page4/" />

So I have found two helpful items in our beloved API:

$page->httpUrl which will gives us the href="http://site.com/parent/ part needed.

$input->pageNum which will tell us if we are on page2 or page3 etc.

Lets say that we have a situation where there are 45 child page results.

Well, we ourselves know that we will end up having 5 pages of results.

So there will be

parent

parent/page2

parent/page3

parent/page4

parent/page5

My question is, when I am on parent/page5, which is the last page of results, how can I programatically determine that there is not a parent/page6? If I can determine this, then on parent/page5 i would be able to output this in my <head>. Note the absence of rel = 'next'

<link rel="canonical" href="http://site.com/parent/page5/" />
<link rel="prev" href="http://site.com/parent/page4/" />

So if any of you have any ideas on how I would be able to test that I have come to the last page, I would be most grateful!

Unfortunately, I have never ever worked with counters and modulus and so am somewhat at a loss as what is the best way to work this one out! Any help ideas etc most welcome!



Link to comment
Share on other sites

Hi Zahari,

$yourRegularLimit = 10;
$cur = $input->pageNum;
$max = intval($pages->find("$yourSelector, limit=2")->getTotal() / $yourRegularLimit);  // limit = 2 so it is fast and uses less memory!
$next = $cur<$max ? $cur + 1 : false;
$prev = $cur>1 ? $cur - 1 : false;

echo '<link rel="canonical" href="http://site.com/parent/page' . $cur . '/" />';
if(false!==$prev) echo '<link rel="prev" href="http://site.com/parent/page' . $prev . '/" />';
if(false!==$next) echo '<link rel="next" href="http://site.com/parent/page' . $next . '/" />';

Not tested, just to ge you started, ;)

  • Like 5
Link to comment
Share on other sites

Hi horst!

Many thanks for this horst!

I was looking at php's mathematical functions and the modulus operator and did come across getTotal() in the API. But couldn't workout how to arrange them to do the necessary last page check I needed.

I see above that you use intval. I have never ever come across that before horst! It kinda seems to be exactly what I was looking to try and do with all the above mentioned items. Yay! :)

I still don't fully understand how your code works just yet. But that's good! I'm going to go and have some lunch, find a good cafe somewhere where they serve both some good coffee and some good Shiraz.

I shall start with the coffee, a good mechanical pencil, some paper and start to figure this one out step by step and expand my brains internal processing functions. Hopefully I can take my brain from a 386 to a 486 >:D

Then reward my self with some Shiraz!

Or ask you for a little more assistance...

Cheers horst!

  • Like 2
Link to comment
Share on other sites

Ok horst and teppo...

My head was spinning at first as I couldn't work out how to make my selector = child pages in the line below:

$max = intval($pages->find("$yourSelector, limit=2")->getTotal()

So what I did was:

$currentPage = $page->id;    then

$max = intval($pages->find("parent=$currentPage, limit=2")->getTotal() / $maxPerPage);

That got me my child pages and thus my $max value.

The next problem I had was the next page link wasn't displaying a page number. This turned out to be here after some headscratching....

$next = $cur<$max ? $cur + 1 : false;

When I changed the less than sign < to less than or equals <=, then that got it to work!

Next thing was I didnt want to see "page1" on any of the urls.

And so I set up a switch arrangment to provide me with the exact markup / structure that I wanted....a major reason why I love processWire !!! 

I have some ideas you see :P ... but not the skills like many of you great helpful guys here.....

Anyway.... this is what I have come up with so far.... no coffee yet... but will have that right after posting this.... and drinks later tonight!

// SEO - CANONICAL & REL SECTION:
$maxPerPage = 10;
$currentPage = $page->id;
$curPageNum = $input->pageNum;
$max = intval($pages->find("parent=$currentPage, limit=2")->getTotal() / $maxPerPage);  // limit = 2 so it is fast and uses less memory!

$next = $curPageNum <= $max ? $curPageNum + 1 : false;
$prev = $curPageNum > 1 ? $curPageNum - 1 : false;

switch ($input->pageNum){

case '1':
echo "<link rel='canonical' href='$page->httpUrl'>\n";
if(false!==$next) echo "<link rel='next' href='$page->httpUrl" . 'page' . "$next" . "/'>\n";
break;

case '2':
echo "<link rel='canonical' href='$page->httpUrl" . 'page' . "$curPageNum/'>\n";
if(false!==$prev) echo "<link rel='prev' href='$page->httpUrl'>\n";
if(false!==$next) echo "<link rel='next' href='$page->httpUrl" . 'page' . "$next" . "/'>\n";
break;

default:
echo "<link rel='canonical' href='$page->httpUrl" . 'page' . "$curPageNum/'>\n";
if(false!==$prev) echo "<link rel='prev' href='$page->httpUrl" . 'page' . "$prev" . "/'>\n";
if(false!==$next) echo "<link rel='next' href='$page->httpUrl" . 'page' . "$next" . "/'>\n";
break;
}

So horst, thanks so much for helping out!

Teppo thanks for coming in.

If you guys were here I would buy you a nice cup of coffee!

Cheers guys!

Thanks a bunch!

  • Like 3
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...