Here’s an example from a book I was reading a while ago: PHP & MySQL Web Development by Luke Welling and Laura Thomson 4th Edition.
It’s self explanatory and you should be able to figure it out from here:
First create a blank text file such as orders.text Content will be saved over this text file. Pretty much this file will be rewritten over and over as you update content.
Here’s the order form to collect info from users/customers
order.html
[php]
PHP Training
Bob's Auto Parts
Order Results
Name |
|
Item |
Quantity
|
Tires |
|
Oil |
|
Spark Plugs |
|
Shipping Address |
|
|
|
|
How did you hear about Bob's |
Regular Customer
Referred by TV Advertisment
Referred by Phone Directory
Referred by Word of Mouth
|
[/php]
processorder.php
[php]
Bob’s Auto Parts
<?php
//old date format date
$todaysdate=date("m",time()).'-'.date("d",time()).'-'.date("y",time());
echo "Order Processed on ".$todaysdate."
"."
";
$totalqty = 0;
$subtotal=0.00;
$dicount =0.10;
$taxrate = 0.085; //NYS Sales tax
$totalamount = 0.00;
$totaldiscount=0.00;
$totalqty = $tireqty + $oilqty + $sparkqty;
//******VALIDATING THE FORM (name)************
if (empty($yourname)){
echo "
".""."Please enter your name!".""."</b"."
"."
";}
//You can use isset also see pag 43 of ebook
//i.e., if (isset($yourname){ }.
//************END VALIDATION*****************
else {
if ($totalqty>0)
echo "".$yourname."". ", your order is as follow: "."
"."
";
if ($tireqty>0)
echo number_format($tireqty,0).""." tires".""."
";
if ($oilqty > 0)
echo number_format($oilqty,0).""." bottles of oil ".""."
";
if ($sparkqty >0)
echo number_format($sparkqty,0).""." spark plugs ".""."
"."
";
echo "Total items ordered: ".number_format($totalqty)."
";
{
//declaring CONSTANTS
define ('TIREPRICE',100);
define ('OILPRICE',10);
define ('SPARKPRICE',4);
$subtotal = $tireqty * TIREPRICE + $oilqty * OILPRICE + $sparkqty * SPARKPRICE;
echo "Subtotal: $".number_format($subtotal,2)."
";
//Formulas for Discounts
if($subtotal>=500) {
$totaldiscount=($subtotal*$dicount);
echo "-10% dicount: ".""."(".number_format($totaldiscount,2).")".""."
"."
";
$totalamount = $subtotal-$totaldiscount;
echo "Total price: $".number_format($totalamount,2)."
";
$totaltax = ($totalamount * (1 + $taxrate))-$totalamount;
echo "NYS Tax: $".number_format($totaltax,2,'.',',')."
";
$grandtotal=$totaltax+$totalamount;
echo ""."Grand Total: $".number_format($grandtotal,2,'.',',').""."
"."
";
}
//DEFAULT FORMULA VALUES
elseif($subtotal<500) {
$totaltax = ($subtotal * (1 + $taxrate))-$subtotal;
echo "NYS Tax: $".number_format($totaltax,2,'.',',')."
";
$grandtotal=$totaltax+$subtotal;
echo ""."Grand Total: $".number_format($grandtotal,2,'.',',').""."
"."
";
}
if($totalqty>0)
echo "Thanks for shopping at Bob's!"."
"."
";
if($totalqty<1)
echo "".""."You did not order anything!"."".""."
"."
";
}
}
if ($totalqty >=1){ echo "";
switch($find) {
case'a':
echo "Regular Customer"."
";
break;
case 'b':
echo "Referred by TV advertismenet"."
";
break;
case 'c':
echo "Referred by Phone Directory"."
";
break;
case 'd':
echo "Referred by Word of mouth"."
";
break;
default:
echo "We Do Not Know How This Customer Found Us!"."
";
break;
}
echo "";
//prepare output: puts all variables and text into a single variable
$outputstring = $todaysdate."\t".$yourname."\t".number_format($tireqty,'','',',')." tires\t".number_format($oilqty,'','',',')." oil bottles \t".number_format($sparkqty,0,'',',')." spark plugs \t"." TOTAL:\t$".number_format($grandtotal,2,'.',',')."\t"."- ".$address."\n";
//open file for appending
@ $fp=fopen("$DOCUMENT_ROOT/../orders/orders.txt",'ab');
flock($fp,LOCK_EX);
if (!$fp) {
echo "Your file could not processed at this time.
Please try again later.
";
exit;
}
fwrite($fp,$outputstring, strlen($outputstring));
flock($fp,LOCK_UN);
fclose($fp);
echo " File written";
}
//END TEXT FILE
?>
[/php]
And Then you can view your saved content:
vieworders.php
[php]<?php
$DOCUMENT_ROOT=$_SERVER[‘DOCUMENT_ROOT’];
?>
View 2
Bob's Auto Parts
Orders Summary:
<?php
@$fp=fopen("$DOCUMENT_ROOT/…/orders/orders.txt",‘rb’);
if(!$fp) {
echo “No Orders Pending. Please Try Again Later.”;
exit;
}
while (!feof($fp)){
$order=fgets($fp,999);
echo $order."
";
}
?>
[/php]
And never be so impatient. Wait until the right person comes across your post. I can only help if it’s something I have experience with, and same goes for everyone here.
For more info you can google how to use the PHP fopen() and fwrite functions.
Good luck!