Reply
Problem with PHP Sessions
Old 08-05-2004, 10:50 AM Problem with PHP Sessions
compmodder26's Avatar
Experienced Talker

Posts: 44
Trades: 0
I am in the process of developing an online store. I am working on the shopping cart right now. The problem I am having is that sometimes when I add the first item to the cart it doesn't always recognize it. Then with each subsequent product added it will only recognize that one product. In other words it will only display the last product that was added. Now the keyword here is sometimes. There are times when it will work flawlessly. The sequence of events in which it works and doesn't work are exactly the same. In both instances I have gone through the exact same routine.

Here is the code for adding an item to the cart:
PHP Code:
session_start();
if (!(isset(
$_SESSION['prod_id']))){
    
$_SESSION['prod_id'] = array($_POST['prod_id']);
    
$_SESSION['name'] = array($_POST['name']);
    
$_SESSION['reg_price'] = array($_POST['reg_price']);
    
$_SESSION['mem_price'] = array($_POST['mem_price']);
    
$_SESSION['quantity'] = array($_POST['quantity']);
    
session_write_close();
    
header("Location: http://www.kentuckygear.com/showcart.php");
    
end;
}
else{
    
array_push($_SESSION['prod_id'], $_POST['prod_id']);
    
array_push($_SESSION['name'], $_POST['name']);
    
array_push($_SESSION['reg_price'], $_POST['reg_price']);
    
array_push($_SESSION['mem_price'], $_POST['mem_price']);
    
array_push($_SESSION['quantity'], $_POST['quantity']);
    
session_write_close();
    
header("Location: http://www.kentuckygear.com/showcart.php");
    
end;

Here is the code used in showing the shopping cart:
*note: I am not showing the code that says "session_start();" but rest assured it is at the top of the page before anything else*
PHP Code:
if (isset($_SESSION['prod_id'])){
    
$subtotal 0;
    
//not empty, loop through arrays and print each product's information
    
for($x=0$x sizeof($_SESSION['prod_id']); $x++){
        echo <<<DONE
        <tr><form action="updatequantity.php" method="post">
        <td><p>
{$_SESSION['prod_id'][$x]}</p></td>
        <td><p>
{$_SESSION['name'][$x]}</p></td>
        <td><input type="text" size="2" name="quantity" value="
{$_SESSION['quantity'][$x]}"></td>
DONE;
        
//if they are a member then apply discount
        
if (!isset($_SESSION['member'])){
        echo <<<DONE
        <td align="right"><p>\${$_SESSION['reg_price'][$x]}</p></td>
DONE;
        
//if not a member calculate regular price total for each product based on quantity for each
        
$regpricetotal$_SESSION['reg_price'][$x] * $_SESSION['quantity'][$x];
        
//format price as a decimal
        
$regpricetotalnumber_format($regpricetotal2".",",");
        
//increment subtotal
        
$subtotal += $regpricetotal;
        echo <<<DONE
        <td align=right><p>$$regpricetotal</p></td>
        <td><input type="submit" value="Update"></td>
        <input type="hidden" name="prod_id" value="
{$_SESSION['prod_id'][$x]}">
        </form>
        </tr>
        
DONE;
        }
        
//user is a valid member
        
else {
            echo <<<DONE
            //quote the member price instead of the regular price
            <td align="right"><p>\$
{$_SESSION['mem_price'][$x]}</p></td>
DONE;
            
//calculate member price total for each product based on quantity ordered for each
            
$mempricetotal $_SESSION['mem_price'][$x] * $_SESSION['quantity'][$x];
            
//format price as a decimal
            
$mempricetotalnumber_format($mempricetotal2".",",");
            
//increment subtotal
            
$subtotal += $mempricetotal;
            echo <<<DONE
            <td align=right><p>\${$mempricetotal}</p></td>
            <td><input type="submit" value="Update"></td>
            <input type="hidden" name="prod_id" value="
{$_SESSION[prod_id][$x]}">
            </form>
            </tr>
DONE;
        }
    }
    
//format subtotal as a decimal
    
$subtotal number_format($subtotal2"."",");
    echo 
"<tr><th colspan=\"4\" align=\"right\">Subtotal:</th><td align=\"right\"><p>\${$subtotal}</p></td></tr>\n";  
    echo <<<DONE
DONE;
}
//shopping cart is empty (there are no items in Session arrays)
else{
    echo 
"<p align=\"center\">There are currently no products in your shopping cart</p>";
}
echo <<<DONE
</table><font face="Arial" size="1" color="red">Notice: to delete a product from your cart,
set its quantity to 0 and click its update button</font><br><br>
<table border="0" align="center" cellpadding="5" cellspacing="5">
DONE;
//user has not entered there member number
//display a prompt to do so
if (!isset($_SESSION['member'])){
echo <<<DONE
<tr>
<td colspan="2">
<form action="checkmember.php" method="post">
<p>Members of the UK Alumni Association recieve a discounted price on select merchandise<br>
Enter your member ID number here to recieve your discount: <input type="text" size="15" name="id" value=""><br>
<input type="submit" value="Submit">&nbsp;<font face="Arial" size="1" color="black">If you are not a member and would like to join, <a href="http://www.ukalumni.net/membership/join.htm" target="_blank">click here</a></font>
</form>
</td>
</tr>
DONE;
}
//user has entered member number
//tell them that they have member discount priviledges
else {
echo <<<DONE
<tr>
<td colspan="2">
<p>You have been verified as a member and you qualify for member discounts</p>
</td>
</tr>
DONE;
}
//there is an item in the shopping cart
//allow them to check out or empty there cart
if (isset($_SESSION['prod_id'])){
echo <<<DONE
<tr>
<td width="20%">
<form action="empty.php" method="post">
<input type="submit" value="Empty Shopping Cart">
</form>
</td>
<td>
<form action="checkout.php" method="post">
<input type="submit" value="Make Purchase">
</form>
</td>
</tr>
</table>
DONE;

I appreciate any help you all can give me.
compmodder26 is offline
Reply With Quote
View Public Profile Visit compmodder26's homepage!
 
 
When You Register, These Ads Go Away!
Reply     « Reply to Problem with PHP Sessions
 

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.11951 seconds with 13 queries