Reputation: 773
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.
Upvotes: 3
Views: 110
Reputation: 2546
If I understood correctly the following should do what you want:
<?php
session_start();
$service = $_SESSION['service1'] == 'yes'
? 'service1'
: 'service2';
$data = array(
'service1'=>array(
array(
'min' => "0",
'max' => "100,000",
'value' => 249
),
array(
'min' => "100,001",
'max' => "200,000",
'value' => 255
)
),
'service2'=>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[ $service ])
);
Good luck!
EDIT: For a third service you will need to add the data to the array and check for the right value in your session, you can use a switch statement, the trick is assigning the right service, I guess you will need to store different values in your session $_SESSION['service1'].
So your code will now look something like :
<?php
session_start();
switch ($_SESSION['service1']) {
case 'yes':
$service = 'service1';
break;
case 'no':
$service = 'service2';
break;
case '???????':
$service = 'service3';
break;
}
$data = array(
'service1'=>array(
array(
'min' => "0",
'max' => "100,000",
'value' => 249
),
array(
'min' => "100,001",
'max' => "200,000",
'value' => 255
)
),
'service2'=>array(
array(
'min' => "0",
'max' => "100,000",
'value' => 450
),
array(
'min' => "100,001",
'max' => "200,000",
'value' => 600
)
),
'service3'=>array(
array(
'min' => "0",
'max' => "100,000",
'value' => 8888888888
),
array(
'min' => "100,001",
'max' => "200,000",
'value' => 9999999999
)
)
);
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[ $service ])
);
Upvotes: 2