Jump to content

Recommended Posts

Posted

Hi,

I have a simple counter on my home page template file, home.php,
that simply counts how many times the home web page is loaded.

$page->homepagehits += 1;
$page->of(false);
$page->save('homepagehits');
$page->of(true);

If I want to echo the value of this counter not only on the home page but on other pages as well,
I know that I can simply do it like this:

On the template file of the other page I put this:

$homepage = $pages->get('/'); 
$counter = $homepage->homepagehits;

and then on this other page I simpy do
<?php echo $counter ?>

Besides doing it this way, my question is, is there another way to make the value of $counter
globally available on all other pages ?

Maybe with an $Data array or something ?

 

Posted
2 hours ago, pwired said:

Besides doing it this way, my question is, is there another way to make the value of $counter
globally available on all other pages ?

Maybe with an $Data array or something ?

Not really. Since you have to store the value, you also need to load the page it is attached whenever you want to read it. PHP provides no built-in persistent storage that reaches across sessions and is shared between individual threads/processes. If you want to avoid the overhead of loading $homepage and the counter for every page view, you could however use a memory cache like memcached or APC, update that when the counter is incremented and read from it when viewing other pages (or fall back to $homepage if its not yet set, e.g. after a server restart).

  • Like 2
Posted (edited)

...Or if you use _init.php, define $homepage there and it will be available in all your template files :-).

Edited by kongondo
typo: should be _init.php (NOT init.php)
  • Like 3
Posted

Thanks for your tips. I have $homepage already defined in the _init.php. Never thought it would work to put the value of $counter there too. I will try and see if it works.

Posted

Just tried it, and yes I can confirm, it is working.

Like Kongondo suggested, I have now put this line

$counter = $homepage->homepagehits;

on both the _init.php prepend file and on the home.php template file

Now <php echo $counter ?> shows the correct value on all the other pages as well.

I can use this way of storing values for other cases as well.
Thanks

Posted

maybe is more efficient to use: $config-> homepagehits .. increment it by 1 at home.php template file and later echo it at any template file.. you never need to query any additional page (homepage) to get that counter because $config is present always by default at your hand.. hummm.. is this correct?

Posted

Hi Pixrael,

Quote

you never need to query any additional page (homepage) to get that counter

Thanks for stepping in on this. We´ll, with this line  $counter = $homepage->homepagehits;  on both the _init.php and home.php I now already have the value of $counter globally available. No need to query for it anymore. Great way for storing values also for other cases and make them globally available. Not sure how $config-> homepagehits could be more efficient.

 

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
  • Recently Browsing   0 members

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