从数据库中添加2号

时间:2019-01-15 03:25:04

标签: php mysqli

我需要我的代码帮助。
这是我的sql代码

session_start();
$User = $_SESSION['username'];
include './auth.php';
$result = mysqli_query($conn,"Select * from booking where 
booking_id=".$_SESSION['booking']."");
if( mysqli_num_rows($result) >0){
    while ($rows = mysqli_fetch_array($result)){
    $q = mysqli_query($conn,"SELECT roombook.totalroombook AS total, room.room_name AS name, roombook.total_adult AS adult, roombook.total_children AS children, roombook.rate AS rate, roombook.chrate AS chrate, roombook.sst AS sst, roombook.infant AS infa
                                                            FROM roombook
                                                            LEFT JOIN room ON roombook.room_id = room.room_id
                                                            WHERE roombook.booking_id =".$rows['booking_id'].";");
                                                            echo 
mysqli_error($conn);

这是我的原始代码。

$s=1;
echo"<table>";
echo "                <tr><td><b>Quantity</b></td><td><b> Room Type</b><br> </td><td style=\"width:200px;\"><b>Number Pax </b></td><td><b>Total (RM)</b></td><td><b> sst (6%)(rm)</b></td><td><b> Total After sst(6%)</b></td></tr>";
while($r = mysqli_fetch_array($q)){

echo "                <tr><td>".$r['total']."</td><td><b> ".$r['name']."</b><br> </td><td>".$r['adult']." adult </td><td><b>RM".$r['adult']*$r['rate']."</b></td> <td><b>RM".$r['sst']*6/100 ."</b></td>";
if(isset($r["children"])){
echo "                <tr><td></td><td><b> </b><br> </td><td>".$r['children']."child </td><td><b>RM".$r['children']*$r['chrate']."</b></td><td><b>RM".$r['sst']*6/100 ."</b></td>";
}
if(isset($r["infa"])){
echo "                <tr><td ></td><td><b> </b><br> </td><td>".$r['infa']."Infant </td><td><b>RM".$r['infa']*90 ."</b></td><td><b>RM".$r['sst']*6/100 ."</b></td>";
}


}
echo"                                                       <tr><td style=\"border:none;\"></td><td style=\"border:none;\"></td><td style=\"border:none;\"></td><td style=\"border:none;\"></td>";
echo"                                                           <td style=\"width:100px;\">Total</td>";
echo"                                                           <td  style=\"width:100px;\"> <b>RM".$rows['total_amount']."</b></td>";
echo"                                                       </tr>";
echo"                                                       <tr><td style=\"border:none;\"></td><td style=\"border:none;\"></td><td style=\"border:none;\"><td style=\"border:none;\"></td>";
echo"                                                           <td style=\"width:100px;\">40% Deposit Due</td>";
echo"                                                           <td  style=\"width:100px;\"><b>RM".$rows['deposit']."</b></td>";
echo"                                                       </tr>";

echo"                                                   </table>";

代码将产生这个 origanal

从这段代码中,我想计算total RMsst列总数下的每一行数字。显示在列total after sst下。 (参考图片假定黑色区域是我想要的值)。

我已经尝试过此代码

$s=1;
echo"<table>";
echo "                <tr><td><b>Quantity</b></td><td><b> Room Type</b><br> </td><td style=\"width:200px;\"><b>Number Pax </b></td><td><b>Total (RM)</b></td><td><b> sst (6%)(rm)</b></td><td><b> Total After sst(6%)</b></td></tr>";
while($r = mysqli_fetch_array($q)){

echo "                <tr><td>".$r['total']."</td><td><b> ".$r['name']."</b><br> </td><td>".$r['adult']." adult </td><td><b>RM".$r['adult']*$r['rate']."</b></td> <td><b>RM".$r['sst']*6/100 ."</b></td> <td><b>RM" .((int)$r['adult'])*((int)$r['rate']) + ((int)$r['sst']) * 6/100 . "</b></td>";
if(isset($r["children"])){
echo "                <tr><td></td><td><b> </b><br> </td><td>".$r['children']."child </td><td><b>RM".$r['children']*$r['chrate']."</b></td><td><b>RM".$r['sst']*6/100 ."</b></td>";
}
if(isset($r["infa"])){
echo "                <tr><td ></td><td><b> </b><br> </td><td>".$r['infa']."Infant </td><td><b>RM".$r['infa']*90 ."</b></td><td><b>RM".$r['sst']*6/100 ."</b></td>";
}


}

尝试代码后,我得到了error picture.

我已经检查过(int)方法以从数据库中将值作为整数调用,但是从我尝试过的方法来看,它不起作用。

我希望获得如何在我的代码中实现(int)方法的帮助。

1 个答案:

答案 0 :(得分:0)

在将其与字符串连接之前,需要将所有计算放在括号内:

echo "                <tr><td>".$r['total']."</td><td><b> ".$r['name']."</b><br> </td><td>".$r['adult']." adult </td><td><b>RM".($r['adult']*$r['rate'])."</b></td> <td><b>RM".($r['sst']*6/100) ."</b></td> <td><b>RM" .( ((int)$r['adult'])*((int)$r['rate']) + ((int)$r['sst']) * 6/100) . "</b></td>";