Jump to content

Ordering records randomly


Meddysong

Recommended Posts

I like how the Providers page loads the records in a different order every time the page refreshes. I think that's such a good idea for a database in which there's no natural hierarchy. But how is it pulled off?

I'm going to hazard a guess that the CategoryTable template in the Listing templates has been amended with a query amendment overwriting the database settings (limited to ascending and descending) and instead instructing the ordering to be random. But I'm not going to be able to do much beyond demonstrating some intuition.

Can anybody shed any light on how this is pulled off and, more importantly, give some example code?

Link to comment
Share on other sites

I got there in the end, thanks to lots of help from @newbie LAC. The key thing is to have 

{{shuffle($rows);}}

in the template at the point of calling the recordRow template.

In my case, the relevant part of categoryTable looks like this:

{{if ! count($rows)}}
	<div class="ipsPad">
		{lang="cms_no_records_to_show" sprintf="\IPS\cms\Databases::load( \IPS\cms\Databases\Dispatcher::i()->databaseId )->recordWord()"}
	</div>
{{else}}
    <ol class='ipsDataList ipsDataList_zebra ipsClear cCmsListing {{foreach $table->classes as $class}}{$class} {{endforeach}}' id='elTable_{$table->uniqueId}' data-role="tableRows">
		{{shuffle($rows);}}
    	{template="$table->rowsTemplate[1]" params="$table, $headers, $rows" object="$table->rowsTemplate[0]"}
	</ol>
{{endif}}

If you need custom work, I heartily recommend Newbie. He's always responded quickly and politely, and I've yet to find a job which he couldn't do :) 

Link to comment
Share on other sites

There are several ways to achieve this, e.g. with adding a counter and comparing the value of the counter to achieve all sorts of things in regards to skipping elements. 

Probably the most simple solution in a block would be …

{{array_shift($records);}}

That removes the first entry and the output would start with the (previously) second element. 

Link to comment
Share on other sites

4 minutes ago, opentype said:

There are several ways to achieve this, e.g. with adding a counter and comparing the value of the counter to achieve all sorts of things in regards to skipping elements. 

Probably the most simple solution in a block would be …


{{array_shift($records);}}

That removes the first entry and the output would start with the (previously) second element. 

Thank you!

I notice this is working when I place it at the top of the block template. When I tell the filter to show only 1 record, it displays nothing. Am I placing the code in the incorrect place?

 

screen4.thumb.jpg.7cdeb40d4721654463a696c5514a83ea.jpgscreen3.thumb.jpg.db29587b3eaf7d462c02e3d940660e44.jpg

screen2.thumb.jpg.a44bcc68ea56c324027880f2392934f5.jpg

Link to comment
Share on other sites

4 minutes ago, opentype said:

Works as expected. If you want to show the second element only, you set the filter to show 2 elements and then the first one gets removed/skipped by array_shift.

Hi open,

Thanks for helping me with this and having patience with me. I'm not a professional coder so the array part of it, I'm not quite sure. I've figured out how to use {{if}} {{endif}} statements, but I haven't used the array function before.

So I don't know how to tell the array to shift +1 , +2, +3 etc

Thanks again for helping me understand this.

Link to comment
Share on other sites

3 minutes ago, opentype said:

It skips one every time you call it.

{{array_shift($records);}}
{{array_shift($records);}} 

That skips two. 

Ah ok! So, I was playing around with my page and noticed that I have to increase the number of records to show.

So, for the first block, I have to show 2 records - skip one

Second block show 3 records - skip two

Third block show 4 records - skip three

Ok, I think this is going to work the way I want it to. Why isn't this a filter option for the block??

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...