jeudi 30 juillet 2015

Run the Correct Array function based on a session variable php

As a very novice php coder and beginner in web development i am in need of some advice and help, I have a calculation.php script that i am including into a landing page.. The script calculates a price depending on the user input value during the form process. Without making things to complicated and because i am going to try and run a dynamic landing page, The calculated price the script outputs can be different due to 2 different services we provide so service 1 is cheaper than service 2.

How can i go about having the array function run based if a session variable is equal to a specific value for example:

<?php
session_start();
if($_SESSION['service1'] == 'yes') {

Here is the Calculation.php script:

$data = array(
    array(
        'min' => "0",
        'max' => "100,000",
        'value' => 249
    ),
    array(
        'min' => "100,001",
        'max' => "200,000",
        'value' => 255
    )
);

function getAdjustedPrice($price, &$table) {
    $priceData = current(array_filter($table, function($value) use(&$price) {
        return $value['min'] <= $price && $value['max'] >= $price;
    }));
    return $priceData['value'];
}

$input = intval($_SESSION["servicecost"]);
printf("", 
       $input, 
       getAdjustedPrice($input, $data));

and if the session variable 1 is not set to yes but is set to "NO" then run the second variant of the calculation and then i will echo the output as variant number 2 is basically service 2 and is a higher price..

<?php
session_start();
if($_SESSION['service1'] == 'no') {

$data = array(
    array(
        'min' => "0",
        'max' => "100,000",
        'value' => 450
    ),
    array(
        'min' => "100,001",
        'max' => "200,000",
        'value' => 600
    )
);

function getAdjustedPrice($price, &$table) {
    $priceData = current(array_filter($table, function($value) use(&$price) {
        return $value['min'] <= $price && $value['max'] >= $price;
    }));
    return $priceData['value'];
}

$input = intval($_SESSION["servicecost"]);
printf("", 
       $input, 
       getAdjustedPrice($input, $data));

Hope this has made sense, Ive tried to do it myself using the elseif state and doing it like: if($_SESSION['service1'] == 'yes') { then run the code below it and then } else { if($_SESSION['service1'] == 'no') { but i have had no luck and constant silly errors. Any advice or input is greatly appreciated. Thanks.

Aucun commentaire:

Enregistrer un commentaire