![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
My First PHP Script
Hey, I'm learning PHP right now through a book that I was reccomended to get on the forum (PHP and MySQL Development - Welling and Thomson) and I have just completed this little project. It isn't much, but I'm very proud of it!
Basically, it's just something that it explains in the book. It is an order form for "Bob's Auto Parts" It takes what the person orders and puts it into a .txt file with a PHP script, and then you can view all customer orders with anoter php script. I used the code from the book, but I also added a bit of my own! orderform.html [HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Bob's Auto Parts</title> </head> <body> <form action="processorder.php" method="post"> <table border="0"> <tr bgcolor="#CCCCCC"> <td width="150">Item</td> <td width="15">Quantity</td> </tr> <tr> <td>Tires</td> <td align="center"><input type="text" name="tireqty" size="3" maxlength="3" /></td> </tr> <tr> <td>Oil</td> <td align="center"><input type="text" name="oilqty" size="3" maxlength="3" /></td> </tr> <tr> <td>Spark Plugs</td> <td align="center"><input type="text" name="sparkqty" size="3" maxlength="3" /></td> </tr> <tr> <td>Shipping Address </td> <td align="center"><input type="text" name="address" size="20" maxlength="100" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="Submit Order" /></td> </tr> </table> </form> </body> </html>[/HTML] processorder.php [PHP]<?php /* Title: Bob's Auto Parts - Order Script Author: Andrew Smythe (aka: MrMan9879) Date Created: January 1st, 2006 */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Bob's Auto Parts - Order Results</title> </head> <body> <h1>Bob's Auto Parts</h1> <h2>Order Results</h2> <?php // create short variable names $tireqty = $_POST['tireqty']; $oilqty = $_POST['oilqty']; $sparkqty = $_POST['sparkqty']; $address = $_POST['address']; $date = date('H:i, F jS'); $totalqty = 0; $totalamount = 0.00; $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']; // add up all of the quantities $totalqty = $tireqty + $oilqty + $sparkqty; echo 'Items ordered: '.$totalqty.'<br />'; // make sure the person actually ordered something! if( $totalqty == 0 ) { echo '<font color="red">'; echo 'You did not order anything on the previous page!<br/>'; echo '</font>'; } else { // display the date echo '<p>Order processed on: '; echo $date; echo '</p>'; // make sure plurals are only used as needed echo '<p>Your order is as follows: </p>'; if ( $tireqty>0 && $tireqty<2 ) echo $tireqty.' tire<br />'; if ( $tireqty>0 && $tireqty>1 ) echo $tireqty.' tires<br />'; if ( $oilqty>0 && $oilqty<2) echo $oilqty.' bottle of oil<br />'; if ( $oilqty>0 && $oilqty>1) echo $oilqty.' bottles of oil<br />'; if ( $sparkqty>0 && $sparkqty<2) echo $sparkqty.' spark plugs<br />'; if ( $sparkqty>0 && $sparkqty>1) echo $sparkqty.' spark plugs<br />'; // create constants for item prices define('TIREPRICE', 100); define('OILPRICE', 10); define('SPARKPRICE', 4); // find out the prices $totalamount = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE; // calculate the subtotal echo 'Subtotal: $'.number_format($totalamount,2).'<br />'; $taxrate = 0.14; // local sales tax is 14% $totalamount = $totalamount * (1 + $taxrate); // calculate the actual total echo 'Total including tax: $'.number_format($totalamount,2).'<br />'; echo '<p>Address to ship to is '.$address.'</p>'; } // configure text to be written to orders.txt $outputstring = $date."\t -".$tireqty." tires, \t".$oilqty." bottles of oil, \t" .$sparkqty." spark plugs\t \nTotal: ".$totalamount ." \t\nShip to: ". $address."\n<hr>"; @ $fp = fopen("orders.txt", 'ab'); // open orders.txt flock($fp, LOCK_EX); // if orders.txt cannot be opened, display this if (!$fp || $totalqty == 0) { echo '<p><strong> Your order could not be processed at this time. ' .'Please try again later.</strong></p>'; exit; } // write the orders to orders.txt fwrite($fp, $outputstring, strlen($outputstring)); flock($fp, LOCK_UN); fclose($fp); echo '<p>Order written.</p>'; ?> </body> </html>[/PHP] vieworders.php [PHP]<?php /* Title: Bob's Auto Parts - Customer Orders Viewing Script Author: Andrew Smythe (aka: MrMan9879) Date Created: January 1st, 2006 */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?PHP //create short variable name $DOCUMENT_ROOT =$HTTP_SERVER_VARS['DOCUMENT_ROOT']; ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Bob's Auto Parts - Customer Orders</title> </head> <body> <h1>Bob's Auto Parts</h1> <h2>Customer Orders</h2> <?php @ $fp = fopen("orders.txt", 'r'); // open orders.txt if(!$fp) { echo '<p><strong>No orders pending.' // if orders.txt has nothing in it, display this .'Please try again later.</strong></p>'; exit; } while (!feof($fp)) // test for end of file { $order= fgets($fp, 999); // read orders from file echo $order.'<br />'; // display orders from file } fclose($fp); // close file ?> </body> </html>[/PHP] I have been reading the book for little under a week now, and I am quite happy with this project! The only thing that I'm unsure of is how to make it so that the quantities of, for example, tires isn't written to orders.txt if it is 0. Or, if there is only 1 tire being ordered, I'm not sure how to make it say 1 tire instead of tires without writing the whole $outputstring tons of times with if statements. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Sep 2004
Posts: 207
Rep Power: 5
![]() |
I would highly suggest you use a database rather than flat files.
I also prefer to inline the PHP rather than echo HTML but thats a style thing. Also you have a large section on making sure plurals are used correctly. I would solve that problem by put "Tire(s)" and save myself some time. :p
__________________
_______________________________ BlazingWolf |
|
|
|
|
|
#3 |
|
Programmer
|
Yeah, I suppose I could do "Tire(s)" but I wanted it to look better. Also, I will be using databases, like i said... I'm learning from a book, and the first half is just on PHP, and it hasn't gotten into MySQL yet. I'm only on chapter 2...
|
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
One thing: it would be safe to check the post variable exists. It is handy to put this check in a function:
[php] function getPost($v) { return (isset($_POST[$v]) ? $_POST[$v] : ""); } $tireqty = getPost('tireqty'); $oilqty = getPost('oilqty'); // ... [/php] |
|
|
|
|
|
#5 |
|
Programmer
|
Yes, I've heard of those functions. Next time I make a PHP script, I will try to add the check functions.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|