Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于给定表的Php价格计算_Php_Matrix - Fatal编程技术网

基于给定表的Php价格计算

基于给定表的Php价格计算,php,matrix,Php,Matrix,使用此价格表,我们计算给定输入度量的价格 例如: (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_

使用此价格表,我们计算给定输入度量的价格

例如:

(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;

您可以使用
ceil
将此函数的宽度和高度四舍五入

为了简单起见,您可以使用array键
height\u width
存储价格

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 ];
}
你可以打电话

echo calculate_price( 30, 56 ); /* width and height parameters */
这将导致

20

您可以使用
ceil
将此函数的宽度和高度四舍五入

为了简单起见,您可以使用array键
height\u width
存储价格

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 ];
}
你可以打电话

echo calculate_price( 30, 56 ); /* width and height parameters */
这将导致

20

可以使用多维数组

$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];

可以使用多维数组

$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];

根据您的代码,如果您没有数据库来获取这些$price,我认为您可以尝试以下方法:

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 ;
}
函数计算价格($width,$height){
//您的$new_宽度与您的$width一致

如果($width基于您的代码,如果您没有数据库来获取$price,我认为您可以尝试以下方法:

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 ;
}
函数计算价格($width,$height){
//您的$new_宽度与您的$width一致

如果($width基于您提供的信息,则有两条逻辑路径。第一条路径是宽度小于或等于25,或者宽度大于25。然后您必须计算出高度

考虑以下几点:

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;
}
echo calculate_price(30, 56) . "<br />";
echo calculate_price(12, 267) . "<br />";
echo calculate_price(25, 249) . "<br />";
我得到:

20
30
24
根据需要设置格式(例如:
echo sprintf($%d.00),计算价格(30,56));

全面测试

<?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 />";
?>

根据您提供的信息,您有两条逻辑路径。第一条是宽度小于或等于25,或者宽度大于25。然后您必须计算出高度

考虑以下几点:

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;
}
echo calculate_price(30, 56) . "<br />";
echo calculate_price(12, 267) . "<br />";
echo calculate_price(25, 249) . "<br />";
我得到:

20
30
24
根据需要设置格式(例如:
echo sprintf($%d.00),计算价格(30,56));

全面测试

<?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 />";
?>

我只是好奇,为什么你不使用2D数组而不是在键中使用下划线?@WilliamPerron我的意思是我们可以使用2D数组,但我发现这种方法对初学者来说太简单了。感谢朋友提供了这种解决方案。但我的问题是我的excel列表很大,如果我用这种方法编写程序,会有很多行。在定价上有什么逻辑吗?amula?@Abilash实际上定价没有逻辑,这是客户给出的。但问题是宽度、高度截止值有逻辑。一级到二级的高度增加50,宽度增加25。我只是好奇,为什么你不选择2D数组,而不是在键中使用下划线?@Williamperon我的意思是我们可以这样做e 2D数组,但我发现这种方法对于初学者来说过于简单。感谢朋友提供这种解决方案。但我的问题是,我的excel列表很大,如果我用这种方法编写程序,会有很多行。定价是否有逻辑?公式?@abilash实际上定价没有逻辑,这是客户给出的。但问题是,这是有逻辑的在宽度上,高度截止。一层到二层的高度增加50,宽度增加25。