Hi. I'm a newbie in php, and I recently bought a book called PHP and MySQL Web Development by Luke Wlling and Laura Thomson. I was able to make an order form in Chapter 1, but in Chapter 2, I have to process orders so that every time I fill out an order form, the order gets stored in my orders.txt file. Here is the code I have for processorder.php:

<?php
  // create short varible names
  $tireqty = $_POST['tireqty'];
  $oilqty = $_POST['oilqty'];
  $sparkqty = $_POST['sparkqty'];
  $address = $_POST['address'];
  $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
  $date = date('H:i, jS F Y');
?>

<html>
<head>
  <title>Bob's Auto parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>

<?php
  echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";

  echo '<p>Your order is as follows: </p>';

  $totalqty = 0;
  $totalqty = $tireqty + $oilqty + $sparkqty;
  echo "Items ordered: ".$totalqty."<br />";

  if ($totalqty == 0) {

    echo "You did not order anything on the previous page!<br />";

  }

  else {

    if ($tireqty > 0) {
      echo $tireqty." tires<br />";
    }

    if ($oilqty > 0) {
      echo $oilqty." bottles of oil<br />";
    }

    if ($sparkqty > 0) {
      echo $sparkqty." spark plugs<br />";
    }
  }


  $totalamount = 0.00;

  define('TIREPRICE', 100);
  define('OILPRICE', 10);
  define('SPARKPRICE', 4);

  $totalamount = $tireqty * TIREPRICE
               + $oilqty *OILPRICE
               + $sparkqty * SPARKPRICE;
  echo "Subtotal: $".number_format($totalamount,2)."<br />";

  $taxrate = 0.10; // local sales tax is 10%
  $totalamount = $totalamount * (1 + $taxrate);
  $totalamount = number_format($totalamount, 2, '.', ' ');

  echo "<p>Total of order is $".$totalamount."</p>";
  echo "<p>Address to ship to is ".$address."</p>";

  $outputstring = $date."\t".$tireqty." tires\t".$oilqty." oil\t"
                  .$sparkqty." spark plugs\t\$".$totalamount
                  ."\t".$address."\n";

  //open file for appending
  @ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');

  flock($fp, LOCK_EX);

  if (!$fp) {
    echo "<p><strong> Your order could not be processed at this time.
          Please try again later.</strong></p></body></html>";
    exit;
  }

  fwrite($fp, $outputstring, strlen($outputstring));
  flock($fp, LOCK_UN);
  fclose($fp);

  echo "<p>Order written.</p>";
?> 
</body>
</html>

After having filled out the order form two times, this is what my orders.txt file looks like when I copy and paste what I have onto here:

01:14, 27th April 2012 1 tires 2 oil 3 spark plugs $145.20 Address
01:14, 27th April 2012 1 tires 2 oil 3 spark plugs $145.20 Address

This looks right, but for some reason, on Notepad, the data isn't shown this way. Every time I used \t, the spacing in the output is inconsistent, and even though I used \n, a new order isn't recorded on a new line in Notepad.

Is there a way for me to make it so that Notepad shows the output correctly? Thanks.

Dani AI

Generated

Short diagnosis and the fix in one line: the file on disk is using Unix-style LF line endings, while older Windows Notepad expects CRLF — Notepad++ and Word accept LF so they looked fine. Make the file use CRLF when you write it (or convert it), and tabs will still look different between editors unless the columns are padded to fixed widths. (devblogs.microsoft.com)

Why changing fopen mode didn't solve it: opening with binary/text flags affects whether the C runtime will translate newlines on Windows, but relying on that is fragile and platform-dependent. Explicitly write Windows line endings when targeting Notepad: append "\r\n" to each record (PHP_EOL returns the server OS EOL and may be "\n" on a Linux host). Example (conceptual):

$data .= "\r\n";    // force CRLF so Notepad will show a new line
fwrite($fp, $data);

Use explicit CRLF for portability rather than depending on text-mode translation. (php.net)

Tabs look inconsistent because different editors and fonts apply different tab stops. To get column alignment in plain text, emit fixed-width fields (pad each field to a chosen width) or produce CSV/excel-friendly output instead of relying on tabs. PHP’s str_pad (or building a simple format with sprintf) is a common solution: pad each column to a fixed length, then write CRLF. This avoids the variable visual width that tabs produce across editors. (php.net)

Practical checklist (what , and were driving at):

  • Confirm server OS (PHP_EOL value) or just force "\r\n".
  • Make a quick test file that contains only "line1\r\nline2\r\n" and open in Notepad.
  • If columns must line up for human reading, switch tabs to padded fields or CSV; if tabs are preferred, use a monospaced viewer/editor and accept that widths vary.
    These steps will make the orders.txt readable in Windows Notepad and keep field alignment predictable. (devblogs.microsoft.com)

Recommended Answers

All 4 Replies

Member Avatar for Member #120589

\n and \t do not do anything in simple html output. They help prettify html code but don't do anything for what you see on the screen unless using textarea

@ardav, he is talking about the contents in the file where he saved the tabs and newlines. Legitimate error. Has it got anything to do with "ab" flag/argument you're sending to the write() function there?

Member Avatar for Member #120589

Fair one.

Hi. Thanks for the help. For some reason, when I open this output file in Notepad++ instead of Notepad, the new order appears on a new line. Moreover, if I copy and paste what I have in Notepad onto Microsoft Word, I also get the new order on a new line. Is there a way for me the make it so that Notepad also shows the output correctly?

Finally, for some reason, instead a tab, I still get a single space between the quantity of tires and the quantity of oil and between the total amount and the address. The tab between the date and the quantity of tires is also different in width than the other tabs that work.

I still get this error even after changing 'ab' to 'a' in

@ $fp = fopen("$DOCUMENT_ROOT/../orders/orders.txt", 'ab');

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.