-1

Here comes the tricky part
In a rent application, I have arrays like this (of course array size and values may vary):

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

The keys are the rent days, and the values are the # of items rent by a user for a specific product

Days are all consecutive (I yet grouped adjacent days with another script)

5 consecutive days are considered a "working week", and special price applies: let's say D is the daily price and W is the weekly price

As you can see, in this example a total of 5 items are rent for the dates' range:

  1. item 1 for 1 week and 2 spare days (15-16-17-18-19-20-21)
    echo total price: 1*W + 2*D
  2. item 2 for 1 week and 1 spare day (15-16-17-18-19-20)
    echo total price: 1*W + 1*D
  3. item 3 for 3 spare days (17-18-19)
    echo total price: 0*W + 3*D
  4. item 4 for 2 spare days (17-19)
    echo total price: 0*W + 2*D
  5. item 5 for 1 day (17)
    echo total price: 0*W + 1*D

... and sum all of the above

Ok, so far so good: I can express the concept in words

But... how to translate this into a script? Really difficult to me!

Many thanks in advance!

EDIT: to clarify, values for each array item represents the number of items of that specific product rent by user; let's say product is "Vacuum Cleaner Brand-X" and there are 10 items in the shop's stock; the cleaning company "Your-Clean-House inc." got a big deal and, in this case, is going to rent:

  • 2 "Vacuum Cleaner Brand-X" on 2013-07-15
  • 2 "Vacuum Cleaner Brand-X" on 2013-07-16
  • 5 "Vacuum Cleaner Brand-X" on 2013-07-17
  • etc...

Oozel answer make sense! In order to work with my example, I just have to slightly modify his script like this:

foreach ($dates as $items){
    for ($i=1; $i<=$items; $i++){
        ... 
    }
}

@oozel: I have a number (and not an array of items) 'cause of the specific 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>

...
4

1 回答 1

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
*/
于 2013-07-19T17:59:29.433 回答