3

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.

2
  • Not sure about the question. Is the code not going to either the yes or the no code blocks? Then you're SESSION variable is the problem. try var_dump($_SESSION) to see what it's holding. Commented Jul 31, 2015 at 2:00
  • Yeah sorry due to being a novice it is still somewhat hard to know the exact terms and phrases to use for all you advanced guys to understand so i end up going to long way round a short road! Basically i am trying to get the 2 blocks of code to run based on the session value so for example if the $_SESSION['service1'] is equal to Yes then it will run the first block of code and not the second due to the value being equal to == "yes" but if it was not set to "yes" and set to == "no" it would skip the first block and run the second block. Maybe this could be done with an elseif statement? Commented Jul 31, 2015 at 2:04

1 Answer 1

2

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

4 Comments

angelcool.net, i need the first block of calculation code to only run if the variable $_SESSION['service1'] is equal to == "yes" and if its not set to "yes" skip the first block and run the second block of code as the first block of calcuaton code will have different output prices for example if you see the first block i posted the first value in the array is set to 'value' => 249 and in the second block of code the first array value is set to 'value' => 450 thats because service1 being set to yes will run the first block and if its not ="yes" then it will run the second if you get me?
Just seen it now :) im going to give it a test asap as i am just moving my workstation to another room so as soon as i get it moved i will test it right away.. shouldn't be to long.. Looks good though and surprised how quickly you did that! i would still be here tomorrow trying to figure the first second out lol! I Really Appreciate it.
being a beginner the one liner with the closure might be hard for the op to follow
Thanks Alot mate ive not had chance to check it yet.. I did not even realise you had edited the original post so thanks, You was very quick on the ball so much appreciated. This little bit of code will go a long way when i use a dynamic landing page :)

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.