Problems, need help? Have a tip or advice? Post it here.
15 posts Page 2 of 2

I wonder if that code is in a embedded snippet.. Couch does cache snippets separately in db.

Good point. Not an embedded snippet.

To simplify the parameters of the issue, I did a test removing the complicating factor of using a query string in the URL and evaluating it with PHP to "turn on" randomization. Instead, I went ahead and just hard-coded orderby='random' directly into the <cms:pages> tag, then tested the page again. It's still the same issue: the blog post order randomizes only the first time, then stays the same until some kind of session expiry type of time period passes.

Code: Select all
<cms:pages orderby='random' limit='10' return_sql='1' />

I placed SQL into my database tool and I was receiveing random sets every request. Tested in MySQL8.0+PHP7.2.
Is your installation online to see it happening firsthand?
I have my setup on a desktop dev environment, but not online. ... That bit of parameter is pretty cool. It's showing me that the RAND number does not change when I reload the page (as already described). Here, for example, is the return SQL that I saw when I hit my "Random blog post" link:

SELECT p.id, p.template_id FROM couch_pages p WHERE p.template_id='10' AND p.publish_date < '2020-01-30 12:00:42' AND NOT p.publish_date = '0000-00-00 00:00:00' AND p.parent_id=0 ORDER BY RAND(1659813988) desc LIMIT 0, 20

This particular RAND() number stays exactly the same every pageload.

Of course, if I open another browser and load the link, I get a different RAND() number, but then that one, in that browser, stays the same over and over again too ... Until 10 minutes or whatever goes by.

Code: Select all

<cms:pages orderby='random' limit='10' return_sql='1' />


I placed SQL into my database tool and I was receiveing random sets every request. Tested in MySQL8.0+PHP7.2.
Is your installation online to see it happening firsthand?

I cracked the problem. Here is my generated SQL -

SELECT p.id, p.template_id FROM couch_pages p WHERE p.template_id='2' AND p.publish_date < '2020-01-30 21:34:29' AND NOT p.publish_date = '0000-00-00 00:00:00' AND p.parent_id=0 ORDER BY RAND() desc LIMIT 0, 10

Note, there is no seed number inside RAND().

According to core Couch code from tags.php, seed is added to split randomized set into several pages, keeping the order of pages. Therefore, you should remove parameter paginate='1' from your <cms:pages> tag to allow Couch randomize set on each page load. Pagination does depend on seed, though. Take a set of 100 pages, paginated by 5 - there would be 20 paginated pages and going to another page by change of url ?pg=2 should not be randomized anew, but instead should display pages from the first query of 100 pages. Seeds are kept in user session - It explains why it messes things in your case.

tags.php -
Code: Select all
elseif( $orderby == 'random' ){
   if( $paginate ){
      if(!session_id()) @session_start();
      if(empty($_SESSION['k_seed'])) $_SESSION['k_seed'] = rand();
      $orderby = 'RAND(' .$_SESSION['k_seed']. ')';
   }
   else{
      $orderby = 'RAND()';
   }
   $PAGE->no_cache=1;
}
Trendoman:

I don't know what to say. You nailed it. I turned off pagination and randomization now works perfectly with every pageload.

Thanks to you and KK for your help.
15 posts Page 2 of 2

Who is online

In total there are 8 users online :: 0 registered, 0 hidden and 8 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 8 guests