mysql - PHP - getting the first sum number only

374

There are 5 types of prices that have been summed-up in each div using MySQL query and works perfectly.

Then, I wish to get the sum of those 5 total values by giving them each a$_SESSION variable before it sum them all up. It only gets the first price but still sums them up perfectly.

(e.g$extra_price is 25 then if i$extra_price+$extra_price the$total can get 50)

Lets say if (e.g$extra_price is 25 and$decoprice is 10 then i sum$extra_price+$decoprice the$total only show 25) the 10 has been ignore.

Any idea how to get the sum perfectly ?

Below is my code:

<?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>
228

Answer

Solution:

U shouldnt use this syntax

$_SESSION['decoprice'] = $show_pextra['SUM(decoprice)'];

Try to name youSUM(decoprice) and use it to register the session.

select SUM(decoprice) as sum_decoprice from selectdeco where title = '$title'
$_SESSION['decoprice'] = $show_pextra['sum_decoprice '];
836

Answer

Solution:

You don't need all those extra variables, you can just write it like this:

$total = 0;
$keys = array('extra_price', 'decoprice', 'foodprice', 'drinksprice', 'venueprice');
foreach ($keys AS $key)
{
    $total += $_SESSION[$key];
}

Obviously, this is irrelevant, just an issue of style. In any case the+ operator will sum things perfectly for you. That's what it does. It sums things. Perfectly.

But the problem is, nobody knows what's in your $_SESSION. Do you? Why don't you try logging it somewhere or at least doing:

print_r($_SESSION);

Then you will see that your session probably doesn't contain what you think it does.

57

Answer

Solution:

Hey first of all you don't need session here. Simple declare total variable at top of your code and replace each session with total variable with plus. and remove extra variables.

Replace

$_SESSION['extra_price'] = $show_pextra['SUM(price)'];

With

$total += $show_pextra['SUM(price)'];

Do with all session.

Here is your full code:

<?php $total = 0;?>
<tr><!--extra-->
  <td >&nbsp;&nbsp;Extra Item:</td>
  <td >
  <?php
    $read_allextra = mysql_query("select * from selectextra where title = '$title'");
    while($show_allextra = mysql_fetch_array($read_allextra))
    {
      ?>
        <b>&diams; <?php echo $show_allextra['extraitem'] ?></b></br>
      <?php
    }

  ?></td>
  <td >
  <?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 >&nbsp;&nbsp;Decoration :</td>
  <td >
  <?php
    $read_alldeco = mysql_query("select * from selectdeco where title = '$title'");
    while($show_alldeco = mysql_fetch_array($read_alldeco))
    {
      ?>
        <b>&diams; <?php echo $show_alldeco['decoitem'] ?></b></br>
      <?php
    }

  ?>

  </td>
  <td >
    <?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 >&nbsp;&nbsp;Foods :</td>
  <td >
  <?php
    $read_allfood = mysql_query("select * from selectfood where title = '$title'");
    while($show_allfood = mysql_fetch_array($read_allfood))
    {
      ?>
        <b>&diams; <?php echo $show_allfood['fooditem'] ?></b></br>
      <?php
    }

  ?>
  </td>
  <td >
  <?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 >&nbsp;&nbsp;Drinks :</td>
  <td >
  <?php
    $read_alldrinks = mysql_query("select * from selectdrinks where title = '$title'");
    while($show_alldrinks = mysql_fetch_array($read_alldrinks))
    {
      ?>
        <b>&diams; <?php echo $show_alldrinks['drinksitem'] ?></b></br>
      <?php
    }

  ?>
  </td>
  <td >
  <?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 >&nbsp;&nbsp; Venue :</td>
  <td >
  <?php
    $read_allvenue = mysql_query("select * from selectvenue where title = '$title'");
    while($show_allvenue = mysql_fetch_array($read_allvenue))
    {
      ?>
        <b>&diams; <?php echo $show_allvenue['venuename'] ?></b></br>
      <?php
    }

  ?>
  </td>
  <td >
  <?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 colspan="2">
  <center><input type="submit"  value="Pay Now" name="send_payment"/></center></td>
  <td >
    <center><b>Total <?php echo $total ?></b></center>
  </td>
</tr>

People are also looking for solutions to the problem: How to input values into database and collect the new id to store in php/html?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.