price matrix?

3 posts by 2 authors in: Forums > CMS Builder
Last Post: February 14, 2013   (RSS)

By Joef5 - February 14, 2013

Looking for an idea on how to pull a price based on incremental width and length for window blinds

The data is presented as width across the top, length along the side, price at the intersection

xxx    24                     27                 34                  42 ...

24     1                         2                 3                    4

36      2                       3                 5                      7

48      4                       7                 9                     12

So if you wanted the result for 34 wide and 48 long it would return 9

The matrix is unique to each item being listed, so 25 products would 25 different matrix data,

though some, but not all would share the same numbers for the incremental width and length numbers.

Maybe as much a database question as it is a cms question as to how to do this.

Any ideas?

By Chris - February 14, 2013

Hi Joef5,

That's a tricky one! I think we normally suggest consulting for something this complicated, but I wanted to give this a shot.

One idea would be to use a text box field and have people enter the information exactly as you've done above with the spaces and everything. Then, on a viewer page, you can parse the field like this:

// parse price matrix field into array-of-arrays
function parsePriceMatrixLine($line) {
  $line = trim($line);
  if (!$line) { return array(); }
  return coalesce(preg_split("/\s+/", $line), array());
}
$priceMatrix = explode("\n", $record['price_matrix']); // split string into rows
$priceMatrix = array_map('parsePriceMatrixLine', $priceMatrix); // split lines into cells
$priceMatrix = array_filter($priceMatrix); // strip blank lines

Then you can look up values like this:

function lookupPriceMatrix($priceMatrix, $rowValue, $columnValue) {
  $rowIndex = @array_search($rowValue, array_pluck($priceMatrix, 0));
  $colIndex = @array_search($columnValue, $priceMatrix[0]);
  if (!$rowIndex || !$colIndex) { return null; }
  return @$priceMatrix[$rowIndex][$colIndex];
}

For example:

$value = lookupPriceMatrix($priceMatrix, 36, 34);
showme("lookupPriceMatrix returned: $value");

Note that with this approach, you'd need to make sure that price matrix fields are filled out correctly. If someone left off the "xxx", your lookups would be incorrect.

Does that help?

All the best,
Chris