简体   繁体   English

根据条件按组格式化结果?

[英]Formatting results by groups based on conditions?

I have data from two different tables that I join together and run a query against. 我有来自两个不同表的数据,这些数据连接在一起并对其执行查询。

What I am trying to achieve is that my results will group based on the order number, and possibly one other condition - if it works out. 我想要达到的结果是,我的结果将根据订单号以及可能的其他情况(如果可行)进行分组。

I am trying to generate price quotes based on damaged items that we receive back in stock. 我正在尝试根据收到的有损库存的物品生成报价。

One quote can hold multiple work record ids so long as that the order numbers match up and those order numbers were all entered on the same day. 一个报价可以包含多个工作记录ID,只要订单号匹配并且这些订单号都在同一天输入即可。 The one caveat being if one of those work records happens to have an item that we have to send out to the manufacturer to get an estimate on (which is a flag in the main database table that I'm bringing in. 一个警告是,如果这些工作记录之一恰好有一项我们必须发送给制造商的项目以进行估算(这是我要引入的主数据库表中的一个标志)。

For now though, I would like to take the data I'm getting back and display it on the screen as a preview of what the quotes will contain prior to the system sending them out. 但就目前而言,我想取回我返回的数据并将其显示在屏幕上,以作为报价在系统发送之前包含的内容的预览。

The end result that I am looking for can be found in this image I've mocked up: 我正在寻找的最终结果可以在我模拟的这张图片中找到:

想要的最终结果的模型

Here is my query: 这是我的查询:

$sql = mysql_query("SELECT workRecord_details.orderNumber, workRecord_main.mfrEstimate, workRecord_main.id, workRecord_main.createdDate, workRecord_main.nameid, workRecord_details.dateEntered
                        FROM workRecord_main
                        INNER JOIN workRecord_details ON workRecord_main.id = workRecord_details.workRecordId
                        WHERE workRecord_main.billable = '1' AND workRecord_main.STATUS = '2' AND workRecord_details.dateEntered LIKE '2013-08-14%' ORDER BY workRecord_details.orderNumber, workRecord_main.mfrEstimate, workRecord_main.id"
                    ) or die("Can't execute: " . mysql_error());

if(mysql_num_rows($getWR) > 0){
$i = 0;
while($wrData = mysql_fetch_array($getWR))
    {
        $color_A = 'class="alt2"'; 
        $color_B = 'class="alt1"';

        $row_color = ($i % 2) ? $color_A : $color_B;        

        echo '<table width = "100%" cellspacing = "0" cellpadding = "0" border = "0">
                <tr>
                    <td class = "colheader">Work Record #</td>
                    <td class = "colheader">Company</td>
                    <td class = "colheader">Order #</td>
                    <td class = "colheader">Description</td>
                </tr>';

        echo '<tr>
                <td width= "100px" ' . $row_color . '>' . $wrData['id'] . '</td>
                <td width = "150px"' . $row_color . '>' . companyNameByNameID($wrData['nameid']) . '('.getKdaccount($wrData['nameid']).')</td>
                <td width = "100px"' . $row_color . '>' . $wrData['orderNumber'] . '</td>
                <td width = "600px"' . $row_color . '>' . $wrData['partNo'] . '</td>
            </tr>';

        echo '</table><br /><br />';


        $i++;
    }
}   

To create tables based off the order number, or send to mfr flag, you need to track the last used order number and/or flag. 要基于订单号创建表或发送至mfr标志,您需要跟踪上次使用的订单号和/或标志。 Try something like this - 试试这样的事情-

$i = 0;
$current_orderNumber = ''; //variable to track current orderNumber
$color_A = 'class="alt2"'; 
$color_B = 'class="alt1"';
while($wrData = mysql_fetch_array($getWR))
{
     // Start a new table for each orderNumber or if send to mfr is set
 if($wrData['orderNumber'] != $current_orderNumber || isset(YOUR_SEND_TO_MFR_FLAG)){
        if($i!=0){  // if not the first table, close the last table
        echo '</table><br /><br />';
    }
            // start a new table
    echo '<table width = "100%" cellspacing = "0" cellpadding = "0" border = "0">
            <tr>
                <td class = "colheader">Work Record #</td>
                <td class = "colheader">Company</td>
                <td class = "colheader">Order #</td>
                <td class = "colheader">Description</td>
            </tr>';
 $i=0; //reset the row # for the new table
 }
    $current_orderNumber = $wrData['orderNumber']; // set current orderNumber to this orderNumber

    $row_color = ($i % 2) ? $color_A : $color_B;        

    echo '<tr>
            <td width= "100px" ' . $row_color . '>' . $wrData['id'] . '</td>
            <td width = "150px"' . $row_color . '>' . $wrData['nameid'] .'</td>
            <td width = "100px"' . $row_color . '>' . $wrData['orderNumber'] . '</td>
            <td width = "600px"' . $row_color . '>' . $wrData['partNo'] . '</td>
        </tr>';

    $i++;
}
echo '</table><br /><br />'; // close the last table.

phpFiddle example - http://phpfiddle.org/main/code/7kx-qgw phpFiddle例子- http://phpfiddle.org/main/code/7kx-qgw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM