Jump to content

Server side session count and AJAX


a-ok
 Share

Recommended Posts

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

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

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.

 

all-services.gif.23dc63ebd9e5f797d0f0c94a694e705b.gif

 

Edited by flydev
gif
  • Like 3
Link to comment
Share on other sites

@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

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

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...