RE: [PHP] fighting with functions

From: Date: Fri, 21 Nov 2003 07:52:40 +0000
Subject: RE: [PHP] fighting with functions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-170525@lists.php.net to get a copy of this message
Hi, Little semantic correction, the function "GetBalance($item)" should be called "GetFullPrice($item)" because it returns full price of the item, but the "$balance" variable seems to be partial sum of the current item and previous items; Bronislv Klucka. > -----Original Message----- > From: Bronislav Klucka [mailto:Bronislav.Klucka@pro2-soft.com] > Sent: Friday, November 21, 2003 8:31 AM > To: PHP Konference; Sara Daugherty > Subject: RE: [PHP] fighting with functions > > > I'm not sure, if I understood, but try this: > > <?php > $items = array ( > 0 => array ("item"=>"car","price"=>10000, > "tax"=>0.06 ), > 1 => array ("item"=>"pencil","price"=>1, > "tax"=>0.06 ), > 2 => array ("item"=>"book","price"=>14.95, > "tax"=>0.065) > ); > > function GetTax($item){ > return $item["price"]*$item["tax"]; > } > > function GetBalance($item){ > return $item["price"]+GetTax($item); > } > > function ShowPrice($price){ > return sprintf ("%.2f", $price); > } > > $balance=0; > if(sizeof($items)) foreach($items as $item){ > $balance+=GetBalance($item); > echo > > $item["item"]."&nbsp;&nbsp;&nbsp;".ShowPrice($item["price"])."&nbs > p;&nbsp;&n > bsp;".ShowPrice(GetTax($item))."&nbsp;&nbsp;&nbsp;".ShowPrice($bal > ance)."<br > >\n"; > } > ?> > > > > -----Original Message----- > > From: Sara Daugherty [mailto:sara@areawebdirectory.net] > > Sent: Friday, November 21, 2003 10:57 AM > > To: php_king@yahoogroups.com > > Cc: php-beginner@egroups.com; php-list@yahoogroups.com; PHP LIST > > ; php-lords@yahoogroups.com; php-newbies@yahoogroups.com; > > mike@areawebdirectory.net; Michael Roush; Chris Hubbard > > Subject: [PHP] fighting with functions > > > > > > I could use a little help. click on the link to see my php code for a > > project that creates an array with a list or items and their cost and > > calculates tax by using a function called getTax() and also creates a > > balance by adding the cost plus the tax in a second function called > > getBalance(). I must also create a table where the cells automatically > > load so the end result looks like this. > > http://www.areawebdirectory.net/taxtable2.txt > > > > > > Item Cost Tax Running Total > > Car 10000.00 600 10600.00 > > Pencil 1.00 .06 10601.06 > > Book 14.95 .097 10616.98 > > > > > > I cant seem to first be able to declare my array and succeed at naming > > the key as item and the value as price. I have attempted to use > > variations of the following with no success. > > > > $items = array ( > > 0 => array ("item"=>"car","price"=>15000, > > "tax"=>.065 ), > > 1 => array ("item"=>"pencil","price"=>1, > > "tax"=>.065 ), > > 2 => array ("item"=>"book","price"=>14.95, > > "tax"=>.065) > > ); > > > > Without being able to declare the key as item and the value as price I > > am not sure how to pass these items into my function and I am riddled > > with error messages. I think my base problem is to either fix how I am > > declaring the array so I may reference "item" and "price" as I am now > > doing in my functions or learn how to change the functions accordingly. > > > > I am also pretty sure that my functions are not quite right. I am also > > not sure how to link the right answers to get my table to print > as above. > > > > Please note that I have included a while loop after my array just to > > prove that my array is working. It is needed part of my program. I also > > have given each cell in my table some text just so I can find them when > > I have figured out how to create the right reference links. > > > > I am sorry that I need as much help with this. Sadly I have been at this > > for almost two weeks trying to solve it. > > > > Thanks, > > Sara > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#170525) next »