
spiderman_2k7
New Members-
Posts
7 -
Joined
-
Last visited
Never
Profile Information
-
Gender
Not Telling
spiderman_2k7's Achievements

Newbie (1/5)
0
Reputation
-
Problem using sessions - how to allow multiple 'purchases'?
spiderman_2k7 replied to spiderman_2k7's topic in PHP Coding Help
How would I go about doing either of these? Could you edit it for me please, or provide further details? Many thanks -
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
-
Sorry about this, especially cunoodle2 as you offered to provide help. I wasn't actually aware that my friend/soon to be partner on this project had posted on PHP Freaks as well! Just spoken to him on the phone.. should really communicate better. Anyone reading this thread, could you please IGNORE any posts made from 'starkytwo', or could a mod or admin please delete his posts as we'll just use my posts for reference now!
-
Sorry, when I run this I get the following error: Your order with Football Shirts 'R' Us Hello, your order is as follows: Notice: Undefined variable: plusvat in /public_html/footballshirts/showvariables3.php on line 42 Total Excluding VAT: 0 Total Including VAT:
-
Hi. Thanks for the reply. Yes, sorry, i've not explained this too well but the 'order' page will display the item number, the shirt type and the price. The order page code I have is as follows: <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 $total="0"; foreach ($_POST as $varname => $varvalue) { 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); if ($varname==$varvalue) { echo " <br/><br/> <tr> <input type='hidden' name='itemcode' value=$id> <td><align='center'> Item Number: $id </td> <input type='hidden' name='description' value=$shirt> <td><align='center'> Item Chosen: $shirt </td> <input type='hidden' name='price' value=$club> <td><align='center'> Price: $club </td> <input type='hidden' name='price' value=$price> <td><align='center'> Price: $price </td> </tr>"; $total=$total+$price; $plusvat=number_format($total*1.175,2); }}} echo " <br> <tr> <td aliign='left'> Total Excluding VAT: </td> <td> $total </td> </tr> <br> <tr> <td align='left'> Total Including VAT: </td> <td> $plusvat </td> </tr>"; ?> </table> </body> </html> So basically, this page needs to show the mentioned shirt details, the price excluding VAT, the price with VAT, and then the total price. Thanks, I appreciate your patience with a novice!
-
Thanks for the reply. The data is stored in flat file. I want to keep it all to PHP and not use cookies or anything else for this mini project. Is there not specific code or tutorials that can pull the 'items' onto a new page (like I have done), but so I can customize this page so that the data appears in the form of an invoice listing the items, and then calculating the price with and without VAT? Thanks
-
Hi. I am very new to PHP and very much a novice, so I would appreciate if you could take this into mind! I'm currently making a small online 'store' on Football Shirts. The project won't be published it will just be used in my personal space for my own benefit and learning in my spare time. I have created a PHP page as follows: <html> <head> <title>Football Shirts 'R' Us</title> </head> <body> <body bgcolor="#339900"> <p> <font size=16, color=white><b><u><center>Football Shirts</center></u></b></p> <form action="url here" 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); echo " <tr> <td>$id</td> <td> $shirt </td> <td> $club </td> <td> $price </td> <td><center><input type='checkbox' name='$theline'></center></td> </tr> "; } ?> </table> <form> <p><input type="submit" value="Submit"> </form> <form> <input type="reset" value="Reset"> </form> </body> </html> Note: where it says <form action="url here" i've removed my local working URL for this post. As you can see, I am pulling the football shirt data from a read file (read_shirts.txt). Once the 'user' has selected which shirts they wish to 'purchase' there is a submit button, that, when clicked, opens a new page that lists something like the following: Please confirm your order Hello, your order is as follows: 004||Manchester_United|39_99 005||Newcastle|39_99 It is pulling the item number, the shirt name and the price. However, now I want to show the items the 'user' has selected to 'purchase' and then show an invoice with the items, the prices, the price excluding tax, the price including tax, and then a total price. Could anyone help me out with this, or point me in the right direction? Thanks