1

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

Information Format

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

2
  • Read up on PHPexcel to read and write to excel sheets. Else you will need to use a database like mysql (and phpmyadmin as gui for mysql) and query it which is the best way to go. With phpexcel you can import an spreadsheet into an array or object, make modifications with php and export it back. Commented Apr 10, 2017 at 15:15
  • You will need to use the indexes of the array for the fastest search $arr = array('US'=>[...], 'UK'=>[...]); or dynamically rearrange the array based on search parameters Commented Apr 10, 2017 at 15:26

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Camille, this looks like the solution I'm after, how would I search the array based on those points though after I've passed them to getPrice?
I add body of search function getPrice
0

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
?>

1 Comment

This doesn't exactly answer the question. As stated in the question "I need to be able to run a function that uses the post variables" You should edit the answer to include such function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.