price matrix?

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

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

By Joef5 - February 14, 2013

Thanks Chris,

I'll look into your suggestion.  The xxx is just a place holder for the top left corner as it might be in a spreadsheet layout, but certainly won't be an issue so far as including it, as most likely I'll be the one entering the data.  My client will have access though.

Added to this mix is the need to convert measured dimensions, rounded up to the next increment as the blinds are cut down from the standard increments to fit customer measurements.  That part I've worked out, doing the coding now to test to see if

my idea works as intended.  The price matrix or price chart if you want to think of it that way, does seem a tricky one.

Certainly open to any additional thoughts on this one if anything comes to mind.  I give this one a try.