sorting fields within a record

3 posts by 2 authors in: Forums > CMS Builder
Last Post: June 18, 2012   (RSS)

By Deborah - June 16, 2012

I have an editor that allows for multiple numerical price options for a product listing. I need to compare the numbers of the various fields and display the lowest value of those fields on the list page.

(Fields only allow entry of numbers and decimal point.)
EXAMPLE:
field: price_option_01 = 10.00
field: price_option_02 = 6.00
field: price_option_03 = 8.00

The desired value returned on the page would be: 6.00

I don't know how to begin to do this or if it can be done, but am hoping someone might have such a method in their CMSB/PHP toolbox. And thanks in advance!

~ Deborah

Re: [Deborah] sorting fields within a record

By Jason - June 18, 2012

Hi Deborah,

You can try using the PHP min() function to get the lowest numerical value like this:

EXAMPLE:
$price = min($record['price_option_01'], $record['price_option_02'], $record['price_option_03']);

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] sorting fields within a record

By Deborah - June 18, 2012

Hi, Jason.

Yes! That is the solution I needed. After plugging in the function, I realized I need to account for null fields, so I went online and found a way to filter those out. (Might be of help to someone else.)

The following returns lowest numerical value and ignores null fields:

$min = array($record['option_01_price'], $record['option_02_price'], $record['option_03_price']);
$min = array_filter($min);
echo min($min);


Thanks very much for your help!
~ Deborah