dtddjq9637 2014-04-19 17:53
浏览 32

计算数组php的总和

Hi im currently doing making a website that sells games as a project but im having problems calculating the sum for the price of the games

I have this loop which displays the games added into the basket

cart.php

<?php
    $count = 0;
    while ($count < $numrow)
    {
    $row = $results -> fetch_assoc();
    extract($row);
    echo"<div>";
    echo"<div class='recommended_games'>";
    echo "<img src='images/".$gameIMG."' />";
    echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag" name="price" method="POST">£'.$gamePrice. '</div>';
echo'</div>';

echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<select name="quantity" id="quantity" />';
echo      '<option value="1">1</option>';
echo      '<option value="2">2</option>';
echo      '<option value="3">3</option>';
echo      '<option value="4">4</option>';
echo      '<option value="5">5</option>';
echo      '</select>';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '<div class="quantity_update">';
echo '<form action="remove_item.php" method="POST">';
echo     '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo     '<input type="submit" value="Remove Item"  />';
echo '</form>';
echo '</div>';
echo '</div>';             



 echo"<img class='box1' src='Images/Grey-Banners.png' />";
 echo"</div>";
 $count = $count + 1;   

 }     
 echo '<div id="delete_all">';
 echo '<form action="delete_cart.php" method="POST">';
 echo    '<input id="hide_button" type="submit" value="Clear All"  />';
 $a=array($gamePrice);
 echo array_sum($a);
 echo '</form>';
 echo '</div>';
 ?>

this is where im trying to calculate the total price

$a=array($gamePrice);
echo array_sum($a);
  • 写回答

1条回答 默认 最新

  • dqgo99177 2014-04-19 18:01
    关注

    The reason this doesnt work, is because $gamePrice never is an array (unless you didnt provide all code). In the loop, it gets set to a new value, after the loop only the last one is stored.

    Based on some hints in your code, I guessing this is a cart and you're looping through the cart. An easy way to get a total is like this:

    $total = 0;
    while( $itemsThatWeLoop){
        // Some code here
        $total+= $gamePrice*$quantity;
    }
    

    You add a variable which increments with the product's price


    To explain the while-only-last-value-saved:

    $i=0;
    while( $i<=10){
        $i= $i+1;
    }
    echo $i; 
    

    Will give 10. All other iterations $i gets set to a new value. The original value is not saved

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题