使用此价格表我们正在计算给定输入测量的价格
示例:
(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;
我写了一个函数来根据给定的值计算价格,这个函数有助于根据表格得到new_width
和height
,但我不知道怎么弄价格基于此表。
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 ;
}
答案 0 :(得分:1)
您可以使用ceil
来舍入此功能的宽度和高度
为简单起见,您可以使用数组以键height_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
答案 1 :(得分:1)
根据您提供的信息,您有两条逻辑路径。第一个是宽度小于或等于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);
我们的输出为:20
。
运行以下内容:
echo calculate_price(30, 56) . "<br />";
echo calculate_price(12, 267) . "<br />";
echo calculate_price(25, 249) . "<br />";
我明白了:
20
30
24
根据需要格式化(E.G。:echo sprintf("$%d.00", calculate_price(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 />";
?>
结果:
$20.00
$30.00
$24.00
$0.00
答案 2 :(得分:0)
您可以使用多维数组
$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];
答案 3 :(得分:0)
根据你的代码,如果你没有数据库来获得这些价格,我想你可以试试这样的话:
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 ;
}
但我认为你也可以找到其他方式来实现它,正如其他anwser所建议的那样!
这是你在找什么?它不是很灵活,因为您不使用数据库来获取这些值,但它应该可以工作