PRIMARY ORDER RAND() [this is not trivial]

  • Anonymous
  • Topic Author
  • Visitor
  • Visitor
12 years 6 months ago #19823 by Anonymous
(material in the catalog in a random order)

Hello.
Sorry for the bad english.

For a project I need to implement the output of articles in the category in a random order. But this sort of parameters PRIMARY ORDER I have not found.

I tried to do as well as in the module mod_flexicontent
Code:
$orderby = 'ORDER BY RAND()';

The file ..components/com_flexicontent/models/category.php replaced in the same sort parameter to
Code:
$filter_order = 'RAND()';

Random sorting of earned but for the design of 'ORDER BY RAND()' to the server went to a very large load, there are other decisions to sort in random order?

Please Log in or Create an account to join the conversation.

  • Anonymous
  • Topic Author
  • Visitor
  • Visitor
12 years 6 months ago #19825 by Anonymous
Replied by Anonymous on topic PRIMARY ORDER RAND() [this is not trivial]
Even tried to do a random sorting in the template.
For example, in a standard template blog
line
Code:
$items = $this->items; $count = count($items); if ($count) :
replaced by
Code:
$items = $this->items; shuffle ($items); $count = count($items); if ($count) :

But there is also a problem, the random sorting works only when the cache is turned off.

Please Log in or Create an account to join the conversation.

More
12 years 6 months ago #19829 by ggppdk
I was going to suggest to do it by PHP in template, which you have done.

If you enable caching then this is the expected behaviour, you can try disable caching in joomla configuration and in FLEXIcontent Global paramaters

Regards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

  • Anonymous
  • Topic Author
  • Visitor
  • Visitor
12 years 6 months ago #19845 by Anonymous
Replied by Anonymous on topic PRIMARY ORDER RAND() [this is not trivial]
will the random sorting be implemented in categories?

Please Log in or Create an account to join the conversation.

More
12 years 6 months ago #19850 by ggppdk
Well,

1. The cost of sorting n rows, using ORDER BY RAND() is c1*n*logn where c1 some constant value

2. The cost of using PHP shuffle() is c2*n where c2 some constant value.

In computer science we would say that:
-- case 1 [ORDER BY RAND() ] is O(nlogn)
-- and case 2 [PHP shuffle ] is O(n)

So when n becomes larger (when having thousands of items) it is better to use PHP shuffle().

Yes, random order of items makes sense but we have a problem!!!

If we use pagination then on each page we will get different items every time we visit page k e.g. oage 2.
Moreover some of the items will never appear after passing through all pages.

More over by your comment i can say also that the c1 constant maybe big and also you must understand that the way you did it with shuffle YOU ONLY randomize the current page (e.g. you have 6 pages)
For these 2 reason PHP shuffle() solution is much faster.

So implementing random order and combining it with pagination is not a trivial problem.

Nevertheless it can be done, but we must:
-- SORT ONCE all items of all pages
-- THEN STORE this sorting somewhere , in DB or SESSION variable,
-- and then for every page retrieve this sorting and use it

About storing in DB is not so convenient since we have many users (& guests) and many categories, it is better to store it in a SESSION array that it will be indexed by category id.

Then the sorting will occur inside the category database model file and not in the template !!!
There when random order is detected, we will ignore the "items per page" setting and query all items of category and then discard items not in current page (query more rows than a part of them is not a serious performance hit, you know that retrieving rows from a database table without setting limitations , can be faster than retrieving just a few by setting limitations.)

Anyway you see that the problem is not trivial, and requires planning to implement.

ANOTHER solution is to randomize order per page only using PHP shuffle(). Which maybe somewhat satisfactory, you did exactly this.

Regards


-- Flexicontent is Free but involves a big effort on our part.
Like the our support? (for a bug-free FC, despite having a long list of functions) Like the features? Like the ongoing development and future commitment to FLEXIcontent?
-- Add your voice to the FLEXIcontent JED listing with a 5-star...

Please Log in or Create an account to join the conversation.

Moderators: vistamediajoomlacornerggppdk
Time to create page: 0.268 seconds
Save
Cookies user preferences
We use cookies to ensure you to get the best experience on our website. If you decline the use of cookies, this website may not function as expected.
Accept all
Decline all
Essential
These cookies are needed to make the website work correctly. You can not disable them.
Display
Accept
Analytics
Tools used to analyze the data to measure the effectiveness of a website and to understand how it works.
Google Analytics
Accept
Decline