Run Correct Array function based on php session variable
As a very new php coder and newbie to web development, I need some advice and help, I have a .php script calculation that I include in the landing page. The script calculates the price based on user input during the form process. Without complicating things, and because I'm about to launch a dynamic landing page. The computed cost of script exits can be different due to the two different services we provide, so service 1 is cheaper than service 2.
How can I run an array function if the session variable is equal to a certain 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 session variable 1 is not set to yes but is set to NO then run the second computation option and then I go out as option number 2 is basically service 2 and is a higher cost. ...
<?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 made sense, I tried to do it myself using the elseif state and do it like: if ($ _ SESSION ['service1'] == 'yes') {then run the code below it and then} else { if ($ _ SESSION ['service1'] == 'no') {but I haven't had any luck and constant stupid mistakes. Any advice or input is appreciated. Thank.
source to share
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!
The EDIT: . For the third service, you will need to add data to the array and check the correct value in the session, you can use a switch statement, the trick is assigning the correct service, I think you will need to store different values ββin your session $ _SESSION ['service1'].
So your code will now look something like this:
<?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 ])
);
source to share