繁体   English   中英

将多种产品添加到一个购物车中

[英]Adding multiple products into one shopping cart

嘿,我试图将不同产品页面中的产品添加到一个购物车中,例如,如果我从华硕页面中选择了产品,而从宏page页面中选择了另一个产品,它将在一个购物车中同时显示这两个产品,但是现在我看到一个错误

从华硕选择名称,其中serial = 1

字段列表中的“名称”列不明确

我为每种产品创建了不同的表格

CREATE TABLE asus ( serial int(11), name varchar(20), price float(), picture varchar(80) );

CREATE TABLE acer ( serial int(11), name varchar(20), price float(), picture varchar(80) );

CREATE TABLE lenovo ( serial int(11), name varchar(20), price float(), picture varchar(80) );

这是获取名称和价格的功能:

function get_product_name($pid){
    $result=mysql_query("select name from asus,acer,lenovo
    where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
    $row=mysql_fetch_array($result);
    return $row['name'];
}




 function get_price($pid){
    $result=mysql_query("select price from  asus,acer,lenovo where serial=$pid") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
    $row=mysql_fetch_array($result);
    return $row['price'];
}

这是addtocart的功能,产品存在并获得总计:

function get_order_total(){
    $max=count($_SESSION['cart']);
    $sum=0;
    for($i=0;$i<$max;$i++){
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=$_SESSION['cart'][$i]['qty'];
        $price=get_price($pid);
        $sum+=$price*$q;
    }
    return $sum;
}

function addtocart($pid,$q){
    if($pid<1 or $q<1) return;

    if(is_array($_SESSION['cart'])){
        if(product_exists($pid)) return;
        $max=count($_SESSION['cart']);
        $_SESSION['cart'][$max]['productid']=$pid;
        $_SESSION['cart'][$max]['qty']=$q;
    }
    else{
        $_SESSION['cart']=array();
        $_SESSION['cart'][0]['productid']=$pid;
        $_SESSION['cart'][0]['qty']=$q;
    }
}




function product_exists($pid){
    $pid=intval($pid);
    $max=count($_SESSION['cart']);
    $flag=0;
    for($i=0;$i<$max;$i++){
        if($pid==$_SESSION['cart'][$i]['productid']){
            $flag=1;
            break;
        }
    }
    return $flag;
}

这用于每个产品页面中的“添加到购物车”按钮:

 <button type="button" title="Add to Cart" class="button btn-cart"onclick="addtocart(<?php echo $row['serial']?>)" />

这用于购物车页面:

<?php
        if(is_array($_SESSION['cart'])){
        $max=count($_SESSION['cart']);
            for($i=0;$i<$max;$i++){
                $pid=$_SESSION['cart'][$i]['productid'];
                $q=$_SESSION['cart'][$i]['qty'];
                $pname=get_product_name($pid);
                $price=get_price($pid);

                if($q==0) continue;
        ?>
                <tr bgcolor="#FFFFFF"><td><?php echo $i+1?></td><td>
                <?php echo $pname?>

                </td>
                <td>RM 
                <?php echo $price?> 

请参阅此处的答案: 使用多个别名从2个表进行SQL查询

我建议将所有三个表组合到一个组合的Product表中,并带有一个Flag标识产品的类型。

如果没有,那么您需要使用表别名。

function get_product_name($pid){
    $result=mysql_query("select isnull(isnull(a.name, c.name), l.name) as name from asus a,acer c,lenovo l
    where(a.serial=$pid OR c.serial=$pid OR l.serial=$pid) ") or die("select name from products where serial=$pid"."<br/><br/>".mysql_error());
    $row=mysql_fetch_array($result);
    return $row['name'];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM