Hi,

Friday, November 21, 2003, 7:56:34 PM, you wrote:
SD> I could use a little help. click on the link to see my php code for a 
SD> project that creates an array with a list or items and their cost and 
SD> calculates tax by using a function called getTax() and also creates a 
SD> balance by adding the cost plus the tax in a second function called 
SD> getBalance(). I must also create a table where the cells automatically 
SD> load so the end result looks like this.
SD> http://www.areawebdirectory.net/taxtable2.txt


SD> Item          Cost          Tax          Running Total
SD> Car          10000.00    600          10600.00
SD> Pencil       1.00           .06            10601.06
SD> Book        14.95         .097         10616.98


SD> I cant seem to first be able to declare my array and succeed at naming 
SD> the key as item and the value as price. I have attempted to use 
SD> variations of the following with no success.

SD> $items = array (
SD>     0 => array ("item"=>"car","price"=>15000, "tax"=>.065 ),
SD>     1 => array ("item"=>"pencil","price"=>1, "tax"=>.065 ),
SD>     2 => array ("item"=>"book","price"=>14.95, "tax"=>.065)
SD> );    

SD> Without being able to declare the key as item and the value as price I 
SD> am not sure how to pass these items into my function and I am riddled 
SD> with error messages. I think my base problem is to either fix how I am 
SD> declaring the array so I may reference "item" and "price" as I am now 
SD> doing in my functions or learn how to change the functions accordingly.

SD> I am also pretty sure that my functions are not quite right. I am also 
SD> not sure how to link the right answers to get my table to print as above.

SD> Please note that I have included a while loop after my array just to 
SD> prove that my array is working. It is needed part of my program. I also 
SD> have given each cell in my table some text just so I can find them when 
SD> I have figured out how to create the right reference links.

SD> I am sorry that I need as much help with this. Sadly I have been at this 
SD> for almost two weeks trying to solve it.

SD> Thanks,
SD> Sara


Using this array you need something like this

<?php
$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)
);

function getTax($cost,$taxrate){
  return $cost * $taxrate;
}

echo '<table border="1">';
echo '<tr><td>Item 
number</td><td>Item</td><td>Cost</td><td>Tax</td><td>Total</td></tr>';
$total = 0;
foreach($items as $key=>$item){
  echo '<tr>';
  echo '<td>'.($key+1).'</td>';
  echo '<td>'.$item['item'].'</td>';
  echo '<td>'.$item['price'].'</td>';
  $tax = getTax($item['price'],$item['tax']);
  echo '<td>'.$tax.'</td>';
  $total += ($item['price'] + $tax);
  echo '<td>'.$total.'</td>';
  echo '</tr>';
}
echo '</table>';
?>

You will need to add number formatting but you get the idea.
I always store money as cents in the database and leave them as integers.
Just /100 to display


-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to