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

时间:2013-08-16 13:27:21

标签: php mysql

我有来自两个不同表的数据,我将它们连接在一起并运行查询。

我想要实现的是,我的结果将根据订单号进行分组,可能还有另外一个条件 - 如果它成功了。

我正在尝试根据我们收到库存的损坏商品生成报价。

只要订单号匹配并且所有订单号都在同一天输入,一个报价可以包含多个工作记录ID。一个警告是,如果其中一个工作记录恰好有一个项目,我们必须发送给制造商以获得估计(这是我带来的主数据库表中的标志。

现在,我想把我收到的数据显示在屏幕上,作为系统发送之前报价将包含的内容的预览。

我正在寻找的最终结果可以在我嘲笑过的图像中找到:

Mockup of wanted end result

这是我的问题:

$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++;
    }
}   

1 个答案:

答案 0 :(得分:1)

要根据订单号创建表,或发送到mfr标志,您需要跟踪上次使用的订单号和/或标志。试试这样的事情 -

$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示例 - http://phpfiddle.org/main/code/7kx-qgw