Abilash Erikson
Abilash Erikson

Reputation: 351

Php Price calculation based on given table

using this price table we are calculating price of the given input measurement

enter image description here

example:

(1) width = 30; , height =56 ; Price=? 

 so 
 (width>25){ new_width=50; }
 (height<100){ new_height=100; }

    so price = 20;


(2) width =12; height=267;

    (width<25){ new_width=25; }
    (height>250){ new_height=300; }

    so price= 30;

I wrote a function for to calculate price based on the given value, and this function help to get the new_width , and height based on the table , But I don't know how to get price based on this table.

function calculate_price($width,$height ){

        if($width<25){
             $new_width=25;
          }elseif($width>25 && $width<50 ){
             $new_width=50;
          }else{
            $new_width=50;
          }  


        if($width<100){
             $new_width=100;
          }elseif($width>100 && $width<150 ){
             $new_width=150;
          }elseif($width>150 && $width<200 ){
             $new_width=200;
          }elseif($width>200 && $width<250 ){
             $new_width=250;
          }elseif($width>250 && $width<300 ){
             $new_width=300;
          }
          else{
            $new_width=300;
          }


        $price=needed forumula [ based on new_width & new_height ];

      return $price ;



    }

Upvotes: 0

Views: 515

Answers (4)

Twisty
Twisty

Reputation: 30893

Base on the info you provided, you have two logical paths. The first, is the width less than or equal to 25 or is the width greater then 25. Then you have to reason out the height.

Consider the following:

function calculate_price($width, $height){
  $price = false;
  if($width > 25){
    switch(true){
      case $height <= 100:
        $price = 20;
        break;
      case $height <= 150:
        $price = 25;
        break;
      case $height <= 200:
        $price = 31;
        break;
      case $height <= 250:
        $price = 42;
        break;
      case $height <= 300:
        $price = 45;
        break;
    }
  } else {
    switch(true){
      case $height <= 100:
        $price = 10;
        break;
      case $height <= 150:
        $price = 15;
        break;
      case $height <= 200:
        $price = 18;
        break;
      case $height <= 250:
        $price = 24;
        break;
      case $height <= 300:
        $price = 30;
        break;
    }
  }
  return $price;
}

With the following input: echo calculate_price(30, 56); we would have an output of: 20.

Running the following:

echo calculate_price(30, 56) . "<br />";
echo calculate_price(12, 267) . "<br />";
echo calculate_price(25, 249) . "<br />";

I get:

20
30
24

Format as needed (E.G.: echo sprintf("$%d.00", calculate_price(30, 56));)

Full Test

<?php
function calculate_price($width, $height){
    $price = false;
    if(!is_int($width) || !is_int($height)){
        return $price;
    }
    if($width > 25){
        switch(true){
            case $height <= 100:
            $price = 20;
            break;
            case $height <= 150:
            $price = 25;
            break;
            case $height <= 200:
            $price = 31;
            break;
            case $height <= 250:
            $price = 42;
            break;
            case $height <= 300:
            $price = 45;
            break;
        }
    } else {
        switch(true){
            case $height <= 100:
            $price = 10;
            break;
            case $height <= 150:
            $price = 15;
            break;
            case $height <= 200:
            $price = 18;
            break;
            case $height <= 250:
            $price = 24;
            break;
            case $height <= 300:
            $price = 30;
            break;
        }
    }
    return $price;
}

echo sprintf("$%d.00", calculate_price(30, 56)) . "<br />";
echo sprintf("$%d.00", calculate_price(12, 267)) . "<br />";
echo sprintf("$%d.00", calculate_price(25, 249)) . "<br />";
echo sprintf("$%d.00", calculate_price(-1, 'a')) . "<br />";
?>

Results:

$20.00
$30.00
$24.00
$0.00

Upvotes: 1

Eddie
Eddie

Reputation: 26844

You can use ceil to round up the width and height this function

You can use array to store the price with key height_width for simplicity.

function calculate_price($width, $height) {
    $price = array(
        "100_25" => 10,
        "100_50" => 20,
        "150_25" => 15,
        "150_50" => 25,
        "200_25" => 18,
        "200_50" => 31,
        "250_25" => 24,
        "250_50" => 42,
        "300_25" => 20,
        "300_50" => 45,
    );

    $newWidth = ceil($width / 25) * 25; //Round up by 25
    $newHeight = ceil($height / 50) * 50; //Round up by 50

    return $price[ $newHeight . "_" . $newWidth ];
}

You can call

echo calculate_price( 30, 56 ); /* width and height parameters */

This will result to

20

Upvotes: 1

Micka&#235;l Leger
Micka&#235;l Leger

Reputation: 3440

Based of your code, if you don't have database to get those $price, I think you can try somehting like this :

function calculate_price($width,$height ){
    // Your $new_width according to your $width
    if($width<=25){
        $new_width=25;
    }else
        $new_width=50;
    }  

    // Your $new_height according to your $height
    if($height<=100){
        $new_height=100;
    }elseif($height>100 && $height<=150 ){
        $new_height=150;
    }elseif($height>150 && $height<=200 ){
        $new_height=200;
    }elseif($height>200 && $height<=250 ){
        $new_height=250;
    }elseif($height>250){
        $new_height=300;
    }

    // Now, according to your new value you will find the price with some test :
    if ($new_width == 25) {
        switch($new_height) {
            case 100 : $price = 10; break;
            case 150 : $price = 15; break;
            case 200 : $price = 18; break;
            case 250 : $price = 24; break;
            case 300 : $price = 30; break;
        }
    } else {
        switch($new_height) {
            case 100 : $price = 20; break;
            case 150 : $price = 25; break;
            case 200 : $price = 31; break;
            case 250 : $price = 42; break;
            case 300 : $price = 45; break;
        }
    }

  return $price ;
}

But I think you can find other way to achieve it too as suggested in other anwser !

Is this what you are looking for? It's not very flexible since you don't use database to get those value but it should work

Upvotes: 0

Madhu
Madhu

Reputation: 326

You can use Multidimensional Array

$x = "300";
$y = "50";

$multi  = array();

$multi["100"]["25"] = "10";
$multi["100"]["50"] =  "20";
$multi["200"]["25"] = "30";
$multi["300"]["50"] =  "40";    

echo $multi[$x][$y];

Upvotes: 0

Related Questions