User Defined Functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joviyach
Forum Newbie
Posts: 8
Joined: Sat Sep 17, 2005 7:55 am

User Defined Functions

Post by joviyach »

I am taking a beginner PHP class at University, and I have been asked to modify the following code to use two user defined functions. I am completely lost at this point. :(

Code: Select all

<?php

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT']//."/genesis/web_pages/joviyach";
?>
<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 = $_POST['tireqty'] + $_POST['oilqty'] + $_POST['sparkqty']
+ $_POST['coolqty'] + $_POST['wiperqty'];
echo 'Items ordered: '.$totalqty.'<br />';

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

$totalamount = 0.00;

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

$totalamount = $_POST['tireqty'] * TIREPRICE + $_POST['oilqty'] * OILPRICE + $_POST['sparkqty'] * SPARKPRICE
+ $_POST['coolqty'] * COOLPRICE + $_POST['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 '.$_POST['address'].'</p>';

$outputstring = $date."\t".$_POST['tireqty']." tires \t".$_POST['oilqty']." oil\t"
.$_POST['sparkqty']." spark plugs\t".$_POST['coolqty']." coolant\t"
.$_POST['wiperqty']." wiper blades\t\$".$totalamount
."\t". $_POST['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="vieworders-2.php"><I>HERE</I></A> to see all submitted orders.</p>
<p> Click <A HREF="bobs_front_page-2.php"><I>HERE</I></A> to go to the home page.</p>
<p> Or click <A HREF="orderform-2.php"><STRONG>HERE</STRONG></A> to spend some more money.</p>
</body>
<?php

	require('footer.inc');

?>
</html>
Last edited by joviyach on Thu Oct 06, 2005 8:24 am, edited 1 time in total.
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

did they say if it the functions had to refer to anything in perticular? or can it be any 2 user defined functions as long as its used in the code somehow?

EDIT: o and you can use [ php] [ /php] (remove the spaces) tags for PHP code instead of those code tags ;)
joviyach
Forum Newbie
Posts: 8
Joined: Sat Sep 17, 2005 7:55 am

Post by joviyach »

The assignment said for example to create user defined function for the order processing portion, and then one for the writing of the info to the flat file. I am sure I am making this a lot harder than it really is, but it took me about a week to finally "get" RegEx too. :?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

i dont know the proper way of doing what youve been asked to do but if i was doing it i would look in the code for 2 blocks of code that appear twice, and then write a function to do that block of code and then include the function instead of one of the code blocks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

example of a few user defined functions:

Code: Select all

function foo() {
  // some code
}

function bar($arg1) {
  // some other code
}

function floob($arg1) {
  // even more code
}
http://php.net/language.functions
Post Reply