2013-03-05 79 views
0

基本上我遵循教程在php中創建一個基本的購物車系統,它與mysql數據庫鏈接。 This is the link to the tutorial將產品尺寸添加到購物車

我能夠使用switch語句在產品頁面上顯示正確的尺寸選項/下拉框。 (大小選項通過管理面板添加到後端)。

product.php:

<?php 
switch ($sizing) { 
case "Womens_t": 
    echo '<select name= "size"> 
     <option value="xsmlsml">X-Small - Small</option> 
     <option value="smlmed">Small - Medium</option> 
     <option value="medlge">Medium - Large</option> 
     </select>'; 
    break; 
case "Womens_b": 
    echo '<select name= "size"> 
     <option value="8">AUS 8</option> 
     <option value="10">AUS 10</option> 
     <option value="12">AUS 12</option> 
     <option value="14">AUS 14</option> 
     </select>'; 
    break; 
case "Mens_t": 
    echo '<select name="size"> 
     <option value="xsmlsml">X-Small - Small</option> 
     <option value="smlmed">Small - Medium</option> 
     <option value="medlge">Medium - Large</option> 
     </select>'; 
    break; 
case "Mens_b": 
    echo '<select name="size"> 
     <option value="8">AUS 8</option> 
     <option value="10">AUS 10</option> 
     <option value="12">AUS 12</option> 
     <option value="14">AUS 14</option> 
     </select>'; 
    break; 
case "": 
    echo ''; 
    break; 

} 

    ?><input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> 
     <input type="submit" name="button" id="button" value="Add to cart" /> 
    <div class="table"> 


    </form> 

當它連接到它與一個錯誤,指出「大小」是一個未定義變量(線135),它是代碼,其中下出現的cart.php頁車被渲染爲用戶觀點:

線135

$cartOutput .= '<td>' . $size . '</td>'; 

所以我不知道,如果大小隻是在開始時沒有或者判斷我公頃定義已經把它拉出cart_array?我有點難過,我還沒有找到任何解決方案。我試過改變變量,但沒有任何工作。

這是cart.php代碼:

if (isset($_POST['pid'])) { 
$pid = $_POST['pid']; 
$size = $_POST['size']; 
$wasFound = false; 
$i = 0; 
// If the cart session variable is not set or cart array is empty 
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
    // RUN IF THE CART IS EMPTY OR NOT SET 
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "size" => $size, "quantity" => 1)); 
} else { 
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT 
    foreach ($_SESSION["cart_array"] as $each_item) { 
      $i++; 
      while (list($key, $value) = each($each_item)) { 
       if ($key == "item_id"&&"size" && $value == $pid&&$size) { 
        // That item is in cart already so let's adjust its quantity using array_splice() 
        array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "size" => $size, "quantity" => $each_item['quantity'] + 1))); 
        $wasFound = true; 
       } // close if condition 
      } // close while loop 
     } // close foreach loop 
     if ($wasFound == false) { 
      array_push($_SESSION["cart_array"], array("item_id" => $pid,"size" => $size, "quantity" => 1)); 
     } 
} 
header("location: cart.php"); 
exit(); 
} 

那麼這就是這使得車爲用戶視圖代碼:

$cartOutput = ""; 
    $cartTotal = ""; 
    $pp_checkout_btn = ''; 
    $product_id_array = ''; 
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; 
    } else { 

    // Start PayPal Checkout Button 

    $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
    <input type="hidden" name="cmd" value="_cart"> 
    <input type="hidden" name="upload" value="1"> 
    <input type="hidden" name="business" value="[email protected]">'; 

    // Start the For Each loop 

    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
    $item_id = $each_item['item_id']; 
    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); 
    while ($row = mysql_fetch_array($sql)) { 
     $product_name = $row["product_name"]; 
     $price = $row["price"]; 
     $details = $row["details"]; 
    } 
    $pricetotal = $price * $each_item['quantity']; 
    $cartTotal = $pricetotal + $cartTotal; 
    setlocale(LC_MONETARY, "en_AUS"); 
    $pricetotal = number_format($pricetotal, 2, '.',' '); 

    // Dynamic Checkout Btn Assembly 

    $x = $i + 1; 
    $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> 
    <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> 
    <input type="hidden" name="on0_' . $x . '" value="size"> 
    <input name="size" type="hidden" value="' . $each_item['size'] . '" /> 
    <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; 

    // Create the product array variable 

    $product_id_array .= "$item_id-".$each_item['quantity'].","; 

    // Dynamic table row assembly 

    $cartOutput .= "<tr>"; 
    $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.png" alt="' . $product_name. '" width="40"      height="52" border="1" /></td>'; 
    $cartOutput .= '<td>' . $details . '</td>'; 
    $cartOutput .= '<td>' . $size . '</td>'; 
    $cartOutput .= '<td>$' . $price . '</td>'; 
    $cartOutput .= '<td><form action="cart.php" method="post"> 
    <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> 
    <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> 
    <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> 
    </form></td>'; 
    //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; 
    $cartOutput .= '<td>' . $pricetotal . '</td>'; 
    $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; 
    $cartOutput .= '</tr>'; 
    $i++; 
} 
setlocale(LC_MONETARY, "en_AUS"); 
$cartTotal = number_format($cartTotal, 2, '.',' '); 
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>"; 

// Finish the Paypal Checkout Btn 

$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '"> 
<input type="hidden" name="notify_url" value="127.0.0.1/storescripts/my_ipn.php"> 
<input type="hidden" name="return" value="127.0.0.1/checkout_complete.php"> 
<input type="hidden" name="rm" value="2"> 
<input type="hidden" name="cbt" value="Return to The Store"> 
<input type="hidden" name="cancel_return" value="127.0.0.1/paypal_cancel.php"> 
<input type="hidden" name="lc" value="US"> 
<input type="hidden" name="currency_code" value="USD"> 
<input type="image" src="http://www.paypal.com/en_AUS/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!"> 
</form>'; 
} 
+0

$ size是一個未定義的變量,因爲如果$ size屬於數據庫產品,則它沒有值,然後在您的代碼中爲$ size分配值'while($ row = mysql_fetc h_array($ sql)){ $ product_name = $ row [「product_name」]; $ price = $ row [「price」]; $ details = $ row [「details」]; $ size = $ row [「size」]; }' – Snippet 2013-03-05 11:02:42

回答

0

你有一個foreach循環它通過你的循環會話數組。你忘了這裏定義$size

foreach ($_SESSION["cart_array"] as $each_item) { 
    $item_id = $each_item['item_id']; 
    $size = $each_item['size'];   //Here :) 

    $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); 
    while ($row = mysql_fetch_array($sql)) { 
     $product_name = $row["product_name"]; 
     $price = $row["price"]; 
     $details = $row["details"]; 
    } 

而且,這是一個糟糕的想法querys使用這樣的(單獨的迴路)來轟擊你的分貝:

$item_IDs = []; 
foreach($_SESSION['cart_array'] as $each_item) { 
    $item_IDs[] = $each_item['item_id']; 
} 

//Then the query becomes 
SELECT * FROM products WHERE ID IN(". explode(',', $item_IDs) .") //and use this result 

而且使用庫MySQLi或PDO,而不是mysql_ 的,因爲mysql_被剔除:) http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/

+0

謝謝你這麼多! – user2135278 2013-03-05 22:23:39

相關問題