<?php
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 1 (if user attempts to add something to the cart from the product page)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$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, "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" && $value == $pid) {
// 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, "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, "quantity" => 1));
}
}
header("location: http://www.hirelogo.com/cart.php");
exit();
}
?>
我在理解錯誤時遇到問題。如果有幫助,我最近剛剛轉而使用PHP的新版本。這是錯誤:修改標題錯誤
Warning: Cannot modify header information - headers already sent by (output started at /home/hirelogo/public_html/cart.php:5) in /home/hirelogo/public_html/cart.php on line 40
線40是
header("location: http://www.hirelogo.com/cart.php ");
任何幫助理解,這是大加讚賞。另一個說明。交換機之前沒有發生此問題。
什麼是第5行? – Scuzzy
ob_end_flush();我試圖緩解這個問題。 – Chris
確保'<?php'之前沒有任何內容,因爲它會在運行代碼之前發送。 – Barmar