Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
use of session can't get the correct number to appear in cart
Old 07-27-2004, 06:39 PM use of session can't get the correct number to appear in cart
Experienced Talker

Posts: 34
Trades: 0
I create a session variable named $_SESSION[cart] to hold the number of items selected in a shopping cart. I then greated a link called <a href=$_SERVER[PHP_SELF]?buy=$n>BUY</a> that once selected is suppose to pass the value passed in the buy variable to $_SESSION[cart]= $_GET[buy]; I thought this would update the number of items in the variable $_SESSION[cart] so I created a statement that printed out the results of what is in the cart
$numitems=count($_SESSION[cart]);
echo "<td>$numitems Items in Shopping Cart</td> ";

The problem I am having is that every time I selected an Item its not updating the number of items stored in the cart variable.

Can someone tell me what I am doing wrong listed below is the script.


<?php
session_start();
if(!isset($_SESSION[cart])){
$_SESSION['cart']=array();
}
if(isset($_GET[buy])){
$_SESSION[cart]= $_GET[buy];
header("location: $_SERVER[PHP_SELF]");
exit();

}


$products=array('IBM Laptop', 'Dell Laptop', 'Gateway Desktop', 'Service Contract');
$prices=array(200.00, 300.00, 400.00, 199.99);
echo "<table cellspacing=20 >";
echo "<tr><TH>Item Title</th><TH>Price</th></tr>";

for($n=0;$n<count($products);$n++){
echo "<tr>";
echo "<td>$products[$n]</td>";
$price=number_format($prices[$n],2);

echo "<td>$price</td>";
echo "<td><a href=$_SERVER[PHP_SELF]?buy=$n>BUY</a></td>";
echo "</tr>";
}
$numitems=count($_SESSION[cart]);
echo "<td>$numitems Items in Shopping Cart</td> ";
echo "</table>";

?>
cedtech23 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-27-2004, 06:59 PM hi
Mooofasa's Avatar
Defies a Status

Posts: 1,611
Name: Michael (mik) Land
Location: England
Trades: 0
I don't believe that you have ended the script. What i mean is there is 'die' or 'exit'. Could this be the problem?

mik
__________________

Please login or register to view this content. Registration is FREE
- Tumblog with thoughts, quotes, links, videos, images and my creations.

Please login or register to view this content. Registration is FREE
- The best free web browser.

Please login or register to view this content. Registration is FREE
- Firefox is now Firefail.
Mooofasa is offline
Reply With Quote
View Public Profile Visit Mooofasa's homepage!
 
Old 07-27-2004, 07:03 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
mik, you do not need a die or exit to end the script. Once the parser has reached the end of the file, the script is stopped.

cedtech2, your problem is with this part:
$_SESSION[cart]= $_GET[buy];
You're simply re-assigning the value of $_SESSION[cart] instead of adding the product to the array. Try this instead:
$_SESSION['cart'][] = $_GET['buy'];
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 07-27-2004, 07:35 PM error message with change
Experienced Talker

Posts: 34
Trades: 0
I changed the script as you said and received this error

Fatal error: [] operator not supported for strings in C:\Program Files\Apache Group\Apache2\htdocs\cart2.php on line 7

do you know what is means??



Quote:
Originally Posted by Chroder
mik, you do not need a die or exit to end the script. Once the parser has reached the end of the file, the script is stopped.

cedtech2, your problem is with this part:
$_SESSION[cart]= $_GET[buy];
You're simply re-assigning the value of $_SESSION[cart] instead of adding the product to the array. Try this instead:
$_SESSION['cart'][] = $_GET['buy'];
cedtech23 is offline
Reply With Quote
View Public Profile
 
Old 07-27-2004, 08:17 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
This is working for me (I cleaned it up a bit):

PHP Code:
<?php

if(!isset($_SESSION['cart']))
        
$_SESSION['cart'] = array();

if(isset(
$_GET['buy']))
{
    
$_SESSION['cart'][] = $_GET['buy'];
    
header('Location: ' $_SERVER['PHP_SELF']);
    exit();
}

$products = array('IBM Laptop''Dell Laptop''Gateway Desktop''Service Contract');
$prices   = array(200.00300.00400.00199.99);
$numitems count($_SESSION['cart']);

echo 
"<table cellspacing=20 >";
echo 
"<tr><TH>Item Title</th><TH>Price</th></tr>";

for(
$n=0$n count($products); $n++)
{
    
$price number_format($prices[$n], 2);

    echo 
"<tr>";
    echo 
"<td>$products[$n]</td>";

    echo 
"<td>$price</td>";
    echo 
"<td><a href=$_SERVER[PHP_SELF]?buy=$n>BUY</a></td>";
    echo 
"</tr>";
}

echo 
"<td>$numitems Items in Shopping Cart</td> ";
echo 
"</table>";

?>
Try closing your browser and trying it again to restart your session with a clean $_SESSION['cart'].
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to use of session can't get the correct number to appear in cart
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23443 seconds with 12 queries