clay1 Posted March 22, 2010 Share Posted March 22, 2010 I have a script that generates an invoice. Clients are billed for different products. Currently the invoice is for one client. I am trying to modify it so that it will reflect the bill for multiple clients. Right now I have some variables that I use to count the quantity of products and then display that on the invoice For example: $product1qty $product2qty And then some if statements like: 'if $price == $20 $product1qty++' Later in the script I have an array of the product quantities and their descriptions which is looped through and printed to a pdf I am trying to add another loop so that multiple clients will be printed on 1 invoice. I've got an array holding my clients ie: $clientsarray("Mike", "Joe","Bill") What I am having trouble with is resetting the counter on the products variables after each client is looped through I tried foreach ($clients as client){ } Around my code that creates the invoice but I don't get the correct quantities Quote Link to comment https://forums.phpfreaks.com/topic/196142-counting-quantities/ Share on other sites More sharing options...
ignace Posted March 22, 2010 Share Posted March 22, 2010 A quick Google search turned up: http://www.devshed.com/c/a/PHP/Invoice-Management-in-a-PHP-Invoicing-System/3/ Quote Link to comment https://forums.phpfreaks.com/topic/196142-counting-quantities/#findComment-1030076 Share on other sites More sharing options...
Psycho Posted March 22, 2010 Share Posted March 22, 2010 Just check whatever variables you are using in the process of the invoice and reset all of them on each foreach loop of clients. Quote Link to comment https://forums.phpfreaks.com/topic/196142-counting-quantities/#findComment-1030085 Share on other sites More sharing options...
clay1 Posted March 22, 2010 Author Share Posted March 22, 2010 I fixed this problem by rewriting my sql queries to do one client at a time However I am at a loss as to how to get my totals to work correctly now and where in or outside the loop they should be calculated How do I add up a total then add to it on the next iteration rather than resetting it each time? Quote Link to comment https://forums.phpfreaks.com/topic/196142-counting-quantities/#findComment-1030088 Share on other sites More sharing options...
clay1 Posted March 22, 2010 Author Share Posted March 22, 2010 I think I have it working Quote Link to comment https://forums.phpfreaks.com/topic/196142-counting-quantities/#findComment-1030095 Share on other sites More sharing options...
Psycho Posted March 22, 2010 Share Posted March 22, 2010 I fixed this problem by rewriting my sql queries to do one client at a time Why would you do that? One query is ALWAYS better than running multiple queries. Here is a quick example of how you could produce a multi-company report from a single query <?php $query = "SELECT * FROM orders WHERE company IN ('company1', 'company2', 'company3') ORDER BY company, order_date"; $result = mysql_query($query); $currentCompany = ''; $grandTotal = 0; while($order = mysql_fetch_assoc($result)) { //Detemine if this is a new company if ($order['company'] != $$currentCompany) { //Display last company sub total if ($currentCompany!='') { echo "<b>Subtotal for {$currentCompany}: $companySubTotal</b><br /><br />\n"; } //Reset sub total vars $companySubTotal = 0; //Display company header echo "<h1>Subtotal for {$currentCompany}: $companySubTotal</h1><\n"; } $companySubTotal += $order['order_total']; $grandTotal += $order['order_total']; echo "{$order['order_date']} : {$order['product_name']} : {$order['order_total']}<br />\n"; } //Display last company subtotal echo "<b>Subtotal for {$currentCompany}: $companySubTotal</b><br /><br />\n"; //Display grad total for all companies echo "<b>Grand total: $grandTotal</b>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/196142-counting-quantities/#findComment-1030099 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.