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)

By gkornbluth - November 21, 2015

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

By gkornbluth - November 23, 2015

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 gkornbluth - November 23, 2015

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

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
Attachments:

artworka3.php 17K

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