if user input :
$sgl = 2; $dbl = 0; $twn = 1; $trp = 0; $quad = 0; $price_room = 250000;
i want result looks picture below :
i try code :
<?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>
but not working. output results different results output in picture
how fix code results output results in images?
any appreciated
cheers
as alternative, use array hold together. first build array of rates:
$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',), );
sort of table wherein have price, labeling , stuff.
then after that, build array of input can apply array of rates , use key make calculations.
example:
// input $input = array('sgl' => $sgl, 'dbl' => $dbl, 'twn' => $twn, 'trp' => $trp, 'quad' => $quad); // lay them out array
now can match keys:
<?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 $key => $user_input) { if($user_input > 0) { // of data 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; } } ?>
as presentation, normal html markup , foreach
should fine:
<table border="1" cellpadding="10"> <thead><tr><th>no</th><th>room type</th><th>price</th></tr></thead> <?php foreach($result $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>
Comments
Post a Comment