使用 MySQL 查询在每个div中总结了5种价格,并且效果很好。
然后,我希望通过给它们每个$_SESSION
变量之前得到这5个总值的总和,然后再将它们加起来。它只获得第一个价格,但仍能完美地总结它们。
(例如$extra_price
为25,如果我$extra_price+$extra_price
$total
可以获得50)
让我们说,如果(例如$extra_price
为25且$decoprice
为10,那么我总和$extra_price+$decoprice
$total
仅显示25)10已被忽略。
知道如何完美地获得总和吗?
以下是我的代码:
<?php
$extra_price = $_SESSION['extra_price'];
$decoprice = $_SESSION['decoprice'];
$foodprice = $_SESSION['foodprice'];
$drinksprice = $_SESSION['drinksprice'];
$venueprice = $_SESSION['venueprice'];
$total = $extra_price+$decoprice+$foodprice+$drinksprice+$venueprice;
?>
<center><b>Total <?php echo $total ?></b></center>
答案 0 :(得分:1)
你不应该使用这种语法
$_SESSION['decoprice'] = $show_pextra['SUM(decoprice)'];
尝试为您命名SUM(decoprice)
并使用它来注册会话。
select SUM(decoprice) as sum_decoprice from selectdeco where title = '$title'
$_SESSION['decoprice'] = $show_pextra['sum_decoprice '];
答案 1 :(得分:0)
你不需要所有这些额外的变量,你可以这样写:
$total = 0;
$keys = array('extra_price', 'decoprice', 'foodprice', 'drinksprice', 'venueprice');
foreach ($keys AS $key)
{
$total += $_SESSION[$key];
}
显然,这是无关紧要的,只是风格问题。在任何情况下,+
运算符都会为您完美地求和。这就是它的作用。它总结了一些事情。完美。
但问题是,没有人知道你的$ _SESSION中有什么。你呢?为什么不尝试将其记录在某处或至少做到:
print_r($_SESSION);
然后你会发现你的会话可能并不包含你的想法。
答案 2 :(得分:0)
替换
$_SESSION['extra_price'] = $show_pextra['SUM(price)'];
用
$total += $show_pextra['SUM(price)'];
完成所有会话。
以下是您的完整代码:
<?php $total = 0;?>
<tr><!--extra-->
<td style="width:120px;border:2px solid #000;"> Extra Item:</td>
<td style="border:2px solid #000;">
<?php
$read_allextra = mysql_query("select * from selectextra where title = '$title'");
while($show_allextra = mysql_fetch_array($read_allextra))
{
?>
<b>♦ <?php echo $show_allextra['extraitem'] ?></b></br>
<?php
}
?></td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pextra = mysql_query("select SUM(price) from selectextra where title = '$title'");
while($show_pextra = mysql_fetch_array($read_pextra))
{
?>
<center><b>RM <?php echo $show_pextra['SUM(price)'] ?></b></center>
<?php
$total += $show_pextra['SUM(price)'];
}
?>
</td>
</tr>
<tr><!--deco-->
<td style="width:120px;border:2px solid #000;"> Decoration :</td>
<td style="border:2px solid #000;">
<?php
$read_alldeco = mysql_query("select * from selectdeco where title = '$title'");
while($show_alldeco = mysql_fetch_array($read_alldeco))
{
?>
<b>♦ <?php echo $show_alldeco['decoitem'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pdeco = mysql_query("select SUM(decoprice) from selectdeco where title = '$title'");
while($show_pdeco = mysql_fetch_array($read_pdeco))
{
?>
<center><b>RM <?php echo $show_pdeco['SUM(decoprice)'] ?></b></center>
<?php
$total += $show_pextra['SUM(decoprice)'];
}
?>
</td>
</tr>
<tr><!--food-->
<td style="width:120px;border:2px solid #000;"> Foods :</td>
<td style="border:2px solid #000;">
<?php
$read_allfood = mysql_query("select * from selectfood where title = '$title'");
while($show_allfood = mysql_fetch_array($read_allfood))
{
?>
<b>♦ <?php echo $show_allfood['fooditem'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pfood = mysql_query("select SUM(foodprice) from selectfood where title = '$title'");
while($show_pfood = mysql_fetch_array($read_pfood))
{
?>
<center><b>RM <?php echo $show_pfood['SUM(foodprice)'] ?></b></center>
<?php
$total += $show_pextra['SUM(foodprice)'];
}
?>
</td>
</tr>
<tr><!--drinks-->
<td style="width:120px;border:2px solid #000;"> Drinks :</td>
<td style="border:2px solid #000;">
<?php
$read_alldrinks = mysql_query("select * from selectdrinks where title = '$title'");
while($show_alldrinks = mysql_fetch_array($read_alldrinks))
{
?>
<b>♦ <?php echo $show_alldrinks['drinksitem'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pdrinks = mysql_query("select SUM(drinksprice) from selectdrinks where title = '$title'");
while($show_pdrinks = mysql_fetch_array($read_pdrinks))
{
?>
<center><b>RM <?php echo $show_pdrinks['SUM(drinksprice)'] ?></b></center>
<?php
$total += $show_pextra['SUM(drinksprice)'];
}
?>
</td>
</tr>
<tr><!--venue-->
<td style="width:120px;border:2px solid #000;"> Venue :</td>
<td style="border:2px solid #000;">
<?php
$read_allvenue = mysql_query("select * from selectvenue where title = '$title'");
while($show_allvenue = mysql_fetch_array($read_allvenue))
{
?>
<b>♦ <?php echo $show_allvenue['venuename'] ?></b></br>
<?php
}
?>
</td>
<td style="width:120px;border:2px solid #000;">
<?php
$read_pvenue = mysql_query("select venueprice from selectvenue where title = '$title'");
while($show_pvenue = mysql_fetch_array($read_pvenue))
{
?>
<center><b>RM <?php echo $show_pvenue['venueprice'] ?></b></center>
<?php
$total += $show_pextra['SUM(venueprice)'];
}
?>
</td>
</tr>
<tr>
<td style="border:2px solid #000;" colspan="2">
<center><input type="submit" class="button" style="width:300px;" value="Pay Now" name="send_payment"/></center></td>
<td style="width:120px;border:2px solid #000;">
<center><b>Total <?php echo $total ?></b></center>
</td>
</tr>