如何创建一个小PHP脚本,根据部分/价格计算一些项目?
我想用PHP编写一个脚本,这将为我做一些数学计算 它应完成以下任务: 示例 假设你有一家网上果汁店。 你想如何创建一个小PHP脚本,根据部分/价格计算一些项目?,php,database,Php,Database,我想用PHP编写一个脚本,这将为我做一些数学计算 它应完成以下任务: 示例 假设你有一家网上果汁店。 你想 将几种果汁保存到数据库中(想想:橙汁、柠檬汁、苹果汁) 您还需要保存这些果汁的分量和价格(例如100ml/2,50美元) 您想要混合/组合不同的果汁,并获得毫升/美元的总量 如果我们加起来: 橙汁:100毫升/2,50美元|我要50毫升=1,25美元 苹果汁:100毫升/1,50美元|我要10毫升=0,15美元 总果汁混合:60ml=1,40美元。 到目前为止还不错。 我编写了以下代码:
<?php
function devide() {
$portion = 100; //variable for ml
$price = 2.50; //variable for price
$portionNew = 50; //value for new portion put into total mix
if($portionNew > 0) {
echo ($price / 100 * $portionNew); //calculates price of new portion
}
}
echo "Total Juice" . (" = ");
echo devide() . (" USD") ;
?>
此代码仅计算从一种果汁中取出的部分的价格。
如果你从100毫升果汁中提取30毫升,它将与价格相呼应
以下是我的问题:
$portionNew
)$portionNews/$price
)来告诉我最终的价格和我的果汁总混合比例devide
函数中添加类似echo'部分:'.$portionNew
。(我相信您可能想称之为除法
?)Juice
类,但我认为在这种情况下这样做太过分了。我可能会坚持使用一个数组来存储果汁及其价格,并使用一个函数来混合多种果汁// In this case, we have an array that maps the juice type to a price (in cents
// per 100 ml).
$juices = array(
'orange' => 250,
'apple' => 150
);
// If you decide you need to store more information about a juice, you can change
// this array to map a juice type to another associative array that contains the
// price and your additional information.
// Beware: if you use this, you have to change the mixJuices function to access
// a juice's "price" value:
// $pricePerLiter = $juices[$juiceType]['price'] * 10;
$juices = array(
'orange' => array(
'price' => 250,
'color' => 'orange'
),
'apple' => array(
'price' => 150,
'color' => 'yellow'
)
);
// The function needs to know what's currently in the mix, which is why it's
// taking the $currentMix as a parameter. Also, it needs to know about all the
// available juices, which is why it's taking the $juices as another parameter.
// A mix is represented by an associative array that contains the current amount
// of juice in the mix (identified by "portion") and the current price
// (identified by "price"). Note that you need to pass the $juices array I
// defined earlier, so it can access the different types of juices and their
// prices.
function mixJuices($currentMix, $juices, $juiceType, $juicePortion)
{
$pricePerLiter = $juices[$juiceType] * 10;
$price = $pricePerLiter * $juicePortion / 1000;
$currentMix['portion'] = $currentMix['portion'] + $juicePortion;
$currentMix['price'] = $currentMix['price'] + $price;
return $currentMix;
}
// At the beginning, there's no juice at all, so it doesn't cost anything (yet)
$mix = array('portion' => 0, 'price' => 0);
// Let's mix some juice in
$mix = mixJuices($mix, $juices, 'apple', 500);
$mix = mixJuices($mix, $juices, 'orange', 250);
// Print the amount of juice and the price
// The price is divided by 100 so we can display it in dollars instead of cents
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
$mix['portion'], $mix['price'] / 100);
请注意,我是用美分而不是美元来设定价格的。通过这样做,我可以避免在计算价格时使用浮动以及与之相关的所有问题(请参阅)
混合果汁的功能可能如下所示:
// In this case, we have an array that maps the juice type to a price (in cents
// per 100 ml).
$juices = array(
'orange' => 250,
'apple' => 150
);
// If you decide you need to store more information about a juice, you can change
// this array to map a juice type to another associative array that contains the
// price and your additional information.
// Beware: if you use this, you have to change the mixJuices function to access
// a juice's "price" value:
// $pricePerLiter = $juices[$juiceType]['price'] * 10;
$juices = array(
'orange' => array(
'price' => 250,
'color' => 'orange'
),
'apple' => array(
'price' => 150,
'color' => 'yellow'
)
);
// The function needs to know what's currently in the mix, which is why it's
// taking the $currentMix as a parameter. Also, it needs to know about all the
// available juices, which is why it's taking the $juices as another parameter.
// A mix is represented by an associative array that contains the current amount
// of juice in the mix (identified by "portion") and the current price
// (identified by "price"). Note that you need to pass the $juices array I
// defined earlier, so it can access the different types of juices and their
// prices.
function mixJuices($currentMix, $juices, $juiceType, $juicePortion)
{
$pricePerLiter = $juices[$juiceType] * 10;
$price = $pricePerLiter * $juicePortion / 1000;
$currentMix['portion'] = $currentMix['portion'] + $juicePortion;
$currentMix['price'] = $currentMix['price'] + $price;
return $currentMix;
}
// At the beginning, there's no juice at all, so it doesn't cost anything (yet)
$mix = array('portion' => 0, 'price' => 0);
// Let's mix some juice in
$mix = mixJuices($mix, $juices, 'apple', 500);
$mix = mixJuices($mix, $juices, 'orange', 250);
// Print the amount of juice and the price
// The price is divided by 100 so we can display it in dollars instead of cents
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
$mix['portion'], $mix['price'] / 100);
此函数也可以编写为使用全局$mix
变量。如果这样做,就不需要将$currentMix传递给函数。但是,您也将失去同时创建多个混音的能力(因为对修改后的mixJuices
函数的所有调用都将修改相同的全局混音)
您可以使用我在上面定义的函数,如下所示:
// In this case, we have an array that maps the juice type to a price (in cents
// per 100 ml).
$juices = array(
'orange' => 250,
'apple' => 150
);
// If you decide you need to store more information about a juice, you can change
// this array to map a juice type to another associative array that contains the
// price and your additional information.
// Beware: if you use this, you have to change the mixJuices function to access
// a juice's "price" value:
// $pricePerLiter = $juices[$juiceType]['price'] * 10;
$juices = array(
'orange' => array(
'price' => 250,
'color' => 'orange'
),
'apple' => array(
'price' => 150,
'color' => 'yellow'
)
);
// The function needs to know what's currently in the mix, which is why it's
// taking the $currentMix as a parameter. Also, it needs to know about all the
// available juices, which is why it's taking the $juices as another parameter.
// A mix is represented by an associative array that contains the current amount
// of juice in the mix (identified by "portion") and the current price
// (identified by "price"). Note that you need to pass the $juices array I
// defined earlier, so it can access the different types of juices and their
// prices.
function mixJuices($currentMix, $juices, $juiceType, $juicePortion)
{
$pricePerLiter = $juices[$juiceType] * 10;
$price = $pricePerLiter * $juicePortion / 1000;
$currentMix['portion'] = $currentMix['portion'] + $juicePortion;
$currentMix['price'] = $currentMix['price'] + $price;
return $currentMix;
}
// At the beginning, there's no juice at all, so it doesn't cost anything (yet)
$mix = array('portion' => 0, 'price' => 0);
// Let's mix some juice in
$mix = mixJuices($mix, $juices, 'apple', 500);
$mix = mixJuices($mix, $juices, 'orange', 250);
// Print the amount of juice and the price
// The price is divided by 100 so we can display it in dollars instead of cents
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
$mix['portion'], $mix['price'] / 100);
这种方法可以让你做各种有趣的事情 假设您有两位客户想要订购果汁混合物。通过创建两个不同的
$mix
阵列,您可以轻松混合同一类型的多种果汁:
// Process customer 1's order
$mixCustomer1 = array('portion' => 0, 'price' => 0);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'apple', 250);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'orange', 250);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
$mixCustomer1['portion'], $mixCustomer1['price'] / 100);
// 500 milliliters of this juice mix will cost 10.00 USD.
// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
$mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 11.00 USD.
现在让我们假设客户2有某种代金券,可以让他享受果汁20%的折扣:
// Apply the discount
$discountedJuices = array_map(function($price) { return (1 - 0.2) * $price; }, $juices);
// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
$mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 8.80 USD.
对于美元,你想用
而不是,
作为小数点,即0.15美元
伙计,我都不知道该怎么感谢你!你一定花了不少功夫才把这一切写下来。太好了,非常感谢。顺便说一句:至于拼写错误,比如(devide/divide),我不是母语人士,所以有时会感到困惑:)我还有很多问题要问:1)我想在我的果汁中添加更多信息,比如说营养。在第一个数组$juices中,您说过可以通过将juice类型映射到另一个数组来存档它。我在谷歌上搜索了一下,但我似乎没有找到正确的答案。你能在这里进一步解释吗?问题2:$mix是关联数组吗?这对我来说似乎非常复杂,而且我不理解语法…特别是在括号()中。我读起来像:$var=name($var,$array,$key,newValue)。但举例来说,为什么偏执论中第二次出现$var?最后但并非最不重要的一点,我要感谢你教我关于“sprint”的知识。我以前不知道…用谷歌搜索并理解它!干得好!THX关于问题1:基本上,不要说'orange'=>250
(juice=>price),而是说'orange'=>array('price'=>250,'color'=>orange')
(juice=>information array)。我在回答中加了一个例子。关于问题2:是的,$mix
是一个关联数组mixJuices
获取包含当前混合信息的数组,并返回包含新混合信息的相同类型的数组。这就是为什么您要传递当前的$mix
(这样它就知道混合中有什么),并将结果分配给$mix
(这样您就可以记住添加到混合中的内容)。