簡體   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