View Full Version : Array Questions

09-17-2005, 04:19 PM
I am about "three weeks old" as far as PHP knowledge goes, and I am trying to complete an assignment where I need to modify code that processes POST information from a form, to use arrays instead of the current code.

I am not sure even where to start with this. If somene could show me an example, I think I could probably get it. There are at least two places in this code where I should probably be able to use arrays, but I am just struggling to wrap my brain around it without an example. The code is below...

// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
$coolqty = $_POST['coolqty'];
$wiperqty = $_POST['wiperqty'];
$address = $_POST['address'];

<title>Bob's Auto Parts - Order Results</title>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
$date = date('H:i, jS F');

echo '<p>Order processed at ';
echo $date;
echo '</p>';

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

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

if( $totalqty == 0)
echo 'You did not order anything on the previous page!<br />';
if ( $tireqty>0 )
echo $tireqty.' tires<br />';
if ( $oilqty>0 )
echo $oilqty.' bottles of oil<br />';
if ( $sparkqty>0 )
echo $sparkqty.' spark plugs<br />';
if ( $coolqty>0 )
echo $coolqty.' coolant<br />';
if ($wiperqty>0 )
echo $wiperqty.' wiper blades<br />';

$totalamount = 0.00;

define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
define('COOLPRICE', 1);
define('WIPERPRICE', 3);

$totalamount = $tireqty * TIREPRICE
+ $oilqty * OILPRICE
+ $sparkqty * SPARKPRICE
+ $coolqty * COOLPRICE
+ $wiperqty * WIPERPRICE;

$totalamount = $totalamount + ($totalamount * .10);
$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".$coolqty." coolant\t".$wiperqty." wiper blades\t\$".$totalamount
."\t". $address."\n";

// open file for appending
@ $fp = fopen("$DOCUMENT_ROOT/folder2/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>';

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

echo '<p>Order written.</p>';
<p> Click <A HREF="vieworders2.php"><I>HERE</I></A> to see all submitted orders.</p>

09-17-2005, 05:23 PM
well the $_POST array is indeed an array ;) , and you could (should) use it as such ...currently you dereference the array with ...

// create short variable names
$tireqty = $_POST['tireqty']; ....etc

which really is of little value other than some think it makes code more readable (I think the opposite) , also leaving $_POST var's as an array would let you take advantage of all the array functions to manipulate the data e.g.

if ( $tireqty>0 )
echo $tireqty.' tires<br />';
if ( $oilqty>0 )

the above would be far neater if you used say foreach() to iterate through the $_POST array ... I wont give the code but the manual has all the examples you need.

You could also define the prices in an array , constants are fine for small collections of important constant data but not normally used for variables (you may change a price for buying in bulk)