|
|
|
ช่วยหน่อยคับ echo $cartOutput; ใน cart.php ละไม่มีอะไรออมาเลย ดูหลายรอบก็ถูกหมดละ |
|
|
|
|
|
|
|
ช่วยหน่อยคับ echo $cartOutput; ละไม่มีอะไรออมาเลย
-----------------------------------------------------------cart.php-----------------------------------------------------------------------
Code (PHP)
<?php
session_start(); // Start session first thing in script
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
if (isset($_POST['pid'])){
$pid = $_POST['pid'];
$wasFound = false;
$i = 0;
//if the cart session variable is not set or cart arraty 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(1 => 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 lets 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));
}
}
}
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 2 (if user chooses to empty their shopping cart
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart"){
unset($_SESSION["cart_array"]);
}
?>
<?php
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Section 3 (render the cart for the user to view)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$cartOutput = "";
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item){
$i++;
$cartOutput .= "<h2>Cart item $i</h2>";
while (list($key, $value) = each($each_item)){
$cartOutput .= "$key: $value<br />";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Untitled Document</title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
left:0px;
top:1px;
width:800px;
height:336px;
z-index:1;
}
#apDiv2 {
position:absolute;
left:1px;
top:339px;
width:254px;
height:468px;
z-index:2;
}
#apDiv3 {
position:absolute;
left:1px;
top:14px;
width:794px;
height:19px;
z-index:3;
}
#apDiv4 {
position:absolute;
left:258px;
top:338px;
width:763px;
height:471px;
z-index:4;
}
-->
</style>
</head>
<body>
<div id="apDiv4">
<table width="100%" border="1">
<tr>
<td valign="top"> <p> </p>
<p>
<?php echo $cartOutput;?>
<p><br />
<a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a><p></td>
</tr>
</table>
</div>
<dd>
<div id="apDiv1"><img src="../images/Head.jpg" width="1024" height="338" /></div>
<div id="apDiv2"><img src="../images/login_02.jpg" width="255" height="468" /></div>
</body>
</html>
--------------------------------------------------------------------product.php----------------------------------------------------------------------------
Code (PHP)
<?php
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php
if (isset($_GET['id'])) {
include "storescript/connect_to_mysql.php";
$id = preg_replace('#[^0-9]#i', '', $_GET['id']);
// Use this var to check to see if this ID exists, if yes then get the product
// details, if no then exit this script and give message why
$sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$price = $row["price"];
$details = $row["details"];
$category = $row["category"];
$subcategory = $row["subcategory"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
}
} else {
echo "That item does not exist.";
exit();
}
} else {
echo "Data to render this page is missing.";
exit();
}
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />
<title><?php echo $product_name; ?></title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
left:0px;
top:1px;
width:800px;
height:336px;
z-index:1;
}
#apDiv2 {
position:absolute;
left:1px;
top:339px;
width:254px;
height:468px;
z-index:2;
}
#apDiv3 {
position:absolute;
left:1px;
top:14px;
width:794px;
height:19px;
z-index:3;
}
#apDiv4 {
position:absolute;
left:258px;
top:338px;
width:763px;
height:290px;
z-index:4;
}
-->
</style>
</head>
<body>
<div id="apDiv4">
<table width="100%" border="6">
<tr>
<td width="21%" align="center" valign="top"><p><br />
<img src="inventory_images/<?php echo $id; ?>.jpg" alt="<?php echo $product_name; ?>" width="136" height="165" border="1" />
</p>
<a href="inventory_images/<?php echo $id; ?>">View Full Size Image</a></td>
<td width="79%" valign="top"><h3> <?php echo $product_name; ?></h3>
<p> <?php echo $price; ?> B.</p>
<p> <?php echo "$subcategory $category"; ?></p>
<p> <?php echo $details; ?></p>
<p>
<form id="form1" name="form1" method=""post action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<input type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
</td>
</tr>
</table>
<p><br />
</p>
</div>
<dd>
<div id="apDiv1"><img src="../images/Head.jpg" width="1024" height="338" /></div>
<div id="apDiv2"><img src="../images/login_02.jpg" width="255" height="468" /></div>
</body>
</html>
Tag : PHP, HTML/CSS, JavaScript, Web Service
|
|
|
|
|
|
Date :
2013-09-08 10:08:30 |
By :
golf |
View :
732 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลอง var_dump($_SESSION["cart_array"]); ดูหน่อยครับว่ามันมีอัลไลบ้าง
|
|
|
|
|
Date :
2013-09-08 12:26:08 |
By :
itpcc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code เยอะน่ะครับ ลองดูระบบที่ผมเขียนเป็นตัวอย่าง ง่าย ๆ สั้น ๆ
PHP สร้างระบบตะกร้าสั่งซื้อสินค้า Shopping Cart ด้วย Session และ Array
|
|
|
|
|
Date :
2013-09-09 06:27:58 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
<form id="form1" name="form1" method=""post action="cart.php">
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
<input type="submit" name="button" id="button" value="Add to Shopping Cart" />
</form>
เจอ แล้วคับที่ผิด เส้นผมบังภูเขา 55555
|
|
|
|
|
Date :
2013-09-09 15:55:22 |
By :
golf |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|