You are storing orders from your online store in a flat file (a simple text file
ID: 3869721 • Letter: Y
Question
You are storing orders from your online store in a flat file (a simple text file). Given a string that represents a customer order, complete the following code to append the customer data to a file.
// Assume: all variables are defined previously
$outputstring = $date." ".$tireqty." tires ".$oilqty." oil " .$sparkqty." spark plugs $".$totalamount ." ". $address." ";
Blank 1 $fp = Blank 2 ("$DOCUMENT_ROOT/../orders/orders.txt", Blank 3 );
if (!$fp) {
echo '<p><strong> Your order could not be processed at this time. ' .'Please try again later.</strong></p></body></html>';
exit;
}
flock($fp, LOCK_EX);
Blank 4 ($fp, Blank 5 , Blank 6 );
flock($fp, LOCK_UN);
Blank 7 ($fp);
echo '<p>Order written.</p>';
Explanation / Answer
Blank 1 - @
Blank 2 - fopen // Opening the file
Blank 3 - 'ab'
Blank 4 - fwrite // writing to the file
Blank 5 - $outputstring
Blank 6 - strlen($outputstring) // length of the output string
Blank 7 - fclose // closing the file
// So rewriting the program by filling in the blanks:
$outputstring = $date." ".$tireqty." tires ".$oilqty." oil " .$sparkqty." spark plugs $".$totalamount ." ". $address." ";
@ $fp = fopen ("$DOCUMENT_ROOT/../orders/orders.txt", 'ab' );
if (!$fp) {
echo '<p><strong> Your order could not be processed at this time. ' .'Please try again later.</strong></p></body></html>';
exit;
}
flock($fp, LOCK_EX);
fwrite ($fp, outputstring , strlen(outputstring) );
flock($fp, LOCK_UN);
fclose ($fp);
echo '<p>Order written.</p>';
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.