ALLOWING ADMINS TO SET SORT ORDERBY VALUES BASED ON A LIST FIELD SELECTION - Sep 5th, 2022
|
I needed to let an site admin decide whether a viewer would show the data on a list page with the newest record in the table 'studio_art_images' first, or show the oldest record first, show the records in a random order, or in the order they appeared in the record list.
With a bit of help from Interactive Tools Guru Daryl Maximo, here's what we came up with.
1) set up a single value list field in a single record editor (in this example it's 'sort_order' and the section is named 'common_information')
2) enter the possible list values as:
Oldest First Newest First Show in Random Order None
At the top of the viewer use the code:
<?php list($common_informationRecords, $common_informationMetaData) = getRecords(array( 'tableName' => 'common_information', //'debugSql' => true, 'limit' => '1', )); $common_informationRecord = @$common_informationRecords[0]; // get first record ?> <?php $orderedBy = ""; ?> <?php if (strpos($common_informationRecord['sort_order'], 'Oldest' )!== FALSE) { $orderedBy = 'createdDate ASC'; }; if (strpos($common_informationRecord['sort_order'], 'Newest' )!== FALSE) { $orderedBy = 'createdDate DESC'; }; if (strpos($common_informationRecord['sort_order'], 'Rand' )!== FALSE) { $orderedBy = 'RAND()'; }; if (strpos($common_informationRecord['sort_order'], 'None' )!== FALSE) { $orderedBy = ''; };
?> <?php list($studio_art_imagesRecords, $studio_art_imagesMetaData) = getRecords(array( 'tableName' => 'studio_art_images', 'orderBy' => $orderedBy, )); ?>
Then display the record data in your viewer using standard foreach loop code.
According to Daryl,
Without the !== FALSE the strpos() function returns the position of the first occurrence of a substring in a string so it could be because it's returning position "0", which is a boolean false, that causes none of your if statements to be true.
You can use the !== or the === operator to check for "0" as a value and not a boolean false.
|
The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact
Interactive Tools for information on the downloading of the software or the purchasing of licenses.