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

The first CMS Builder reference book is now available on-line!







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,

Daryl Maximo
PHP Programmer - interactivetools.com

Thanks Daryl,

Figured it was something silly like that. :-((

Appreciate your help,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







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,

Daryl Maximo
PHP Programmer - interactivetools.com

Thanks Daryl,

The !== FALSE did the trick

So much to learn...

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php