We help IT Professionals succeed at work.


socross asked
Last Modified: 2008-01-09

I have a database table which stores items for an invoice, there are 4 cols (where item_net is a float (4,2) and stores the amount before tax and item_vat is a true or false (1 or 0) which identifys whether vat is required on that item.

invoice_id     |    item_details     |        item_net       |      item_vat

When i pull out all items related to an invoice (invoice_id) i need to do some calculations to pull out three results.

1. total net ($net)
2. total vat ($vat)
3 Total ($net +$vat)

So i need to take each row of the $item array that i create when i pull all the values out of the database and then for each row calculate the vat if required and then add to the running total.

I have tried creating some empty vars for the net and the vat and then adding to them but i cant seem to get it working, could this be something to do with the float format in the db.

Any suggestions, example code would be much appriciated, i can post my current attempt, but i would rather see how a pro would approach it first.

Many thanks

Watch Question

can you post your code here ?


                       //EMPTY TOTAL VALUE
                        $net = '';
                        $vat = '';                        
                        //GET TOTALS
                        if(!empty($items)) {                              
                              foreach($items as $row){
                                    $net = $net + $row['item_net'];
                                    if($row['item_vat'] == 1)
                                          $vat = $vat + ($row['item_net'] * '0.175');
                              $total = $net + $vat;      
                              $net = $vat = $total = 'n/a';
Unlock this solution and get a sample of our free trial.
(No credit card required)
Why don't you use sum(total), sum(vat), sum(total+vat) when retrieving daa from the db?

well u can request to delete this Q and refund points
Closed, 500 points refunded.
EE Admin

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.