2011-10-26 66 views
1

我一直在爲此工作了幾天。我從來沒有搞過任何與購物車有關的事情,所以我試圖理解這一點。這是我的網頁,如果你向旁邊看,它不是做我需要它做的事情,也顯示其他代碼:http://www.elinkswap.com/snorris/header.html。我想要做的是當你在header.html頁面上,並且你看看應該說你的購物車的內容你有0項。然後說你去的項目,你想1並點擊更新它應該說你有你的購物車中的1項。但由於某種原因,我無法讓它正常工作。這裏是我一起工作的代碼一點點:如何添加購物車總計

<?php # Script 5.2 - header.html 

/* 
* This page begins the HTML header for the site. 
* The header also creates the right-hand column. 
* This page calls session_start(). 
*/ 

// Need sessions! 
session_start(); 

// Check for a $page_title value: 
if (!isset($page_title)) $page_title = 'WoW::World of Widgets!'; 
?><!DOCTYPE html 
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
    <title><?php echo $page_title; ?></title> 
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
    <link href="./includes/style.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 
<div class="all"> 

    <div class="box"> 
     <div class="menu"><a href="#">home</a><a href="#">about</a><a href="#">products</a><a href="#">contact</a></div> 
     <div class="header"><img alt="" style="float:right; " src="./images/www.jpg" width="225" height="95" /> 
     <h1>[<span class="style1">WoW</span>] World of Widgets</h1> 
     <div class="clearfix"></div> 
    </div> 

    <div class="newsbar"> 
     <h1>Browse Widget Categories</h1> 
     <div class="p2"><ul> 
<li><a href="category.php?cid=5">Fuzzy Widgets</a></li> 
<li><a href="category.php?cid=4">Non-widget Widgets</a></li> 
<li><a href="category.php?cid=6">Razor-sharp Widgets</a></li> 
<li><a href="category.php?cid=2">Widgets That Bounce</a></li> 
<li><a href="category.php?cid=3">Widgets That Sit There</a></li> 
<li><a href="category.php?cid=1">Widgets That Wiggle</a></li> 
<?php 
// Get all the categories and 
// link them to category.php. 

// Define and execute the query: 
$q = 'SELECT category_id, category FROM categories ORDER BY category'; 
$r = mysqli_query($dbc, $q); 

// Fetch the results: 
while (list($fcid, $fcat) = mysqli_fetch_array($r, MYSQLI_NUM)) { 

    // Print as a list item. 
    echo "<li><a href=\"category.php?cid=$fcid\"></a></li>\n"; 

    if($_SERVER['PHP_SELF']!="CART FILE"){ 
       echo "<h1>Cart Contents</h1>"; 
       echo "<div class=\"p2\">"; 
       $itemCount=$k; 
       foreach($_SESSION['cart'] as $k=>$v){ 

        for($i=0;$i<count(X);$i++){ 
         $itemCount+=X; 
         } 

       } 
       echo "<a href=\"cart.php\">You have ".$itemCount." total items in your cart.</a>"; 
       echo "</div>\n"; 


} // End of while loop. 

     ?></ul></div> 


     <h1>Specials?</h1> 
     <div class="p2"> 
      <p>Maybe place specials or new items or related items here.</p> 
     </div> 

    </div> 

    <div class="content"> 

我還在學習一些事情和一些這只是沒有道理,我不能得到它的工作。感謝您尋找

回答

2

您可以只使用count();

無需循環數組遞增$itemCount變量在統計數組對象。

只是有:

$itemCount = count($myShoppingCart);

0

嘗試基地的回聲,如果上車查詢爲空或產品編號。 產品的數量可以通過您在 $ k => $ V中引用的值達到,對於這些值中的每一個,也有一個或多個值,即 計數結果+每件物品的訂購次數不止一次。

是這樣的:

 $aantal += $value ; 

     if ($value < 1 ) { 
     echo"Cart is empty"; 
     } else { 
     echo'<h3>YOURE CART('. $aantal .')</h3>'; 
     } 
相關問題