Jump to content

PHP help with 'purchasing items'


spiderman_2k7

Recommended Posts

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

Link to comment
Share on other sites

Hi there

 

Im trying to sort out some code for a simple ordering process.

 

I have a php form that lists the items from a .txt file with checkboxes where the user can then select items. However on the next form I cant get the form to display the itesm selected and their prices to calculate VAT and total cost etc.

 

Im getting the following error when I try to process this second form:

 

Notice: Undefined variable: plusvat in /nas/........

 

 

Heres the form I'm trying to pull the data from:

 


<form action="purchases2.php" method="post">

   
<table border="2" cellpadding="5" cellspacing="5" width="50%">
<tr> 
    <th> Image </th> 
   <th> Description  </th>   <th> Sport  </th>   <th> Price  </th> <th> Please Select </th>
</tr>
<?php

if(!($lines = file('itemlist.txt'))) 
{echo 'ERROR: Unable to open file! </body></html>'; exit;} 

foreach($lines as $theline) 
{
list($itemid, $sport, $description, $price, $image) 
= split('\|',$theline); 
echo " 
	<tr>
<td><img src='$image' width='45' height='45'></td>  <td> $description </td>   <td> $sport </td>
<td> $price  </td> <td> <input type='checkbox' name='$theline' </td>
	</tr>   "; 
}
?> 


 

 

and here is the php code i currently have that I want to retrieve the items price and description from a .txt file and manipulate the data to get VAT prices etc.

 

 


<form action="ordersummary2.php" method="post">

<?php
$total="0";
foreach ($_POST as $varname => $varvalue) {

if(!($lines = file('itemlist.txt'))) 
{echo 'ERROR: Unable to open file! </body></html>'; exit;} 

foreach($lines as $theline) 
{
	list($itemid, $sport, $description, $price, $image) = split('\|',$theline); 

if ($varname==$varvalue)
{    

echo "
<br/><br/>
<tr>
<input type='hidden' name='itemcode' value=$itemid>
<td><align='center'> Item Number: $itemid </td>
<input type='hidden' name='description' value=$description>
<td><align='center'> Item Chosen: $desciption </td>
<input type='hidden' name='price' value=$price>
<td><align='center'> Price: $price </td>
<td><align='center'><img src='$image' width='70' height='70'></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>";

?>

 

Can anyone spot any obvious mistakes I have made

 

Thanks in advnace and sorry if i have included irrelevant code!

Link to comment
Share on other sites

Sounds like you don't have access to a database, so I would create a new Order file with that information in it, then read it.

 

Or you can try to rely on sessions to hold all the info, but you would lose the data if the browser was closed.

 

Another idea is cookies, but I personally don't like them for storing this type of info.

Link to comment
Share on other sites

Well, from the notice it looks like the if

 if ($varname==$varvalue)

where you set plustvat

$plusvat=number_format($total*1.175,2);

isn't being run... so when you get to outputting it... its not set yet.  And so php is letting you know.

 

If you put an else on there that set $plusvat='';  then you would eliminate that error

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Can you just clear this up for me a little as I would like to help (I remember being new and it was tough).

 

You said "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."

 

Doesn't the page example "Hello, your order is as follows:" Show their order??

 

Sorry I guess I'm a little confused.  Post the code for the "Show their order" page and I'll help you add totals and such.

 

Is this what you are looking for?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

It appears as though you have 2 different topics going at the same time for the exact same problem.  Please mark one of them as solved and/or have a moderator remove it.

 

It is just too confusing going back and forth between the 2 of them....

 

http://www.phpfreaks.com/forums/index.php/topic,171045.0.html

 

http://www.phpfreaks.com/forums/index.php/topic,171046.0.html

Link to comment
Share on other sites

It appears as though you have 2 different topics going at the same time for the exact same problem.  Please mark one of them as solved and/or have a moderator remove it.

 

It is just too confusing going back and forth between the 2 of them....

 

http://www.phpfreaks.com/forums/index.php/topic,171045.0.html

 

http://www.phpfreaks.com/forums/index.php/topic,171046.0.html

Link to comment
Share on other sites

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!

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.