如果用户输入:
$sgl = 2;
$dbl = 0;
$twn = 1;
$trp = 0;
$quad = 0;
$price_room = 250000;
我希望结果如下图所示:
我尝试这样的代码:
<?php
$sgl = 2;
$dbl = 0;
$twn = 1;
$trp = 0;
$quad = 0;
$price_room = 250000;
$counter = 0;
if($sgl>0){
$counter++;
$room_type = 'Single';
$price = $price_room * 1;
}
if($dbl>0){
$counter++;
$room_type = 'Double';
$price = $price_room * 2;
}
if($twn>0){
$counter++;
$room_type = 'Twin';
$price = $price_room * 2;
}
if($trp>0){
$counter++;
$room_type = 'Triple';
$price = $price_room * 3;
}
if($quad>0){
$counter++;
$room_type = 'Quad';
$price = $price_room * 4;
}
// echo $counter;
?>
<table border="1">
<tr>
<td>No</td>
<td>Room type</td>
<td>Price</td>
</tr>
<?php
$no=1;
for($i=0;$i<$counter;$i++){
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $room_type; ?></td>
<td><?php echo $price; ?></td>
</tr>
<?php
$no++;
}
?>
<tr>
<td colspan="3" style="text-align: right;"><?php echo $price; ?></td>
</tr>
</table>
但它不起作用。输出结果与图片中的结果输出不同
如何修复我的代码,以便将结果作为输出产生图像?
任何帮助非常感谢
干杯
答案 0 :(得分:3)
作为替代方案,只需使用数组将它们组合在一起即可。首先建立一系列费率:
$rates = array(
'sgl' => array('rate' => 1, 'type' => 'Single',),
'dbl' => array('rate' => 2, 'type' => 'Double',),
'twn' => array('rate' => 2, 'type' => 'Twin',),
'trp' => array('rate' => 3, 'type' => 'Triple',),
'quad' => array('rate' => 4, 'type' => 'Quad',),
);
有点像桌子,你有价格,标签和东西。
然后,构建一个输入数组,以便将其应用到您的数据库中并使用该键进行计算。
示例:
// input
$input = array('sgl' => $sgl, 'dbl' => $dbl, 'twn' => $twn, 'trp' => $trp, 'quad' => $quad);
// lay them all out as an array
现在你可以匹配键:
<?php
$sgl = 2;
$dbl = 0;
$twn = 1;
$trp = 0;
$quad = 0;
// input
$input = array('sgl' => $sgl, 'dbl' => $dbl, 'twn' => $twn, 'trp' => $trp, 'quad' => $quad);
$price_room = 250000;
$rates = array(
'sgl' => array('rate' => 1, 'type' => 'Single',),
'dbl' => array('rate' => 2, 'type' => 'Double',),
'twn' => array('rate' => 2, 'type' => 'Twin',),
'trp' => array('rate' => 3, 'type' => 'Triple',),
'quad' => array('rate' => 4, 'type' => 'Quad',),
);
$total = 0;
foreach($input as $key => $user_input) {
if($user_input > 0) {
// all of the data are pushed in here
$result[] = array(
'no' => $user_input,
'room_type' => $rates[$key]['type'],
'price' => $rates[$key]['rate'] * $price_room,
);
$total += $rates[$key]['rate'] * $price_room;
}
}
?>
至于演示文稿,普通的html标记和foreach
应该可以正常使用:
<table border="1" cellpadding="10">
<thead><tr><th>No</th><th>Room Type</th><th>Price</th></tr></thead>
<?php foreach($result as $r): ?>
<tr>
<td><?php echo $r['no']; ?></td>
<td><?php echo $r['room_type']; ?></td>
<td><?php echo $r['price']; ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="3" align="right"><?php echo $total; ?></td>
</tr>
</table>
答案 1 :(得分:1)
请使用此代码,它会以您期望的方式输出。
<?php
$sgl = 2;
$dbl = 0;
$twn = 1;
$trp = 0;
$quad = 0;
$price_room = 250000;
$rooms = array();
$counter = 0;
if($sgl>0){
$counter++;
$room_type = 'Single';
$price = $price_room * 1;
$rooms[$room_type] = $price;
}
if($dbl>0){
$counter++;
$room_type = 'Double';
$price = $price_room * 2;
$rooms[$room_type] = $price;
}
if($twn>0){
$counter++;
$room_type = 'Twin';
$price = $price_room * 2;
$rooms[$room_type] = $price;
}
if($trp>0){
$counter++;
$room_type = 'Triple';
$price = $price_room * 3;
$rooms[$room_type] = $price;
}
if($quad>0){
$counter++;
$room_type = 'Quad';
$price = $price_room * 4;
$rooms[$room_type] = $price;
}
// echo $counter;
?>
<table border="1">
<tr>
<td>No</td>
<td>Room type</td>
<td>Price</td>
</tr>
<?php
$no=1;$total_price =0;
foreach($rooms as $toom_type=>$room_price){
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $toom_type; ?></td>
<td><?php echo $room_price; ?></td>
</tr>
<?php
$total_price += $room_price;
$no++;
}
?>
<tr>
<td colspan="3" style="text-align: right;"><?php echo $total_price ; ?></td>
</tr>
</table>
如果需要任何其他帮助,请告诉我。
由于 阿米特
答案 2 :(得分:1)
您可以使用compact
将变量添加到数组中(尽管您可以通过其他方式执行此操作)。
循环遍历它们,并使用switch
设置值和输出。
$sgl = 2;
$dbl = 0;
$twn = 1;
$trp = 0;
$quad = 0;
$price_room = 25000;
// compact the variables into an array
$rooms = compact('sgl', 'dbl', 'twn', 'trp', 'quad');
echo '<table border="1">
<tr>
<td>No</td>
<td>Room type</td>
<td>Price</td>
</tr>';
// set start values
$no = 1;
$total = 0;
// loop through array
foreach ($rooms as $type => $rm)
{
// only output if greater than 0
if ($rm > 0)
{
// set the values based on the type
switch ($type) {
case 'sgl':
$room_type = 'Single';
$price = $price_room * 1;
break;
case 'dbl':
$room_type = 'Double';
$price = $price_room * 2;
break;
case 'twn':
$room_type = 'Twin';
$price = $price_room * 2;
break;
case 'trp':
$room_type = 'Triple';
$price = $price_room * 3;
break;
case 'quad':
$room_type = 'Quad';
$price = $price_room * 4;
break;
}
// output the row
echo '<tr>
<td>'.$no.'</td>
<td>'.$room_type.'</td>
<td>'.($rm * $price).'</td>
</tr>';
// add to total
$total += $rm * $price;
// increment number
$no++;
}
}
// output the total and close the table
echo '<tr>
<td colspan="3" style="text-align: right;">'.$total.'</td>
</tr>
</table>';