Jump to content

Explode() Function Error?


sunicani

Recommended Posts

hi experts,I got confused when displayed orders information via the following code,

--orderprocess.php<!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=gb2312" /><title>Bob's Auto Parts - Order Results.</title></head><body><h1>Bob's Auto Parts.</h1><h2>Order Results.</h2><?php //create short variable name $tireqty=$HTTP_POST_VARS['tireqty']; $oilqty=$HTTP_POST_VARS['oilqty']; $sparkqty=$HTTP_POST_VARS['sparkqty']; $address=$HTTP_POST_VARS['address']; //$date=$HTTP_POST_VARS['date']; -- date should not be defined $date=date('H:i,jS F');//very important to added! //echo order details echo '<p>Order processed at '; echo date ('H:i, jS F'); echo '</p>'; echo '<p>Your order is as follows: </p>'; echo $tireqty.' tires<br />'; echo $oilqty.' bottles of oil<br />'; echo $sparkqty.' sparks<br />'; //echo total amount $totalqty = 0; $totalqty = $tireqty + $oilqty + $sparkqty; echo 'Items ordered: '.$totalqty.'<br />'; //echo address echo 'address shipped to: '.$address.'<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,3).'<br />'; $taxrate = 0.10; // local sales tax is 10% $totalamount = $totalamount * (1 + $taxrate); echo 'Total including tax: $'.number_format($totalamount,2).'<br />'; //write to a file - orders.txt $fp = fopen('orders.txt', 'a', 1); //$fp = fopen("$DOCUMENT_ROOT/../orders.txt","a",1); //note $fp = fopen('orders.txt', 'a', 1); and, find the differences! flock($fp, LOCK_EX); // lock the file for writing fwrite($fp,$outputstring = $date."\t".$tireqty.' tires'."\t".$oilqty.' oils'."\t".$sparkqty.' sparks'."\t"."$".$totalamount."\t".$address."\n"); flock($fp, LOCK_UN); //release write lock ?></body></html>
---vieworders.php
<?php //create short variable name $orders = file('orders.txt');?> <html><head> <title>Bob's Auto Parts - Customer Orders</title></head><body><h1>Bob's Auto Parts</h1><h2>Customer Orders</h2><?php //Read in the entire file. //Each order becomes an element in the array $orders = file('orders.txt'); //$orders= file("$DOCUMENT_ROOT/../orders/orders.txt"); // count the number of orders in the array $number_of_orders = count($orders); if ($number_of_orders == 0) { echo '<p><strong>No orders pending. Please try again later.</strong></p>'; } else { echo "<table border=1>\n"; echo '<tr><th bgcolor="#CCCCFF">Order Date</th> <th bgcolor="#CCCCFF">Tires</th> <th bgcolor="#CCCCFF">Oil</th> <th bgcolor="#CCCCFF">Spark Plugs</th> <th bgcolor="#CCCCFF">Total</th> <th bgcolor="#CCCCFF">Address</th> <tr>'; for ($i=0; $i<$number_of_orders; $i++) { //split up each line $line = explode( "\t", $orders[$i] ); // keep only the number of items ordered $line[1] = intval( $line[1] ); $line[2] = intval( $line[2] ); $line[3] = intval( $line[3] ); // output each order echo '<tr> <td>$line[0]</td> <td align="right">$line[1]</td> <td align="right">$line[2]</td> <td align="right">$line[3]</td> <td align="right">$line[4]</td> <td align="right">$line[5]</td> </tr>'; } echo "</table>"; }?></body></html>Well, after clicking http://localhost/vieworders.php, I got the following results,
Date Tires Oil Spark Plugs Total Address$line[0] $line[1] $line[2] $line[3] $line[4] $line[5]$line[0] $line[1] $line[2] $line[3] $line[4] $line[5]
why I can't get the exact figures(order quantities) as hoped?thanks a lot, every experts
Link to comment
Share on other sites

Because single quotes are literal, i.e., variable syntax expressed inside them will not be interpreted. Use double quotes.

Link to comment
Share on other sites

You should use double quotes, or else, as I said, the variables as written ($line[0] etc.) will not be interpreted.

echo "<tr><td>$line[0]</td><td align=\"right\">$line[1]</td><td align=\"right\">$line[2]</td><td align=\"right\">$line[3]</td><td align=\"right\">$line[4]</td><td align=\"right\">$line[5]</td></tr>";

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...