使用pdo的总数组和

时间:2017-01-10 03:25:15

标签: php arrays pdo

我试图从购物车表中获得总价。其他表中的product_price是产品。我只得到最新的价格而不是总价。谢谢

//  function total_price (){

$total = 0;

global $db;
$ip = getIp();

    $sql = $db->query("SELECT * from cart WHERE ip_add='$ip'");

    $no=$sql->rowCount();   // number of rows affected by the last SQL statement  
    if ($no == 0){
        echo "";


    } else {
        foreach($sql as $row)

            $product_id = $row["p_id"];
            $sql = $db->query("SELECT product_price from product WHERE product_id='$product_id'");
            $no=$sql->rowCount();   // number of rows affected by the last SQL statement  
            if ($no == 0){
            echo "";
            }
            else
            {
            foreach($sql as $row)
            $product_price = array($row["product_price"]);

            $values = array_sum($product_price );
            $total += $values;

            }
    }

    echo "RM" . $total;

        }

1 个答案:

答案 0 :(得分:1)

如果我正确地阅读了这个结构,那么这个查询就应该是您所需要的:

select sum(product_price) from product 
inner join cart on product.product_id=cart.product_id