Php 从基本购物车脚本计算小计和总计

Php 从基本购物车脚本计算小计和总计,php,Php,我试图从mysql数据库返回的数组中存储的一系列值计算小计和总计 这就是我所拥有的thusfar while($row = mysql_fetch_array($cart)){ foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } $id = $row['id']; $contents = unserialize($row['contents']);

我试图从mysql数据库返回的数组中存储的一系列值计算小计和总计

这就是我所拥有的thusfar

    while($row = mysql_fetch_array($cart)){ 
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    $contents = unserialize($row['contents']);
        foreach( $contents as $key => $value){
            if($key == "price"){$subtotal = $subtotal+$value;}
            echo "$key : $value <br />";
        }
    echo "<br><br><br>";
    } 
echo "<font color=red>SubTotal $subtotal</font>";
while($row=mysql\u fetch\u array($cart)){
foreach($key=>$value的行){$row[$key]=stripslashes($value);}
$id=$row['id'];
$contents=unserialize($row['contents']);
foreach($key=>$value形式的内容){
如果($key==“price”){$subtotal=$subtotal+$value;}
回显“$key:$value
”; } 回声“


”; } 回显“小计$小计”;
$contents包含一个数组
[name]=>super[price]=>65.87[quantity]=>25

因此,我需要将价格乘以数量,然后取小计(每项)并在循环后将其相加

foreach( $contents as $key => $value)
{
   if($key == "price") $total = $total+$value;

   if($key == "name")
   { 
       if(!isset($subtotal[$key])) $subtotal[$key] = 0;
       $subtotal[$key] = $subtotal[$key] + $value;
   }
}
然后,在$total中有总价,在$subtotal数组中有每个单独项目的总价


那么你有$total中的总价和$subtotal数组中的每个单独项目的总价

我不确定你的total和subtotal是什么意思。我假设小计是一个项目的价格乘以他的数量

对于总计,我假设您打算用红色打印小计

您的代码变成:

$total=0;
while($row = mysql_fetch_array($cart)){ 
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    $contents = unserialize($row['contents']);
    $contents['subtotal'] = $contents['price']*$contents['quantity'];
    foreach($contents as $key => $value){echo "$key : $value <br />"; }
    $total +=$content['subtotal'];
} 
echo "<font color=red>Total: $total</font>";
$total=0;
而($row=mysql\u fetch\u数组($cart)){
foreach($key=>$value的行){$row[$key]=stripslashes($value);}
$id=$row['id'];
foreach($key=>$value的行){$row[$key]=stripslashes($value);}
$id=$row['id'];
$contents=unserialize($row['contents']);
$contents['subtotal']=$contents['price']*$contents['quantity'];
foreach($key=>$value的内容){echo“$key:$value
”;} $total+=$content['subtotal']; } echo“总计:$Total”;
如果格式不是强制性的,我会使用稍微不同的解决方案 用于格式化输出。(您应该检查

$billLine=“%s(%0.2f x%d):%0.2f



”; $total=0; 而($row=mysql\u fetch\u数组($cart)){ foreach($key=>$value的行){$row[$key]=stripslashes($value);} $id=$row['id']; foreach($key=>$value的行){$row[$key]=stripslashes($value);} $id=$row['id']; $contents=unserialize($row['contents']); $subtotal=$contents['price']*$contents['quantity']; printf($billLine,$contents['name'], $contents[‘数量’], $contents[“价格”], 美元小计); $total+=$subtotal; } echo“总计:$Total”;
我不知道你说的总计和小计是什么意思。我假设小计是一个项目的价格乘以他的数量

对于总计,我假设您打算用红色打印小计

您的代码变成:

$total=0;
while($row = mysql_fetch_array($cart)){ 
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    $contents = unserialize($row['contents']);
    $contents['subtotal'] = $contents['price']*$contents['quantity'];
    foreach($contents as $key => $value){echo "$key : $value <br />"; }
    $total +=$content['subtotal'];
} 
echo "<font color=red>Total: $total</font>";
$total=0;
而($row=mysql\u fetch\u数组($cart)){
foreach($key=>$value的行){$row[$key]=stripslashes($value);}
$id=$row['id'];
foreach($key=>$value的行){$row[$key]=stripslashes($value);}
$id=$row['id'];
$contents=unserialize($row['contents']);
$contents['subtotal']=$contents['price']*$contents['quantity'];
foreach($key=>$value的内容){echo“$key:$value
”;} $total+=$content['subtotal']; } echo“总计:$Total”;
如果格式不是强制性的,我会使用稍微不同的解决方案 用于格式化输出。(您应该检查

$billLine=“%s(%0.2f x%d):%0.2f



”; $total=0; 而($row=mysql\u fetch\u数组($cart)){ foreach($key=>$value的行){$row[$key]=stripslashes($value);} $id=$row['id']; foreach($key=>$value的行){$row[$key]=stripslashes($value);} $id=$row['id']; $contents=unserialize($row['contents']); $subtotal=$contents['price']*$contents['quantity']; printf($billLine,$contents['name'], $contents[‘数量’], $contents[“价格”], 美元小计); $total+=$subtotal; } echo“总计:$Total”;
您正在自行提供计算总数的步骤;)只要将这些步骤放在一个过程或函数中,您就完成了,您当然应该跟踪小计:)但这很容易做到,方法是在循环外设置一个变量(例如$carttotal),并将每行小计(数量*价格)添加到$carttotal中。您自行提供用于计算总数的步骤;)只要将这些步骤放在一个过程或函数中,您就完成了,您当然应该跟踪小计:),但这很容易做到,方法是在循环外设置一个var(例如$cartotal),并将每行小计(数量*价格)添加到$cartotal.foreach($key=>$value的小计){$count[$key]=count($subtotal[$key]);$totalCount=$totalCount+$count[$key];}然后您将拥有一个数组,其中每个项目计数和总计数为$totalCountforeach($key=>$value){$count[$key]=count($subtotal[$key]);$totalCount=$totalCount+$count[$key];}然后您将拥有一个数组,其中每个项目计数和总计数为$totalCount