/    Sign up×
Community /Pin to ProfileBookmark

Couple of Questions

The following code is what I have so far to create a simple shopping cart. Is there a different way of adding the id number of the product into the array? – As I don’t 100% understand what is happening. How do I notify the user that “The Item already exists”, if the cart already contains their selected item?

[CODE]$cart = $_SESSION[‘cart’];
if ($cart) {
$cart .= ‘,’.$_GET[‘id’];
} else {
$cart = $_GET[‘id’];
}
$_SESSION[‘cart’] = $cart;[/CODE]

How do I delete records from the shopping cart?

[CODE]<?php
session_start();

function writeShoppingCart() {
$cart = $_SESSION[‘cart’];
if (!$cart) {
return ‘<p>You have no items in your shopping cart</p>’;
} else {
// Parse the cart session variable
$items = explode(‘,’,$cart);
$s = (count($items) > 1) ? ‘s’:”;
return ‘<p>You have <a href=”cart.php”>’.count($items).’ item’.$s.’ in your shopping cart</a></p>’;
}
}

$cart = $_SESSION[‘cart’];
if ($cart) {
$cart .= ‘,’.$_GET[‘id’];
} else {
$cart = $_GET[‘id’];
}
$_SESSION[‘cart’] = $cart;

$total = 0;
echo ‘<table>’;
$id = $_GET[‘id’];

mysql_select_db(“MP3”, $con);

$items = explode(‘,’,$cart);
echo $cart;
foreach ($items as $item)

{
$result = mysql_query(“SELECT * FROM books WHERE id = ‘$item'”) or die(“Your have an error because:<br />” . mysql_error());
while($row = mysql_fetch_array($result))
{

echo ‘<tr>’;
echo ‘<td><a href=”cart.php?action=delete&id=’.$id.'” class=”r”>Remove</a></td>’;
echo ‘<td>’.$row[‘title’].’ by ‘.$row[‘author’].'</td>’;
echo ‘<td>&pound;’.$row[‘price’].'</td>’;
$total = $total + $row[‘price’];
echo ‘</tr>’;
}
}
echo ‘</table>’;
echo ‘<p>Grand total: &pound;’.$total.'</p>’;
?>[/CODE]

Also, I have a how do I put the add and delete code into a switch statement? I have the following code.

[CODE]switch ($action)
{
case ‘add’:
break;

case ‘delete’:
break;
}[/CODE]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@jasonahouleNov 21.2007 — You may have bigger problems here than you think. Your cart should be an array and currently it is a comma separated string. I suggest you take a look at the array documentation. http://us.php.net/array

You can do what you want with a comma delimited string but you are making it unnecessarily complex. You would be better off using multi-dimensional arrays. This way you can account for multiples of the same product in your cart. For example, your cart variable may look something like this
[code=php]
$_SESSION['cart'][184][2];
[/code]

Where cart is your cart, 184 is your product id, and 2 is the quantity of the product. This way you can use the PHP array functions to determine if the product is already in your array and then from there can easily get the quantity. Definitely check out the documentation.
Copy linkTweet thisAlerts:
@DysanauthorNov 21.2007 — May I ask for your help in changing the code to include a multi-dimensional array, instead of a comma delimited string?

What needs to be changed, and what to?

Much Appreciated! ?
Copy linkTweet thisAlerts:
@jasonahouleNov 21.2007 — Did you look at the documentation yet?
Copy linkTweet thisAlerts:
@DysanauthorNov 21.2007 — Yes, where do I go from there?

Where do I start?
Copy linkTweet thisAlerts:
@jasonahouleNov 21.2007 — Ok, post the changed code that you have (using the array instead of string) and I will help guide you in the right direction.
Copy linkTweet thisAlerts:
@DysanauthorNov 21.2007 — All I have is this? The comma delimited string version (as posted earlier) I need help changing it to a multi-dimensional array, and adding/deleting products:

Also, I have a lot of crap code in there, that doesn't really do anything, how do I tidy the code up?

[CODE]<?php

session_start();

function writeShoppingCart() {
$cart = $_SESSION['cart'];
if (!$cart) {
return '<p>You have no items in your shopping cart</p>';
} else {
// Parse the cart session variable
$items = explode(',',$cart);
$s = (count($items) > 1) ? 's':'';
return '<p>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></p>';
}
}

$cart = $_SESSION['cart'];
if ($cart) {
$cart .= ','.$_GET['id'];
} else {
$cart = $_GET['id'];
}
$_SESSION['cart'] = $cart;




$total = 0;
echo '<table>';
$id = $_GET['id'];

mysql_select_db("book_store", $con);

$items = explode(',',$cart);
echo $cart;
foreach ($items as $item)

{
$result = mysql_query("SELECT * FROM books WHERE id = '$item'") or die("Your have an error because:<br />" . mysql_error());
while($row = mysql_fetch_array($result))
{

echo '<tr>';
echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
echo '<td>'.$row['title'].' by '.$row['author'].'</td>';
echo '<td>&pound;'.$row['price'].'</td>';
$total = $total + $row['price'];
echo '</tr>';
}
}
echo '</table>';
echo '<p>Grand total: &pound;'.$total.'</p>';

?>[/CODE]
Copy linkTweet thisAlerts:
@jasonahouleNov 21.2007 — Well, it appears that you are really not making any attempt to do this yourself. I will be happy to help you but I won't do it for you. If you read the documentation and make some effort then I will be glad to help you learn.

Also, the switch to the multi-dimensional array was a suggestion. If you are not comfortable working with them then don't do it. I was simply stating that it would be much more complicated dealing with comma separated values.
×

Success!

Help @Dysan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...