...

View Full Version : Shoping cart, help me fix it..



crazyman
08-10-2008, 08:12 AM
At first im sorry about my bad English.Now i have got a big problem with shopping cart.I have got my teacher's demo code about it.But I just learned php in 3 days, i cant understand all of them.So my problem is i dont know how to send session from my cart to order_form to insert data into database.
Here is my code:

<?php
session_start();
ob_start();
?>
<!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=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="../Css/style.css">
<link rel="stylesheet" type="text/css" href="../Css/giohang.css">
</head>

<body>
<div id="toanbo">
<div id="min_banner">
<?php
include("../include/banner.php");
?>
</div>
<div id="menu">
<?php
include("../include/menu.php");
?>
</div>
<?php
$cart = $_SESSION["CART"];
$action = $_REQUEST["action"];
$_SESSION["soluong"] = $quantity;
switch($action)
{
case "add":
$id = $_REQUEST["id"];
if($cart)
{
if(array_key_exists($id,$cart))
{
$cart[$id] +=1;
}
else
{
$cart[$id]=1;
}
}
else
{
$cart[$id]=1;
}
$_SESSION["CART"] = $cart;
header("Location:cart.php");
break;
case "update":

foreach(array_keys($cart) as $value)
{
$cart[$value] = $_REQUEST["quantity_".$value];
}
$_SESSION["CART"] = $cart;
break;
case "delete":
$id = $_REQUEST["id"];
if($cart)
{
foreach(array_keys($cart) as $value)
{
if($value != $id)
{
$newcart[$value] = $cart[$value];
}
}
$_SESSION["CART"] = $newcart;
$cart=$newcart;
}
header("Location:cart.php");
break;

}
function GetQuantity($id)
{
$cart = $_SESSION["CART"];
$quantity=0;
foreach(array_keys($cart) as $value)
{
if($value == $id)
{
$quantity = $cart[$id];
break;
}
}
$_SESSION["soluong"]= $quantity;
return $quantity;

}

?><div id="shop">
<form id="form1" name="form1" method="post" action="giohang.php">
<input type="hidden" id="action" name="action" value="update" />
<table width="60%" border="0" align="center">
<caption class="shopping_cart">Shopping Cart</caption>
<tr>
<td width="47%">Name</td>
<td width="14%">Quantity</td>
<td width="14%">Price</td>
<td width="8%">Total</td>
<td width="17%">Function</td>
</tr>
<tr>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
</tr>
<?
if($cart){
$idlist = "";
$total=0;
foreach(array_keys($cart) as $value){
$idlist = $idlist.$value.",";
}
$idlist = $idlist."0";
$sql = "SELECT * FROM game WHERE GameID IN (".$idlist.")";

$conn = mysql_connect("localhost","root","gameshop");
if(!$conn){
die("Could not connect: ". mysql_error());
}else{
mysql_select_db("gameshop",$conn);

}

$result = mysql_query($sql,$conn);
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td><div align="left"><span class="style9"><? echo $row["GameName"]?></span></div></td>
<td><div align="left"><span class="style9">
</span><span class="style9">
<label>
</span>
<span class="style9">
<input name="quantity_<? echo $row["GameID"]?>" type="text" id="quantity_<? echo $row["GameID"]?>" value="<? echo GetQuantity($row["GameID"])?>" size="3"/>
</span><span class="style9">
</label>
</span></div></td>
<td><div align="left"><span class="style9"><? echo $row["Price"]?></span></div></td>
<td><div align="left"><span class="style11">
<? $total += $row["Price"]*GetQuantity($row["GameID"]);echo $row["Price"]*GetQuantity($row["GameID"]);?>
</span></div></td>
<td><div align="left"><span class="style9"><a href="cart.php?action=delete&id=<? echo $row["GameID"] ?>">Delete</a></span></div></td>
</tr>
<?

}
mysql_close($conn);
}else{
?>
<tr>
<td><div align="left"><span class="style9">Your cart have 0 items</span></div></td>
<td><div align="left"><span class="style9"></span></div></td>
<td><div align="left"><span class="style9"></span></div></td>
<td><div align="left"><span class="style11"></span></div></td>
<td><div align="left"><span class="style9"></span></div></td>
</tr>
<?
}
?>
<tr>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
</tr>
<tr>
<td><div align="left"><span class="style9"></span></div></td>
<td><div align="left"><span class="style9"></span></div></td>
<td><div align="center" class="style11">
<div align="left"><span class="style16">Summary</span></div>
</div></td>
<td><div align="center" class="style11">
<div align="left"><span class="style14"><? echo $total?></span></div>
</div></td>
<td><div align="left"><span class="style9">
</span></div> <span class="style9"><label></label>
</span></td>
</tr>
<tr>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
</tr>
<tr>
<td><div align="left"></div></td>
<td><div align="left"></div></td>
<td><div align="left"><span class="style9">
<input type="submit" name="Submit" value="Update" />
</span></div></td>
<td><div align="left">
<label>
<input type="button" name="Submit2" value="Continue"
onclick="location.replace ('../templates/index.php');"/>
</label>
</div></td>
<td><div align="left">
<label>
<input type="button" name="Submit3" value="CheckOut" onclick="window.location = 'checkout.php';"/>
</label>
</div></td>
</tr>
</table>
</form>

Can somebody tell me how to send session to order_form(checkout.php) when i click check out button ? I need to know how much is price of a gameid,total of a gameid and something when insert in to order table.Thanks everybody in advance.!!! T__T

Fou-Lu
08-10-2008, 08:36 AM
Since this is an assignment for you, we can't offer a whole pile of help.
What I can tell you is that $_SESSION is a superglobal, this means you can access it in any scope. To use sessions on multiple pages, you simply need a session_start() call:


// Page.php
<?php
session_start();
$_SESSION['test'] = 'this is a test';
?>
// anotherpage.php
<?php
session_start();
if (isset($_SESSION['test']))
{
printf("Session's test value is %s\n", $_SESSION['test']);
}
?>


SQL is used for you're insertions. You have SQL inside the current script, just look for mysql_ functions. SQL insertions are writen in the format: INSERT INTO {TABLE} (field, [field, field...]) VALUES (value, [value, value]);

Also, you'll want to keep a bookmark on php.net (.ca, .com, etc). Most functions are well documented with examples plus the addition of user contributed examples.

I hope thats enough to point you in the right direction. We do have pretty stringent policies on homework help, so we cannot offer a lot of help in that regard.

crazyman
08-10-2008, 08:57 AM
Ah u dont understand my mean.My mean that i know about session but how to send it to order_form.If i follow that code, i just can be send a session, is not an array session to order_form.I need to know how much are total of gameids,and how much is each gameid have total ? . How do u think when i have just learned php in 3 days ??? my teacher dont teach me about array and now he want to me must code a shopping cart.But dont teach anything about shopping cart.Please tell me how to send an array session ???

Fou-Lu
08-10-2008, 06:18 PM
Well, according to you're current code, any page with session_start() can access the cart array which is stored in $_SESSION['CART']. No special passing is required as long as you have the session_start() on any page that you wish to use them in. With this, you can access any variable set within the session.

Arrays are easy. Just consider them a small collection of mixed data in php, with index/value pairs. Iteration through loops is also easy, depending on what you have done. According to this code, you're session cart is setup like so:


// _SESSION
CART => array(
$id => $num,
$id => $num,
...
);

So, you have whats called a multidimensional $_SESSION array indexed by CART. Since you're id index can potentially be imxed, this somewhat limits you on you're iteration. You will require either a while() loop to fetch the current value and proceed to the next, something like:


while ($cur = current($_SESSION['CART']))
{
next($_SESSION['CART']);
}

But that has a pain because you need to separate you're key / value pairs. A better solution is a foreach loop which was created just for this purpose:


foreach ($_SESSION['CART'] AS $itemKey => $itemQuant)
{
}




Think of a session as a server side cookie. The user gives you an associated key for the session and any data within that session can be retrieved regardless of from what page, and still access the same values. This is how the builtin functionality of sessions work in PHP.

If you are having great difficulty with this assignment, you are probably better of requesting help from you're instructor instead of consulting with us. You're instructor knows what you know already and knows what the assignment should be like in the end. This should let him better explain how things are done and what he expects compared to us.

mlseim
08-11-2008, 06:01 PM
I'm also questioning the motives of the instructor.

That sounds like an impossible assignment for a 3-day PHP student,
especially with no explanations about arrays. I'm guessing most of
the students have previous programming knowledge and you don't?

Was there a pre-requisite class that came before your current class?
The class you're in sounds like a 2nd level PHP class.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum