我用PHP知识为酒店制作剧本,用户(我)选择有多少人即将租房。
我所有的房间都是2人。根据有多少,我每人收取额外的价格/天。更多可以留下来,但那些"额外"必须根据年龄付费。
function calc()
{
$data['msg'] = "";
$data['from_date'] = $this->input->post('from_date');
$data['to_date'] = $this->input->post('to_date');
$data['category'] = $this->input->post('category');
$data['guestid'] = $this->input->post('guestid');
$data['idroom'] = $this->input->post('room');
$data['nguests'] = $this->input->post('nguests');
$data['nchilds'] = $this->input->post('nchilds');
$data['lchilds'] = $this->input->post('lchilds');
$data['greek'] = $this->input->post('greek');
$data['continental'] = $this->input->post('continental');
$data['halfboard'] = $this->input->post('halfboard');
$data['whitewine'] = $this->input->post('whitewine');
$data['redwine'] = $this->input->post('redwine');
$data['preferences'] = $this->input->post('preferences');
$data['mandiscount'] = $this->input->post('mandiscount');
$data['booking'] = $this->input->post('booking');
$data['airport'] = $this->input->post('airport');
$data['annotation'] = $this->input->post('annotation');
$data['is_manual'] = $this->input->post('is_manual');
if($data['is_manual'] == "manual")
{
$data['price_per_day'] = $this->input->post('price_per_day');
}
$room_avail = $this->kratiseis->getRoomPrice($data['idroom'], $data['from_date'], $data['to_date']);
$category_details = $this->admin->getSingleRoomCategory($data['category']);
$extras = $this->admin->getExtras();
$client = $this->guests->details($data['guestid']);
$data['room'] = $this->details->getSingleRoom($data['idroom']);
//$stay_days = date_diff($data['from_date'], $data['to_date']) - 1;
$from = new DateTime($this->input->post('from_date'));
$to = new DateTime($this->input->post('to_date'));
$stay_days = $from->diff($to);
$data['stay_for'] = $stay_days->format('%d');
$data['total_guests'] = $data['nguests'] + $data['nchilds'] + $data['lchilds'];
/*
* We only accept 2 persons in any room. If there are more than 2, we charge extra
*/
$extra_adults_price = 0;
$extra_childs_price = 0;
$extra_adults = 0;
$extra_childs = 0;
$we_have_extras = false;
if($data['total_guests'] > 2)
{
/*
* Find how many adults extra we have.
*/
if($data['nguests'] > 2)
{
$we_have_extras = true;
$extra_adults = $data['nguests'] - 2;
$extra_adults_price = ($extra_adults * $data['stay_for']) * 10;
}
/*
* Find how many extra children do we have.
*/
if( $data['nchilds'] > 2)
{
$we_have_extras = true;
$extra_childs = $data['nchilds'] - 2;
$extra_childs_price = ($extra_childs * $data['stay_for']) * 5;
}
}
这是我从中获取数据的视图:
<div class="column-container">
<div class="column column-1" style="background-color: #097ea1; color: #fff;">Adults:</div>
<div class="column column-2" style="background-color: #cee8ef">
<select name="nguests">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div class="column column-3" style="background-color: #097ea1; color: #fff;">Children 0-5:</div>
<div class="column column-4" style="background-color: #cee8ef">
<select name="nchilds">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div class="column column-5" style="background-color: #097ea1; color: #fff;">Children 6-12:</div>
<div class="column column-6" style="background-color: #cee8ef">
<select name="lchilds">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div class="column column-7"></div>
<div class="column column-8"></div>
</div>
我在控制器中也有一些代码。
现在,假设我们预订了3名成人+ 1名儿童=共4人。额外费用是10美元,但应该是15美元(成人10美元,儿童5美元)
如何区分额外人员的价值观,以便获得理想的结果?
答案 0 :(得分:1)
尝试使用此代码,我认为问题出在你的逻辑中
Function CountExtras($data) {
if($data['total_guests'] <= 2) return 0;
$extra_adults_price = 0;
$extra_childs_price = 0;
$extra_adults = 0;
$extra_childs = 0;
$data['nguests'] = $data['total_guests'] - $data['nchilds'];
If( $data['nguests'] >= 2 ) $data['nguests'] = $data['nguests'] - 2;
else {
$data['nguests'] = 0;
--$data['nchilds'];
}
return $data['nguests']*10 + $data['nchilds']*5;
}
echo CountExtras(array ( 'total_guests' => 1, 'nchilds' => 0 )).'<br>'; // 0
echo CountExtras(array ( 'total_guests' => 2, 'nchilds' => 0 )).'<br>'; // 0
echo CountExtras(array ( 'total_guests' => 3, 'nchilds' => 1 )).'<br>'; // 5
echo CountExtras(array ( 'total_guests' => 3, 'nchilds' => 0 )).'<br>'; // 10
echo CountExtras(array ( 'total_guests' => 4, 'nchilds' => 1 )).'<br>'; // 15
答案 1 :(得分:0)
确定。前两个人的房价基本相同。我想你在这里被解雇了。您已经收集了严格数量的额外房间用户。 我不确定为什么不使用基本代数来计算它,因为您在每个表单输入字段中收集特定数据。即如果有人设置2个额外的成员,则需要按以下方式计算:
$price = $this->roomPrice;//some basic value for two persons
$price += ( (int)$this->input->post('extraAdults') > 0 ) ? (int)$this->input->post('extraAdults') * 10 : 0;
$price += ( (int)$this->input->post('extraChild') > 0 ) ? (int)$this->input->post('extraChild') * 5 : 0;
//var_dump($price);