JavaScript Text, Appendix B (Working with Arrays, Converting Between Strings and Arrays), pgs 663 - 668
Practice:
- PHP Practice
-
You will continue to work with your money exchange program.
You are going to update your ATM program to create another file, transaction.txt.
(You will continue working with your balance.txt file.)
The transaction.txt file will keep track of each transaction made by type d for deposit and w for withdrawal,
and the amount. It does not keep a balance.
- Create an assignment5 folder on your hard drive.
- The transaction.txt file is a data file with both a string
value and an amount.
d 500.00
w 100.00
- The following is php program transValues.php.
<?php
function readTrans()
{ $file = "transactions.txt";
$fp = fopen($file, "r");
if (!$fp)
{
echo "<p>Could not open the transaction file.</p>";
}
else
{
$trans = fgets($fp,2);
while (!feof ($fp))
{
$amount = fgets($fp);
if ($trans == 'w')
echo "<p>withdrawal ".$amount."</p>";
else
echo "<p>deposit ".$amount."</p>";
$trans = fgets($fp,2);
}
fclose ($fp);
}
}
?>
- Look at the code.
- The coding opens the transactions.txt file as a read only file and
checks to see if the file opened properly.
- If the file opens, a priming read is used. The fgets gets the first
2 bytes and places them in the variable $trans.
- The while loop begins to see if the end of file was read (rather than
2 bytes).
- The next value is then read into the variable amount.
- A message is displayed that displays the variable $trans with
a label describing what was read first.
- The variable $trans is tested to see if it is a 'w'.
- Either the label withdrawal or deposit is entered as a label
to the variable $amount.
- The next value in the file is read into the variable $trans.
- The while loop ends
- The file is closed.
- If you want to add data to a file that already has data, then it must be opened in the append mode.
The write mode, overwrites from the beginning of the file. The append, adds the next value after
the last value in the file.
- If you want to write more than one variable to a file, you can do it in the same statement. For instance
the statement
fwrite( $fp, "$transType $newTrans\n");
will write to the file $fp the variable $transtype, a space, the variable $newTrans, and an end of line.
Project for this assignment
- Create a new blank text file called transactions.txt and upload to your site.
- Make sure the properties have the value 666 so that you can write to the file.
- Continue working with your atm/money conversion problem.
- You will need a new button on your login page to "List Transactions".
- On both of your deposit and withdrawal pages, add the ability to write the type of transaction being made and the
amount of the transaction to your transaction.txt file.
- Create a new page that will list all transactions made.
Update your index page:
- Add links on your index page to the pages in your assignment5 folder that are to be graded:
Uploading:
- Upload your revised index.htm page to the public_html folder.
- Upload these files to your assignment5 folder (and remember to set properties to 666):
- login.htm
- conversion.htm
- currency.htm
- testBalance.htm
- testDeposit.htm
- testWithdrawal.htm
- getBalance.php
- addBalance.php
- subBalance.php
- transaction.txt
- listTransactions.htm
- transValues.php
- balance.txt
Testing uploaded pages
- Follow the checklist for grading shown on
the schedule page.
- Make any corrections for any page that does not validate or display correctly.