这是棘手的部分 在租赁应用程序中,我有这样的数组(当然数组大小和值可能会有所不同):
$dates = array();
// each "pipe" | is an item for visual help
$dates['2013-07-15'] = 2; // ||
$dates['2013-07-16'] = 2; // ||
$dates['2013-07-17'] = 5; // |||||
$dates['2013-07-18'] = 3; // |||
$dates['2013-07-19'] = 4; // ||||
$dates['2013-07-20'] = 2; // ||
$dates['2013-07-21'] = 1; // |
关键是租用天数,值是用户为特定产品租用的物品数量
天数都是连续的(我用another script分组了相邻的天数)
连续5天被视为“工作周”,特价适用:假设D是每日价格,W是每周价格
如您所见,在此示例中,日期范围共租用了5个项目:
...并总结以上所有
好的,到目前为止一直很好:我可以用文字表达这个概念
但是......如何将其翻译成脚本?对我来说真的很难!
非常感谢提前!
编辑:澄清一下,每个数组项的值表示用户特定产品租金的项目数;假设产品是“吸尘器品牌-X”,商店库存中有10件商品;清洁公司“Your-Clean-House inc。”得到了一笔大买卖,在这种情况下,将要租房:
Oozel回答有意义!为了使用我的示例,我只需稍微修改他的脚本:
foreach ($dates as $items){
for ($i=1; $i<=$items; $i++){
...
}
}
@oozel:我有一个数字(而不是一系列项目)'因特定的UI
Oversemplifying:
Vacuum Cleaner Brand-X
Calendar
July 2013
...
<td>
15<br>
<input type="checkbox" title="Activate this day" value="2013-07-15">
<select title="Choose how many Vacuum Cleaner Brand-X to be rent on this day">
<option>1</option>
...
<option>10</option>
</select>
</td>
<td>
16<br>
<input type="checkbox" title="Activate this day" value="2013-07-16">
<select title="Choose how many Vacuum Cleaner Brand-X to be rent on this day">
<option>1</option>
...
<option>10</option>
</select>
</td>
<td>
17<br>
<input type="checkbox" title="Activate this day" value="2013-07-17">
<select title="Choose how many Vacuum Cleaner Brand-X to be rent on this day">
<option>1</option>
...
<option>10</option>
</select>
</td>
...
答案 0 :(得分:1)
我不完全理解$ date数组结构,但我认为它应该是一个多维数组,如下所示,根据需要计算总价格
$dates['2013-07-15'] = array('item2', 'item1');
$dates['2013-07-16'] = array('item2', 'item1');
$dates['2013-07-17'] = array('item5', 'item4', 'item3', 'item2', 'item1');
$dates['2013-07-18'] = array('item3', 'item2', 'item1');
$dates['2013-07-19'] = array('item4', 'item3', 'item2', 'item1');
$dates['2013-07-20'] = array('item2', 'item1');
$dates['2013-07-21'] = array('item1');
然后计算每个项目的租用天数并计算出有多少W和D.
$rent_days = array();
foreach($dates as $date => $items) {
foreach($items as $item) {
if(!isset($rent_days[$item]))
$rent_days[$item] = 1;
else $rent_days[$item]++;
}
}
$w = 10.5; // as an example
$d = 2.3;
foreach($rent_days as $item => $n) {
$w_num = floor($n/5); // number of weeks (namely, 5 consecutive days)
$d_num = $n % 5; // number of days
printf('Total price for %s: %.2f<br/>', $item, $w_num*$w + $d_num*$d);
}
输出
/*
Total price for item2: 12.80
Total price for item1: 15.10
Total price for item5: 2.30
Total price for item4: 4.60
Total price for item3: 6.90
*/