Page 1 of 1

Array assignment

Posted: Sat Sep 17, 2005 8:00 am
by joviyach
feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I have an assignment in which I am to modify code to use arrays where it does not use them currently. The code is a PHP page that processes POST information from an order form. I am just about exactly "three weeks old" when it comes to PHP, and I am just not sure where to start with this one? Any suggestions would be greatly appreciated...

Code: Select all

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

  $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<html>
<head>
  <title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
$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 />';
}
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 />';
  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>';
  exit;
}

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

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

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Sep 17, 2005 8:09 am
by n00b Saibot

Code: Select all

$tireqty = $_POST['tireqty']; 
  $oilqty = $_POST['oilqty']; 
  $sparkqty = $_POST['sparkqty']; 
  $coolqty = $_POST['coolqty']; 
  $wiperqty = $_POST['wiperqty']; 
  $address = $_POST['address']; 

----------------------------

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 />'; 

-------------------------------

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;
These are few sections worth mention...

Posted: Sat Sep 17, 2005 8:19 am
by joviyach
I see where I could probably do something with that last section, but how do I make the $_POST values into something usable by an array?

Posted: Sat Sep 17, 2005 8:33 am
by n00b Saibot
You could use arrays as target for assigning POST Vals. ;)