2014-05-20 32 views
0

有5種類型的價格已被彙總在每個股利用MySQL查詢和完美的作品。PHP - 獲取第一個總和號碼

然後,我希望通過給他們每個$_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

也穿上你如何存儲這些會話變量中的值的代碼。 –

+0

你可能想檢查你是否有實際的會話變量:'var_dump($ _ SESSION);' – Cully

+0

如果你回顯所有的變量,你可以從會話中獲得所有你想要的數據?如果沒有,你應該檢查'$ _SESSION'的註冊表 –

回答

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 ']; 
+0

總還是一樣...沒有總結所有數字 – user3652484

0

你並不需要所有這些額外的變量,你可以寫這樣的:

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

顯然,這是無關緊要的,只是風格的問題。無論如何,+運營商會爲你完美地總結一些事情。這就是它的作用。它總結了一些事情。完美。

但問題是,沒有人知道你的$ _SESSION中有什麼。你做?你爲什麼不嘗試登錄的地方,或至少做:

print_r($_SESSION); 

然後你會看到你的會話可能不包含什麼,你認爲它。

+0

我已經嘗試過你的解決方案,但輸出仍然相同 – user3652484

+0

'array('extra_price','decoprice','foodprice' ,'飲料價格','場地價格');'直接從其他地方得到?不需要申報? – user3652484

+0

我仍然不知道$ _SESSION中有什麼。你能打印它並顯示結果嗎? – Okonomiyaki3000

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;">&nbsp;&nbsp;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>&diams; <?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;">&nbsp;&nbsp;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>&diams; <?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;">&nbsp;&nbsp;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>&diams; <?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;">&nbsp;&nbsp;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>&diams; <?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;">&nbsp;&nbsp; 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>&diams; <?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> 
+0

我試試你的代碼總的輸出還是一樣的25,其餘沒有總結 – user3652484