a-ok Posted February 23, 2017 Share Posted February 23, 2017 I have a simple loop that is returning a different layout if the $count is odd or even (image on left, text on right if odd and text on left, image on right if even). I've then set this up with Waypoints Infinite Scroll which is essentially an AJAX loading of content from the next page and therefore, as the $count is server side but the AJAX is client side, it breaks the count and therefore every item that is loaded in the 'else' even layout. Is there a way to do this to it keeps the $count and would return correctly? I thought about using $session and storing the variable but wasn't sure I had this set up correctly. Any thoughts on the below? <?php $limit = 1; $news = $pages->find("parent=/news/, sort=-news_detail_date, limit=$limit"); $total = ceil($news->getTotal() / $limit); ?> <?php $session->set('count', 0); ?> <div class="news--layout"> <?php foreach ($news as $article) : ?> <?php include('./inc/news-item.inc'); ?> <?php $session->count++; endforeach; ?> </div> And in my 'news-item.inc' I have something like... <?php if ($session->count % 2 != 0) : // Odd ?> <?php else : // Even ?> <?php endif; ?> Thanks. Link to comment Share on other sites More sharing options...
qtguru Posted February 23, 2017 Share Posted February 23, 2017 Is it possible to use TracyDebugger to inspect what the count is and if indeed it is an integer and not string Link to comment Share on other sites More sharing options...
a-ok Posted February 23, 2017 Author Share Posted February 23, 2017 3 minutes ago, Sephiroth said: Is it possible to use TracyDebugger to inspect what the count is and if indeed it is an integer and not string Not sure! I echo out the count per item and it's always 0. Link to comment Share on other sites More sharing options...
flydev Posted February 23, 2017 Share Posted February 23, 2017 2 hours ago, oma said: it's always 0. because $limit = 1 in your selector, no ? As you get only one news, $count is not incremented in your foreach loop.. Link to comment Share on other sites More sharing options...
Wanze Posted February 23, 2017 Share Posted February 23, 2017 $session->count++; You cannot increment the count variable like this, as it is accessed via PHP's magic setter. Try this: $count = $session->count + 1; $session->count = $count; Link to comment Share on other sites More sharing options...
a-ok Posted February 23, 2017 Author Share Posted February 23, 2017 1 hour ago, Wanze said: $session->count++; You cannot increment the count variable like this, as it is accessed via PHP's magic setter. Try this: $count = $session->count + 1; $session->count = $count; Thanks @Wanze! Referencing what @flydev said; do you think this would still work? I'm confused because if the limit was 8, for example, when it got to '8' items would it not just reset to 0? Link to comment Share on other sites More sharing options...
flydev Posted February 24, 2017 Share Posted February 24, 2017 (edited) Ok, I think I got what you mean. Yes it will be reset to 0. Just forget what I said before. Instead of having $session->set('count', 0) in your template, set it in _init.php : if(!$config->ajax) $session->set('count', 0); So, if you refresh the page, the $session->count will be reset to 0, if the page is called by AJAX then $session->count is incremented. Edited February 24, 2017 by flydev gif 3 Link to comment Share on other sites More sharing options...
a-ok Posted February 24, 2017 Author Share Posted February 24, 2017 @flydev Thanks for your help. I'm wondering... if I set a session... I should be able to see it being set in Chrome DevTools > Application > Cookies, right? But I'm not so unsure if I'm even setting it correctly. Does session_start(); need to be initialised, or is it set by PW already? Link to comment Share on other sites More sharing options...
flydev Posted February 24, 2017 Share Posted February 24, 2017 3 minutes ago, oma said: Does session_start(); need to be initialised, or is it set by PW already? The session is started automatically and ready-to-use from your templates. Link to comment Share on other sites More sharing options...
a-ok Posted February 24, 2017 Author Share Posted February 24, 2017 5 minutes ago, flydev said: The session is started automatically and ready-to-use from your templates. Thanks. I'm unsure where I am going wrong then... <?php if (!$config->ajax) $session->set('count', 0); ?> <?php foreach ($news as $article) : ?> <?php include('./inc/news-item.inc'); ?> <?php endforeach; ?> In news-item.inc I have $session->count + 1; Link to comment Share on other sites More sharing options...
Wanze Posted February 24, 2017 Share Posted February 24, 2017 $session->count = $session->count + 1 2 Link to comment Share on other sites More sharing options...
a-ok Posted February 24, 2017 Author Share Posted February 24, 2017 1 minute ago, Wanze said: $session->count = $session->count + 1 !!! Yes. Thanks @Wanze and @flydev 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