条款太多了?

时间:2011-10-25 13:49:10

标签: php while-loop

我编写了一个非常混乱的函数,它根据与其他字段匹配的约束返回3个字段。根据最终结果它有3个结果它将返回结果2.

public function ListRoomTotals($room, $date, $dateTo = null) {

    // If dateTo hasn't been set, make it now

    if(!isset($dateTo) or $dateTo == "") {

        $dateTo = $date;

    }

    // Return an array with each bundle number and the quantity for each day

    $scanner = $this->GetScannerNumber($room);

    $sql = "SELECT * FROM `scanners` WHERE `Scanner` IN (";

        foreach($scanner as $x) {

            $sql .= $x . ",";

        }

    $sql .= "0)

            AND `Date` BETWEEN '" . $date . "' AND '" . $dateTo . "'

            GROUP BY `KordNo`, `BundleNumber`;";

    $result = mysql_query($sql);

    while($row = mysql_fetch_array($result)) {

        $sql = "SELECT `BundleReference`, `QtyIssued`, `WorksOrder`,

                FROM `ArchiveBundle`

                WHERE `KordNo` = '" . $row['KordNo'] . "'

                AND `BundleNumber` = '" . $row['BundleNumber'] . "';";

        $result2 = mysql_query($sql);

        while($row = mysql_fetch_array($result2)) {

        $sql = "SELECT `ArchiveBundle`.`QtyIssued`, `ArchiveBundle`.`WorksOrder`,

                        `ArchiveBundle`.`Colour`, `ArchiveBundle`.`Size`

                FROM `ArchiveBundle`

                WHERE `ArchiveBundle`.`KordNo` = '" . $x['KordNo'] . "' AND

                  `ArchiveBundle`.`BundleNumber` = '" . $x['BundleNumber'] . "';";

        $result3 = mysql_query($sql);
                        $row = mysql_fetch_row($result3);   


           // Now we need to query what the product group is

        $sql = "SELECT `Stock`.`ProductGroup`

            FROM `Stock`, `TWOrder`

            WHERE `TWOrder`.`WorksOrderNumber` = '" . $row[1] . "' AND

                `TWOrder`.`Colour` = '" . $row[2] . "' AND

                `TWOrder`.`Size` = '" . $row[3] . "' AND

                `Stock`.`ProductCode` = `TWOrder`.`Product`;";



        $result4 = mysql_query($sql);

        $row2 = mysql_fetch_row($result4);

        if(in_array($row2[0], array(50, 100, 150, 300 , 350 , 925 ,930, 940)))

        {


        while($row = mysql_fetch_array($result2)) {

            if($row[0] != "") {

                $final[] = $row;

            } else {

                $final[] = array("Can't find bundle number", "N/A");

            }

        }

    }

    return $final;

   }

我在下面的页面上使用了这个函数的for循环。

 $result = $scanner->ListRoomTotals($_GET['room'], $_GET['date']);

            $total = 0;

            foreach($result as $x) {

                $content .= "<tr>

                    <td>" . $x[0] . "</td>

                    <td>" . $x[1] . "</td>

                </tr>";
                                    $total += $x[1];

            }

            $weeklyTotal += $total;

            $content .= "<tr><td>Total Pairage:</td><td>" . $total . "                    </td></tr>

                    <tr><td>Total Dozens:</td><td>" .      number_format($total/12,2) . "</td></tr></table>

        </td>

我知道它写得很糟糕,因为它是没有正确返回的主要原因。有没有人有任何建议可能分开它。我在php上非常基础,所以我对它的理解很差。谢谢。

2 个答案:

答案 0 :(得分:0)

如果您的代码删除了大部分不需要的空白区域,我做了一些小改动。我的建议是将函数ListRoomTotals分解为更小的函数。例如,getScanners可以移动到它自己的功能中。因此,不是基于结果嵌套函数,而是将每个子任务分解为更小的函数。在您的查询中,您还有SELECT *。如果您只需要两个或三个字段,这也是一个性能问题。你也可以重构你的查询。

public function ListRoomTotals($room, $date, $dateTo = null) {
    // If dateTo hasn't been set, make it now
    if(!isset($dateTo) || $dateTo == "") { //<-- Replaced or with ||
        $dateTo = $date;
    }

    // Return an array with each bundle number and the quantity for each day
    $scanner = $this->GetScannerNumber($room);
    $sql = "SELECT * FROM `scanners` WHERE `Scanner` IN (".implode(',', $scanner);

    // replace foreach loop with implode
    //foreach($scanner as $x) {
    //  $sql .= $x . ",";
    //}

    $sql .= "0)
    AND `Date` BETWEEN '" . $date . "' AND '" . $dateTo . "'
    GROUP BY `KordNo`, `BundleNumber`;";

    $result = mysql_query($sql);

    while($row = mysql_fetch_array($result)) {
        $sql = "SELECT `BundleReference`, `QtyIssued`, `WorksOrder`,
        FROM `ArchiveBundle`
        WHERE `KordNo` = '" . $row['KordNo'] . "'
        AND `BundleNumber` = '" . $row['BundleNumber'] . "';";

        $result2 = mysql_query($sql);

        while($row = mysql_fetch_array($result2)) {     
            $sql = "SELECT `ArchiveBundle`.`QtyIssued`, `ArchiveBundle`.`WorksOrder`,
            `ArchiveBundle`.`Colour`, `ArchiveBundle`.`Size`
            FROM `ArchiveBundle`
            WHERE `ArchiveBundle`.`KordNo` = '" . $x['KordNo'] . "' AND
            `ArchiveBundle`.`BundleNumber` = '" . $x['BundleNumber'] . "';";

            $result3 = mysql_query($sql);
            $row     = mysql_fetch_row($result3);

            // Now we need to query what the product group is
            $sql = "SELECT `Stock`.`ProductGroup`
            FROM `Stock`, `TWOrder`
            WHERE `TWOrder`.`WorksOrderNumber` = '" . $row[1] . "' AND
            `TWOrder`.`Colour` = '" . $row[2] . "' AND
            `TWOrder`.`Size` = '" . $row[3] . "' AND
            `Stock`.`ProductCode` = `TWOrder`.`Product`;";

            $result4 = mysql_query($sql);
            $row2    = mysql_fetch_row($result4);

            if(in_array($row2[0], array(50, 100, 150, 300 , 350 , 925 ,930, 940))) {
                while($row = mysql_fetch_array($result2)) {
                    if($row[0] != "") {
                        $final[] = $row;
                    } else {
                        $final[] = array("Can't find bundle number", "N/A");
                    }
                }
            }
            return $final;
        }
    }
}

$result = $scanner->ListRoomTotals($_GET['room'], $_GET['date']);
    $total = 0;

    foreach($result as $x) {
        $content .= "<tr>
            <td>" . $x[0] . "</td>
            <td>" . $x[1] . "</td>
        </tr>";
        $total += $x[1];
    }
    $weeklyTotal += $total;
    $content .= "<tr><td>Total Pairage:</td><td>".$total."                    </td></tr>
            <tr><td>Total Dozens:</td><td>".number_format($total/12,2)."</td></tr></table>
</td>"; //<-- adding the closing double quote

更新:

不确定定义的位置:

$x['KordNo']
$x['BundleNumber']

你的意思是

$row['KordNo']
$row['BundleNumber']

答案 1 :(得分:0)

您的代码格式化使得很难看到正在发生的事情。

清理后,您似乎正在执行一系列SQL查询,每个查询都取决于前一个查询。使用JOIN执行单个查询更有意义,以访问所需的数据。

此外,您还要进行两次查询

 $sql = "SELECT `BundleReference`, `QtyIssued`, `WorksOrder`,
         FROM `ArchiveBundle`
         WHERE `KordNo` = '" . $row['KordNo'] . "'
           AND `BundleNumber` = '" . $row['BundleNumber'] . "';";

 $sql = "SELECT `ArchiveBundle`.`QtyIssued`, `ArchiveBundle`.`WorksOrder`,
                `ArchiveBundle`.`Colour`, `ArchiveBundle`.`Size`
         FROM `ArchiveBundle`
         WHERE `ArchiveBundle`.`KordNo` = '" . $x['KordNo'] . "' 
           AND `ArchiveBundle`.`BundleNumber` = '" . $x['BundleNumber'] . "';";

这似乎是多余的。

此外,您在这里指的是$x,但$x只是一个索引变量,用于生成以逗号分隔的扫描程序列表;我怀疑这就是你的意思。

我的建议是:弄清楚你要从数据库中提取哪些信息,为这个数据请求找出正确的SQL,然后从那里开始。

从您的代码中,我认为您想要的查询是:

   SELECT `scanners`.*, ArchiveBundle`.`QtyIssued`, `Stock`.`ProductGroup`
    FROM `Stock` INNER JOIN `TWOrder` ON 
                    `Stock`.`ProductCode` = `TWOrder`.`Product`
                 INNER JOIN `ArchiveBundle` ON
                    `TWOrder`.`WorksOrderNumber` = `ArchiveBundle`.`WorksOrder` AND
                    `TWOrder`.`Colour` = `ArchiveBundle`.`Colour` AND
                    `TWOrder`.`Size` = `ArchiveBundle`.`Size`
                 INNER JOIN `scanners` ON
                    `ArchiveBundle`.`KordNo` = `scanners`.`KordNo` AND 
                    `ArchiveBundle`.`BundleNumber` = `scanners`.`BundleNumber`
    WHERE `scanners`.`Scanners` IN (?...?)";