pwired Posted October 5, 2018 Share Posted October 5, 2018 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 ? Link to comment Share on other sites More sharing options...
BitPoet Posted October 5, 2018 Share Posted October 5, 2018 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). 2 Link to comment Share on other sites More sharing options...
kongondo Posted October 5, 2018 Share Posted October 5, 2018 (edited) ...Or if you use _init.php, define $homepage there and it will be available in all your template files :-). Edited October 5, 2018 by kongondo typo: should be _init.php (NOT init.php) 3 Link to comment Share on other sites More sharing options...
pwired Posted October 5, 2018 Author Share Posted October 5, 2018 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. Link to comment Share on other sites More sharing options...
pwired Posted October 5, 2018 Author Share Posted October 5, 2018 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 Link to comment Share on other sites More sharing options...
Pixrael Posted October 5, 2018 Share Posted October 5, 2018 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? Link to comment Share on other sites More sharing options...
pwired Posted October 5, 2018 Author Share Posted October 5, 2018 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. Link to comment Share on other sites More sharing options...
Pixrael Posted October 5, 2018 Share Posted October 5, 2018 Forget it, it was just an idea. I think it doesn't work. That is why ask at the end of the paragraph Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now