2010-02-16 34 views
0
<?php 

switch($_GET["action"]) 
{ 
case "add_item": 
{ 
AddItem($_GET["idc"], $_GET["qty"]); 
ShowCart(); 
break; 
} 
case "update_item": 
{ 
UpdateItem($_GET["idc"], $_GET["qty"]); 
ShowCart(); 
break; 
} 
case "remove_item": 
{ 
RemoveItem($_GET["idc"], $_GET["id"]); 
ShowCart(); 
break; 
} 
default: 
{ 
ShowCart(); 
} 
} 
function AddItem($itemId, $qty){ 


$result = mysql_query("SELECT COUNT(*) FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); 

$row = mysql_fetch_row($result); 
$numRows = $row[0]; 

if($numRows == 0) 
{ 
// This item doesn't exist in the users cart, 
// we will add it with an insert query 

mysql_query("INSERT INTO cart(cookieId, id, qty) values('" . GetCartId() . "', $itemId, $qty)"); 
//printf ("Inserted records: %d\n", mysql_affected_rows()); 

} 
else 
{ 
// This item already exists in the users cart, 
// we will update it instead 
mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); 

} 

} 

function UpdateItem($itemId, $qty) 
{ 

mysql_query("UPDATE cart SET qty = $qty WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); 
//printf ("Updated records: %d\n", mysql_affected_rows()); 

} 

function RemoveItem($itemId) 
{ 

mysql_query("DELETE FROM cart WHERE cookieId = '" . GetCartId() . "' AND id = $itemId"); 
} 

?> 

<?php 
function ShowCart() 

{ 
    $result = mysql_query("SELECT 
        cart.id   cart_id, 
        cart.id   cart_id, 
        cart.cartId  cartId, 
        cart.cookieId cookie_Id, 
        cart.qty  qt_y,      
        cdkb.id   cdkb_id, 
        cdkb.name  name, 
        cdkb.image  image, 
        cdkb.price  price, 
        dkb.id   dkb_id, 
        dkb.name  name1, 
        dkb.image  image1, 
        dkb.price  price2, 
        dbl.product_id product_id, 
       dbl.price  price3, 
       dbl.variety  variety, 
       dbl.description description     
FROM 
     cart 


     LEFT OUTER JOIN cdkb 
      ON cart.id = cdkb.id  

     LEFT OUTER JOIN dkb 
      ON cart.id = dkb.id 

     LEFT OUTER JOIN dbl 
      ON dbl.id = dkb.id 


    WHERE 
     cart.cookieId ='" . GetCartId() . "' ' ORDER BY cdkb.name AND dkb.name ASC"); 

    <div id="cart"> 
<div id="group"> 
<div id="quantity">Qty</div> 
<div id="cartpic">Pic</div> 
<div id="product">Product</div> 
<div id="cartprice">Price</div> 
<div id="remove">Remove</div> 
</div> 
<?php 

$totalCost=0; 
while($row = mysql_fetch_array($result)) 
{ 
// Increment the total cost of all items 
$totalCost += ($row["qt_y"] * $row["price1"]); 

?> 

<div id="cart1"> 
<select name="<?php echo $row["ckb_id"];?>" onChange="UpdateQty(this)"> 
<?php print($row["ckb_id"]);?> 
<?php 

for($i = 1; $i <= 30; $i++) 
{ 
echo "<option "; 
if($row["qt_y"] == $i) 
{ 
echo " SELECTED "; 
} 
echo ">" . $i . "</option>"; 
} 
?> 
</select> 
</div> 
<div id="cart2"> 
<img src="images/logopic.gif"<?php /*?><?php echo $row["image1"]; ?><?php */?> alt="we" width="60" height="50" /> 
</div> 
<div id="cart3"><p><?php echo $row["dishname1"]; ?></p></div> 



<div id="cart4"><p> 
$<?php echo number_format($row["price3"], 2, ".", ","); ?></p></div> 

<div id="cart5"> 
<p><?php 
printf('<a href="cart.php?action=remove_item&id=%d&idc=%d&register=%s">Remove</a>', $_GET['id'], $row['ckb_id'], $_GET['register']); 
?></p></div> 

<hr size="1" color="red" > 

<script language="JavaScript"> 

function UpdateQty(item) 
{ 
itemId = item.name; 
newQty = item.options[item.selectedIndex].text; 

document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; 
} 

</script> 





<?php 
} 
?> 

<font face="verdana" size="2" color="black" style="clear:right;"> 
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b></font></td> 


<?php 
} 
?> 

以上是cart.php的更新代碼和查詢。如果您注意到查詢下方有一個while循環。現在,while循環完全適用於來自page1.php的dkb表格。這個循環的設置方式是它會重複保存,顯示和管理物品的數量,圖像,名稱,價格和刪除選項,顯示它們,並重複用戶選擇的項目。那麼,設置和設計的字段並不完全適用於來自page2.php的項目,其中顯示在購物車中的信息從表dkb和dbl中拉取。現在的問題是,當從page2.php到購物車時,相同的項目名稱重複三次,因爲在dbl表中,設計如下。幫助語法並向查詢添加表格和條件

INSERT INTO `dbl` (`id`, `price`, `variety`, `description`) VALUES 
(1, 20.30, 'Small Tray', 'Serves 6 to 8 people'), 
(1, 25.90, 'Medium Tray', 'Serves 12 to 15 people'), 
(1, 30.90, 'Large Tray', 'Serves 18 to 21 people'), 
(136, 0.00, 'Small Tray', '', 'small'), 
(136, 0.00, 'Medium Tray', ''), 
(136, 0.00, 'Large Tray', ''), 
(2, 0.00, 'Small Tray', ''), 
(2, 0.00, 'Medium Tray', ''), 
(2, 0.00, 'Large Tray', ''); 

上述假設是三個項目DKB的看到它加入對dkb.id查詢dbl.id = dkb.id的dbl.id插入井dbl.id有三個價格在DKB一個項目在這種情況下,項目1的價格是三個,項目136的價格是三個,項目2的價格是三個。所以它是三種不同的品種,以及dkb中每個項目的描述。從現在開始,如果您查看查詢下的while循環中的字段assign,其中一些是來自cdkb和dbl的其他dkb字段。 Rigth現在正如我所說的那樣,在while循環中的設置之上,從dkb表中獲得的信息是可以的,這些信息只有一個價格和一個名稱。一些我在想的是安排一個if語句條件來檢測信息來自page1.php哪個是名稱和價格,或者來自page2.php這將是一個名稱,從一個到三個價格和品種取決於用戶選擇的小,中或大托盤。每個托盤都有一個價格,但它將是一個名稱1,136或2,這些是來自dkb表的id。在while循環中的if語句中,顯示來自page2.php的信息,這些信息需要一到三個價格和種類,它們會有一個foreach循環,或者for循環不知道要計算用戶在page2中選擇的種類。 PHP將其顯示在購物車中的while循環內,並通過if語句的幫助來檢測從page1.php或page2.php中選擇的天氣。

$result = mysql_query("SELECT * FROM .... LEFT OUTER JOIN .... WHERE") // This is the query we have discussed in this forum. 
while($row = mysql_fetch_array($result)) 
{ if($row['cdkb_id']){ 
echo "<div>qty</div>"; 
echo"<div>". $row['image'] . "</div>"; 
echo"<div>". $row['name'] . "</div>"; 
echo"<div>". $row['price'] . "</div>"; 
echo"<div>remove</div>"; 
} 
else{ 
echo "<div>qty</div>"; 
echo"<div>". $row['image1'] . "</div>"; 
echo"<div>". $row['name1'] . "</div>"; 
foreach ($row['dkb_id'] as $variety) { 
echo"<div>".$variety['variety']. $variety['price3'] . "</div>"; 
// the vriety and price3 field will display three times as in table dbl has three prices and three variety. It is correct to use a foreach loop for in this case?} 
echo"<div>remove</div>"; 
// end of foreach loop inside the else statement 
}// end of if else statement 
}// end of while loop 
?> 

因此,最終的顯示和插圖被搜索和作爲最終產品呈現如下,這是所需的外觀。

while($row = mysql_fetch_array($result)) 
{ 
if (table cdkb) { 
    [1]qyt   image  name   price  remove 
    1   ---  marina  $18.90  remove?  
    }end of if statement 
Else table dkb { 
    1]qyt   Image   Name    Price     Remove 
    1    ---   marina   Small Tray $18.90  remove? 
               Medium Tray $30.24 
               Large Tray $35.90 
        } // end of else statement 

        }//end of while loop 

給我一個最後的手。

回答

0

如果你有所有的選擇單獨,然後表。*這可能是你的問題。

SELECT * 
FROM 
cart 

LEFT OUTER JOIN dkb 
    ON cart.id = dkb.id 

LEFT OUTER JOIN cdkb 
    on cart.id = cdkb.id 

LEFT OUTER JOIN double 
    on cart.id = double.id 
WHERE 
cart.cookieId = GetCartId() AND cdkb.id IS NOT NULL 
+0

第一個外連接dkb我想顯示ON cart.id = dkb.id WHERE dkb.id = $ idc 第二個外連接cdkb顯示ON購物車.id = dkb.id WHERE dkb.id = $ id $ idc來自page1.php的網址到cart.php,$ id來自page2.php的網址到cart.php .... LEFT OUTER JOIN DKB ON cart.id = dkb.id WHERE dkb.id = $ ID LEFT OUTER JOIN cdkb ON cart.id = cdkb.id WHERE cdkb.id = $ IDC WHERE cart.cookieId = GetCartId() – jona

+0

所以我認爲,而不是dkb.id然後$ idc應該在那個地方,如 ON cart.id = $ idc – jona

+0

什麼是非空意味着在這種情況下? – jona

1

要設置這個查詢一些數據對MySQL:

-- ********************************************************************** 
-- Table holding all the items in the users shopping cart, along with 
-- a cookieId for thier session 
-- ********************************************************************** 
Create table cart 
    (
    cartId   integer, 
    cookieId   integer, 
    id    integer, 
    qty    integer 
    ) 
go 

-- ********************************************************************** 
-- Table all the items available for sale with the price 
-- ********************************************************************** 
Create table dkb 
    (
    id    integer, 
    name    varchar(20), 
    price   decimal(12,2) 
    ) 
go 

-- ********************************************************************** 
-- Table that looks like the items table, but we have no idea why it is 
-- here. 
-- ********************************************************************** 
Create table cdkb 
    (
    id    integer, 
    name    varchar(20), 
    price   decimal(12,2) 
    ) 
go 

-- ********************************************************************** 
-- Another table with a price in it .. a completely different id field 
-- and a variety ... WTH is it for - No idea. 
-- ********************************************************************** 
Create table dblv 
    (
    dbl_id   integer, 
    price   decimal(12,2), 
    variety   varchar(20) 
    ) 
go 

insert into cart (cartid, cookieid, id, qty) values (1, 1, 1, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 2, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 3, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 4, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 5, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 6, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 7, 1); 
go 
insert into cart (cartid, cookieid, id, qty) values (1, 1, 8, 1); 
go 

insert into dkb (id, name, price) values (1,'my dkb 1', 10.00); 
go 
insert into dkb (id, name, price) values (2,'my dkb 2', 20.00); 
go 
insert into dkb (id, name, price) values (3,'my dkb 3', 30.00); 
go 
insert into dkb (id, name, price) values (4,'my dkb 4', 40.00); 
go 

insert into cdkb (id, name, price) values (5,'my cdkb 5', 50.00); 
go 
insert into cdkb (id, name, price) values (6,'my cdkb 6', 60.00); 
go 
insert into cdkb (id, name, price) values (7,'my cdkb 7', 70.00); 
go 
insert into cdkb (id, name, price) values (8,'my cdkb 8', 80.00); 
go 

insert into dblv (dbl_id, price, variety) values (1,1.99,'my dbl 1 variety'); 
go 
insert into dblv (dbl_id, price, variety) values (2,1.99,'my dbl 2 variety'); 
go 

那麼這個查詢的結果:

SELECT 
    cart.id cart_id, 
    dkb.id  dkb_id, 
    cdkb.id cdkb_id, 
    cart.*, 
    dkb.*, 
    cdkb.*, 
    dblv.* 
FROM 
    cart 

    LEFT OUTER JOIN dkb 
     ON cart.id = dkb.id 

    LEFT OUTER JOIN dblv 
     on dkb.id = dblv.dbl_id 

    LEFT OUTER JOIN cdkb 
     on cart.id = cdkb.id 

結果顯示爲:

cart_id dkb_id  cdkb_id cartId  cookieId id   qty  id   name     price  id   name     price  dbl_id  price  variety    
---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------------------- ------------ ---------- -------------------- ------------ ---------- ------------ -------------------- 
1   1   NULL  1   1   1   1   1   my dkb 1    10.00  NULL  NULL     NULL   1   1.99   my dbl 1 variety  
2   2   NULL  1   1   2   1   2   my dkb 2    20.00  NULL  NULL     NULL   2   1.99   my dbl 2 variety  
3   3   NULL  1   1   3   1   3   my dkb 3    30.00  NULL  NULL     NULL   NULL  NULL   NULL     
4   4   NULL  1   1   4   1   4   my dkb 4    40.00  NULL  NULL     NULL   NULL  NULL   NULL     
5   NULL  5   1   1   5   1   NULL  NULL     NULL   5   my cdkb 5   50.00  NULL  NULL   NULL     
6   NULL  6   1   1   6   1   NULL  NULL     NULL   6   my cdkb 6   60.00  NULL  NULL   NULL     
7   NULL  7   1   1   7   1   NULL  NULL     NULL   7   my cdkb 7   70.00  NULL  NULL   NULL     
8   NULL  8   1   1   8   1   NULL  NULL     NULL   8   my cdkb 8   80.00  NULL  NULL   NULL     

對不起今天沒多少時間 - 漫長的一天a工作。 :-)我明天再來看看..

+0

在我發佈表結構之前,我想添加一個雙表,在您發佈dlb.id = dkb.id時加入它,並將其與dkb表一起顯示其表信息。他們都需要開啓,因爲他們一起顯示信息。 – jona

+0

看看我編輯過的第一篇文章,併發布了帶有字段名稱的完整查詢。 – jona

+1

的確如此,因爲您希望它只在cart.id = dkb.id時顯示,所以它真的加入到dkb.id. :-) –