Reputation: 1761
I've currently got a page that receives form data, I have two pieces of post information -
$country_name = $_POST[“country_name”];
$price_point = $_POST[“price_point”];
I have a spreadsheet currently that has this information in columns
I need to be able to run a function that uses the post variables and finds the corresponding value from the array.
I haven't created an array yet, as I don't know the best way to format this array. This is where I'm struggling..
Example output would be;
Post value for country is US. The price point is 2. The value would be 1.5.
Major love for anyone that can help me out with the below!
Thanks
Upvotes: 1
Views: 55
Reputation: 571
alternatively i would just put the values in a multidimensional array like my example below
once this spreadsheet get's a lot bigger i would advice to generate the array from values out of a database.
<?php
$country_name = 'UK';
$price_point = 4;
$arr = array(
'UK' => array(
1 => 1.5,
2 => 1.6,
3 => 1.9,
4 => 12
),
'US' => array(
1 => 1.3,
2 => 1.5,
3 => 1.6,
4 => 1.7
),
'SWEDEN' => array(
1 => 0.8,
2 => 0.7,
3 => 0.5,
4 => 0.3
)
);
$value = $arr[$country_name][$price_point];
echo $value;
// echoes 12
?>
Upvotes: 0
Reputation: 2531
I would create an array like that
$dataPoints = array(
array(
'point' => 1,
'price' => 1.5,
'country' => 'UK'
),
array(
'point' => 1,
'price' => 1.3,
'country' => 'US'
),
array(
'point' => 1,
'price' => .8,
'country' => 'SWEDEN'
),
...
)
And a function like
function getPrice($pricePoint, $country) {
// Search in array for good price
foreach ($dataPoints as $dataPoint) {
if ($dataPoint['point'] == $pricePoint && $dataPoint['country'] == $country) {
return $dataPoint['price'];
}
}
return null;
}
And call it with your post parameters for print
$pp = filter_input(INPUT_POST, 'price_point');
$country = filter_input(INPUT_POST, 'country_name');
$price = getPrice($pp,$country);
echo 'Country is ', $country, '. The price point is ', $pp, '. The price is ', $price;
Upvotes: 1