Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
Meddysong Posted August 5, 2017 Posted August 5, 2017 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?
Meddysong Posted August 11, 2017 Author Posted August 11, 2017 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
joshuaj Posted August 11, 2017 Posted August 11, 2017 Does he know how to make a record offset by chance? I've been trying to find a solution to make the block skip records and display like the 3rd or 4th record.
opentype Posted August 11, 2017 Posted August 11, 2017 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.
joshuaj Posted August 11, 2017 Posted August 11, 2017 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?
opentype Posted August 11, 2017 Posted August 11, 2017 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.
joshuaj Posted August 11, 2017 Posted August 11, 2017 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.
opentype Posted August 11, 2017 Posted August 11, 2017 It skips one every time you call it. {{array_shift($records);}} {{array_shift($records);}} That skips two.
joshuaj Posted August 11, 2017 Posted August 11, 2017 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??
joshuaj Posted August 11, 2017 Posted August 11, 2017 @opentype, I can't thank you enough on helping me with this. It is working now the way I wanted it to. You sir, are the best. Seriously, thank you very much.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.