Jump to content

Counting quantities


clay1

Recommended Posts

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

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.