Jump to content

Problem using sessions - how to allow multiple 'purchases'?


spiderman_2k7

Recommended Posts

Hi. I am creating a simple PHP ordering system where a user can purchase items and then 'buy' them. I was having problems totalling the prices up of all the items a user 'purchased' until I came accross using sessions. I adopted this in to my PHP and once a submit button is pressed a new page opens displaying the price of the item without VAT and a price with VAT, which is what my aim is.

 

However, the problem is that I need my system to allow a user to purchase one OR more items. At the moment I can only get a user to purchase one item with sessions, is it possible to have a user purchase more then one item?

 

Here are my codes:

 

Page listing items for 'sale' -

 

<html> <head> <title>Football Shirts 'R' Us</title> </head> 
<body> 
<body bgcolor="#339900"> 
<p> <font size=16, color=white><b><u><center>Football Shirts 'R' Us!</center></u></b></p> 
<form action="/showvariables3.php" method="post">   
<br> 
<table width=45%, border=1, align=center> 
<tr> 
   <th> ID </th>     <th> Football Shirt </th> 
    <th> Team  </th>    <th> Price  </th>    <th> Select  </th> 
</tr> 
<tr><tr> 
<?php 
    if(!($lines = file('read_shirts.txt'))) 
    {echo 'ERROR: Unable to open file! </body></html>'; exit;} 
    foreach($lines as $theline) { 
list($id, $shirt, $club, $price) = split('\|',$theline); 
?> 
    <tr> 
            <td><? echo $id; ?></td>  <td><? echo $shirt ?></td> 
    <td><? echo $club ?> </td>  <td>£<? echo $price ?> </td>    <td> 
<form method="post" action="addtocart.php" > 
  <input name="id" type="hidden" value="<?php echo $id; ?>"> 
        <input name="price" type="hidden" value="<?php echo $price; ?>"> 
            <input type="submit" value="Submit"> 
</form> 
    </td>     
    </tr> 
<?php 
} 
?> 
</table> 
</body> </html>

 

Cart -

 

<? 
session_start(); // start the session 

$id = $_POST["id"]; // get the id 
$price = $_POST["price"]; // get the price 

if ( !isset( $_SESSION["cart"] ) ) {  // check if the array is there 
$_SESSION["cart"] = array(); // if not create one 
} 

$_SESSION["cart"][$id] = $price; // add the data to the array 
header("location:showvariables3.php");  // redirect to the cart page 
?>

 

Order summary page -

 

<html> <head> <title>Football Shirts 'R' Us!</title> </head> 
<body bgcolor="green">
<p> <font size=12, color=white><b><u><center>Your order with Football Shirts 'R' Us</center></u></b></p></font>
<p><font size=6, color=white>Hello, your order is as follows:</p></font>
<table> <tr> </tr>
<?php 
session_start(); 
$session = $_SESSION['cart']; 
$count = count($session); 
if($count==0){ 
echo "No Items In Cart"; 
} 
else { 
?> 
<html> <head> <title>Football Shirts 'R' Us!</title> </head> 
<body bgcolor="green"> 
<p> <font size=12, color=white><b><u><center>Your order with Football Shirts 'R' Us</center></u></b></p></font> 
<p><font size=6, color=white>Hello, your order is as follows:</p></font> 
<table> 
  <tr> </tr> 
<?php 
foreach ($_SESSION['cart'] as $id => $price ) { 
$sub[]=$price; 
echo $id; 
echo " - "; 
echo $price; 
echo "<br>"; 
} 
?> 
<br> 
<tr> <td aliign='left'> Total Excluding VAT: </td> 
<td nowrap> 
£<?php 
$subtotal = array_sum($sub); 
echo number_format($subtotal, 2, '.', ','); 

$total = $subtotal /100 * 17.5 ;   
$total = $total + $subtotal; 
$total = round($total, 2); 

?> </td> 
</tr> 
<br> 
<tr> <td align='left'> Total Including VAT: </td> 
<td nowrap>£<? echo number_format($total, 2, '.', ','); ?></td> 
</tr> 
</table> 
</body> </html> 
<? 
} 
?>
</table> 
</body> </html>

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.