Forum Moderators: coopster

Message Too Old, No Replies

Shopping Cart problems

         

mysterious guy

5:49 am on Dec 16, 2004 (gmt 0)

10+ Year Member



Hi,
i'm trying to build the simplest shopping cart bcoz i'm new to PHP.I found a piece of code on the net could someone xplain it to me.Here it is:

session_start();

if (!isset($_SESSION['caddy']))
{
$_SESSION['caddy'] = array();
}

if (isset($_REQUEST['id']))
{

if ($_SESSION['caddy'][$_REQUEST['id']])---line41
{
$_SESSION['caddy'][$_REQUEST['id']]++;
}
else
{
$_SESSION['caddy'][$_REQUEST['id']]=1;
}
}

if (count($_SESSION['caddy'])==0)
{
echo "<p>Caddy Empty!</p>";
}
----------------------------------------------------
when i'm using this piece of code i'm getting this error:

Notice: Undefined index: 7 in c:\program files\easyphp1-7\www\caddy\gerer_caddy.php on line 41

I d'nt understand..i've passed the 'id' to this script! or is it 'caddy' that is undefined..

A+

jatar_k

7:18 am on Dec 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what happens if you just

echo $_SESSION['caddy'][$_REQUEST['id']];

do you get the id you passed?

mysterious guy

8:22 am on Dec 16, 2004 (gmt 0)

10+ Year Member



I get the same error...but when I click on the refresh button it works! how come

mysterious guy

10:41 am on Dec 16, 2004 (gmt 0)

10+ Year Member



Hi..there's a couple of things I dn't know how to do:

1)How to put two words on the same line but one to the xtreme right and the other to the xtreme left

I have this:

echo "<html>";
echo "<head><title>Librairie</title></head>";
echo "<body>";
echo "<h1>Welcome..</h1>";
echo "<p align='right'>";
echo "[ <a href=\"login.php?&log='out'\" align='right'>Log Out</a> ] ";
echo "</p>";
echo "<hr>";
I want 'Log Out' to appear to the xtreme right of 'Welcome'
----------------------------------------------------------
2)Every time I click on the refresh button, the quantity of items in my shopping cart is incremented..How to avoid this?(look at my 1st post for the code)

thx..

jatar_k

6:24 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sorry mysterious guy, busy day, couldn't respond

darn jobs always get in the way ;)

1) use a table or a div and align them to the two sides

2) protecting against refresh problems is always interesting. I can tell you why it is doing it

if ($_SESSION['caddy'][$_REQUEST['id']])
{
$_SESSION['caddy'][$_REQUEST['id']]++;
}

that says if $_SESSION['caddy'][$_REQUEST['id']] is already set then increment it by 1

to protect against this there needs to be some scenario to check.

if ('form already submitted') do nothing

maybe if you give us a little more logic we might be able to help out more.

mysterious guy

6:56 am on Dec 17, 2004 (gmt 0)

10+ Year Member



Hi..
thx.I'm not using form.In fact the script(add.php) is executed when clicking on a link passing as parameter the id of the item the user want to buy. So the php script add the id to the cart('session[caddy]') and display the items bought in the browser.
But when I click on refresh, the cart is incremented.

there must be a way to know whether the script is being executed for the 2nd time with the same session_id.Or simply disabled the refresh button.

I dn't know much about php.I think its high time for me to buy a book!

Thx again.