Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

php - Combine results for SQL query

I am in a bit of a pinch. I am using WHMCS and building a custom report but am running into trouble with one of my queries. Here is an overview:

I have 2 tables; tblinvoices which has the subtotal, tax, total, etc. of the invoice and tblinvoiceitems which has the individual line items that appear on the invoice. I am wanting to run a query that returns all of the individual line items and their price which I have been able to do. I run into problems when I do 'GROUP BY' and group the results by invoice numder, then it only returns the first line item for each invoice. I want to group them by invoice number so it only has 1 line in the report. Here is my query:

$query = "SELECT date_format(tblinvoices.datepaid,'%m-%d-%Y') AS datepaid,
    tblinvoices.userid,
    tblinvoices.id,
    tblinvoices.subtotal,
    tblinvoices.credit,
    tblinvoices.tax,
    tblinvoices.tax2,
    tblinvoices.total,
    tblinvoices.taxrate,
    tblinvoices.taxrate2,
    tblinvoiceitems.description,
    tblinvoiceitems.amount,
    tblinvoices.status,
    FROM tblinvoices
    INNER JOIN tblinvoiceitems ON tblinvoices.id = tblinvoiceitems.invoiceid
    GROUP BY tblinvoices.id";
$result = mysql_query($query);

# Math Operations
$statement = array();
$count = 0;

if ($result !== false) {
    while ($data = mysql_fetch_object($result)) {
        $invoiceid = $data->id;
        $datepaid = $data->datepaid;
        $description = $data->description;
        $item_amount = $data->item_amount;
        $subtotal = $data->subtotal;
        $credit = $data->credit;
        $tax = $data->tax;
        $tax2 = $data->tax2;
        $total = $data->total;

        if ($export != true) {
            $client_link = '<a href=clientssummary.php?userid='.$data->userid.'>'.$data->userid;
            $invoice_link = '<a href=invoices.php?action=edit&id='.$data->id.'>'.$data->id;
        }
        else {
            $client_link = $data->userid;
            $invoice_link = $data->id;
        }
        if (strpos($description, 'Setup') !== false) {
            $setup = $item_amount;
        }
        else {
            $setup = 0;
        }
        if (strpos($description, 'Addon') !== false) {
            $addon = $item_amount;
        }
        else {
            $addon = 0;
        }
        if (strpos($description, 'Tax Guide: No => Yes') !== false) {
            $taxguide = $item_amount;
        }
        else {
            $taxguide = 0;
        }
        if (strpos($description, 'Reading Rack Bundle') !== false) {
            $reading = $item_amount;
        }
        else {
            $reading = 0;
        }
        if (strpos($description, 'Toolkit Bundle') !== false) {
            $toolkit = $item_amount;
        }
        else {
            $toolkit = 0;
        }
        $hosting = $subtotal - $setup - $addon - $taxguide - $reading - $toolkit;

        $statement[$invoiceid."_".$count] = array($datepaid,$client_link,$promo,$dtn,$company,$state,$invoice_link,$setup,$addon,$taxguide,$reading,$toolkit,$hosting,$subtotal,$credit,$tax+$tax2,$total);
        $count++;
    }
}

foreach ($headings AS $k=>$v) {
    $reportdata["tableheadings"][] = $v;
}

//ksort($statement);
foreach ($statement AS $invoiceid=>$entry) {
    $reportdata["tablevalues"][] = array(
        $entry[0], // datepaid
        $entry[1], // clientid
        $entry[2], // promocode
        $entry[3], // dtn
        $entry[4], // companyname
        $entry[5], // state
        $entry[6], // invoiceid
        formatCurrency($entry[7]), // setup
        formatCurrency($entry[8]), // addon
        formatCurrency($entry[9]), // taxguide
        formatCurrency($entry[10]), // reading
        formatCurrency($entry[11]), // toolkit
        formatCurrency($entry[12]), // hosting
        formatCurrency($entry[13]), // subtotal
        formatCurrency($entry[14]), // credit
        formatCurrency($entry[15]), // tax
        formatCurrency($entry[16]) // total
    );
}
mysql_free_result($result);

I will be happy to provide any additional information/code if it helps. I though this might be a more general type question...thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With the result you are describing your result set would look like:

userid | id | subtotal | credit | tax | status | desc | amount
______________________________________________________________

     1 |  1 |       20 |      0 |  .7 |      1 | item1|     10
     1 |  1 |       20 |      0 |  .7 |      1 | item2|     10
     1 |  2 |       30 |      0 | 2.1 |      1 | item3|     15
     1 |  2 |       30 |      0 | 2.1 |      1 | item3|     15
     1 |  3 |       10 |      0 |  .7 |      0 | item1|     10
     1 |  4 |        1 |      0 | .07 |      0 | item4|      1

Is this what you are looking for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...