Determinn Sort Order from single record editor pull down list
6 posts by 2 authors in: Forums > CMS Builder
Last Post: November 23, 2015 (RSS)
Hi All
I have a single record editor (Common Information) and a single value pull down list field called sort_order with the values, Newest Record First, Oldest Record First, Random Records, and None.
I would expect the following to pass the $orderedBy variable based on the value of the field, but it doesn’t, and I can’t seem to figure out why.
<?php $orderedBy = "RAND()";
if (strpos($common_informationRecords['sort_order'], Rand )) { $orderedBy = "RAND()"; }
if (strpos($common_informationRecords['sort_order'], Oldest )) { $orderedBy = "createdDate DESC"; }
if (strpos($common_informationRecords['sort_order'], Newest )) { $orderedBy = "createdDate ASC"; }
if (strpos($common_informationRecords['sort_order'], None )) { $orderedBy = "" ; }
?>
Order Variable <?php echo $orderedBy // this remains RAND() regardless of the list field value?>
<?php list($studio_art_imagesRecords, $studio_art_imagesMetaData) = getRecords(array(
'tableName' => 'studio_art_images',
'orderBy' => $orderedBy,
));
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By Daryl - November 23, 2015
Hi Jerry,
You're missing quotation marks on your strpos() functions.
if (strpos($common_informationRecords['sort_order'], "Rand" )) { $orderedBy = "RAND()"; }
Cheers,
PHP Programmer - interactivetools.com
Thanks Daryl,
Figured it was something silly like that. :-((
Appreciate your help,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Hi Daryl,
I'm not feeling too bright. And I can't get this simple code to work.
I'm using :
<?php $orderedBy = ""; ?>
<?php
if (strpos($common_informationRecord['sort_order'], 'Rand' )) { $orderedBy = 'RAND()'; };
if (strpos($common_informationRecord['sort_order'], 'Oldest' )) { $orderedBy = 'createdDate DESC'; };
if (strpos($common_informationRecord['sort_order'], 'Newest' )) { $orderedBy = 'createdDate ASC'; };
if (strpos($common_informationRecord['sort_order'], 'None' )) { $orderedBy = ''; };
?>
Top Order Variable <?php echo $orderedBy ?>
Common Information <?php echo $common_informationRecord['sort_order'] ?>
<?php list($studio_art_imagesRecords, $studio_art_imagesMetaData) = getRecords(array(
'tableName' => 'studio_art_images',
'orderBy' => $orderedBy,
));
but the Common Information echoes Random Order (or any other value chosen in the section) but the Top Order variable remains blank no matter what I've tried:P sort_order:label, double quotes around the strpos value, stripos to make surre that is was not a case sensitive issue, semicolons at the end of each if line and without them
Still not sure what I'm doing wrong, it should be so simple.
I've attached the complete file and hope you can spot something.
Thanks,
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
By Daryl - November 23, 2015
Hi Jerry,
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 !== or === operator to check for "0" as a value and not a boolean false. For example:
if (strpos($common_informationRecord['sort_order'], 'Rand') !== FALSE) { $orderedBy = 'RAND()'; }
if (strpos($common_informationRecord['sort_order'], 'Oldest') !== FALSE) { $orderedBy = 'createdDate DESC'; }
if (strpos($common_informationRecord['sort_order'], 'Newest') !== FALSE) { $orderedBy = 'createdDate ASC'; }
if (strpos($common_informationRecord['sort_order'], 'None') !== FALSE) { $orderedBy = ''; }
Also, there are ";" at the end of the strpos() lines on your code.
Cheers,
PHP Programmer - interactivetools.com
Thanks Daryl,
The !== FALSE did the trick
So much to learn...
Jerry Kornbluth
Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php