使用WHILE循环的意外结果 - PHP& MySQL的

时间:2014-05-09 12:59:58

标签: php mysql arrays

我从以下WHILE循环和SQL SELECT语句中检索意外结果:

<?php

    $sql="SELECT name, brand_id FROM brand WHERE (name IS NOT NULL OR name != '')
          AND (name != 'Hot Brands') ORDER BY name ASC";

    $result = mysql_query($sql);
    while($brand=mysql_fetch_array($result)){
?>

<?
    $bid = $brand['brand_id'];

    /*
    *   Overall array by counting total pairs
    */

    $sql1 = "SELECT COUNT(*) as cnt FROM shop_orders WHERE
             DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
             BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
             AND shop_order_action = 'Sale'";

    $result_1 = mysql_query($sql1) or die(mysql_error());
    $total = mysql_fetch_array($result_1);

    /*
    *   Overall array counting total pairs by shop location 
    */

    $sql1 = "SELECT shop_order_location, COUNT(*) AS TOTAL
              FROM shop_orders WHERE
              DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
              BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
              AND shop_order_action = 'Sale' GROUP BY shop_order_location";

    $result1 = mysql_query($sql1);
    while ($overall = mysql_fetch_array($result1)){
        ${$overall['shop_order_location'] == 'Bawtry' ? 'totalBawtry' :     $overall['shop_order_location']} = $overall['TOTAL'];
        ${$overall['shop_order_location'] == 'Chain' ? 'totalChain' : $overall['shop_order_location']} = $overall['TOTAL'];
        ${$overall['shop_order_location'] == 'Grantham' ? 'totalGrantham' : $overall['shop_order_location']} = $overall['TOTAL'];
        ${$overall['shop_order_location'] == 'Internet' ? 'totalInternet' : $overall['shop_order_location']} = $overall['TOTAL'];
    }

    /*
     *  Mens array - counting pairs of brand 
    */

    $sql2 = " SELECT shop_order_location, COUNT(*) AS TOTAL FROM shop_orders
        LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
        WHERE DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
        BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
        AND shop_order_action = 'Sale' AND category = 'Mens' GROUP BY shop_order_location";

    $result2 = mysql_query($sql2);
    while ($mens = mysql_fetch_array($result2)){
        ${$mens['shop_order_location'] == 'Bawtry' ? 'bawtryMens' : $mens['shop_order_location']} = $mens['TOTAL'];
        ${$mens['shop_order_location'] == 'Chain' ? 'chainMens' : $mens['shop_order_location']} = $mens['TOTAL'];
        ${$mens['shop_order_location'] == 'Grantham' ? 'granthamMens' : $mens['shop_order_location']} = $mens['TOTAL'];
        ${$mens['shop_order_location'] == 'Internet' ? 'internetMens' : $mens['shop_order_location']} = $mens['TOTAL'];
    }
    /*
    *   Ladies array - counting pairs of brand
    */  

    $sql3 = " SELECT shop_order_location, COUNT(*) AS TOTAL FROM shop_orders
        LEFT JOIN product ON shop_orders.shop_order_product_id = product.product_id
        WHERE DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
        BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."'
        AND shop_order_action = 'Sale' AND category = 'Ladies' GROUP BY shop_order_location";

    $result3 = mysql_query($sql3);
    while ($ladies = mysql_fetch_array($result3)){
        ${$ladies['shop_order_location'] == 'Bawtry' ? 'bawtryLadies' : $ladies['shop_order_location']} = $ladies['TOTAL'];
        ${$ladies['shop_order_location'] == 'Chain' ? 'chainLadies' : $ladies['shop_order_location']} = $ladies['TOTAL'];
        ${$ladies['shop_order_location'] == 'Grantham' ? 'granthamLadies' : $ladies['shop_order_location']} = $ladies['TOTAL'];
        ${$ladies['shop_order_location'] == 'Internet' ? 'internetLadies' : $ladies['shop_order_location']} = $ladies['TOTAL'];
    }

    /*
    *   SUM query - sales total cost
    */

    $sql4 = "SELECT SUM(shop_order_price) FROM shop_orders WHERE  
              DATE(CONCAT(shop_order_year,'-',shop_order_month,'-',shop_order_day))
              BETWEEN '".$start."' and '".$end."' AND brand = '".$bid."' AND shop_order_action = 'Sale'";

    $result4 = mysql_query($sql4) or die(mysql_error());

     while($row = mysql_fetch_array($result4)){
    ?>

    <?
        $showtotal = $row['SUM(shop_order_price)'] ;
        $showaverage = $row['SUM(shop_order_price)'] / $total['cnt'] ;
    ?>

    <tr>
        <td align="center"><strong><?=$brand['name'];?></strong></td>
        <!-- Bawtry Mens figure -->
        <td align="center"><? if (empty($bawtryMens)) {echo '0';} if (isset($bawtryMens)) {echo $bawtryMens;}?></td>
        <!-- Bawtry Ladies figure -->
        <td align="center"><? if (empty($bawtryLadies)) {echo '0';} if (isset($bawtryLadies)) {echo $bawtryLadies;}?></td>
        <!-- Overall Bawtry Figure -->
        <td align="center"><? if (empty($totalBawtry)) {echo '0';} if (isset($totalBawtry)) {echo $totalBawtry;}?></td>
        <!-- Chains mens figure -->
        <td align="center"><? if (empty($chainMens)) {echo '0';} if (isset($chainMens)) {echo $chainMens;}?></td>
        <!-- Chain ladies figure -->
        <td align="center"><? if (empty($chainLadies)) {echo '0';} if (isset($chainLadies)) {echo $chainLadies;}?></td>
        <!-- Chain overall figure -->
        <td align="center"><? if (empty($totalChain)) {echo '0';} if (isset($totalChain)) {echo $totalChain;}?></td>
        <!-- Granthams mens figure -->
        <td align="center"><? if (empty($granthamMens)) {echo '0';} if (isset($granthamMens)) {echo $granthamMens;}?></td>
        <!-- Grantham ladies figure -->
        <td align="center"><? if (empty($granthamLadies)) {echo '0';} if (isset($granthamLadies)) {echo $granthamLadies;}?></td>
        <!-- Grantham overall figure -->
        <td align="center"><? if (empty($totalGrantham)) {echo '0';} if (isset($totalGrantham)) {echo $totalGrantham;}?></td>
        <!-- Internet mens figure -->
        <td align="center"><? if (empty($internetMens)) {echo '0';} if (isset($internetMens)) {echo $internetMens;}?></td>
        <!-- Internet ladies figure -->
        <td align="center"><? if (empty($internetLadies)) {echo '0';} if (isset($internetLadies)) {echo $internetLadies;}?></td>
        <!-- Internet overall figure -->
        <td align="center"><? if (empty($totalInternet)) {echo '0';} if (isset($totalInternet)) {echo $totalInternet;}?></td>
        <!-- Total pairs -->
        <td align="center"><?=$total['cnt']?></td>
        <!-- Total value -->
        <td align="center"><strong><?=round($showtotal, 2)?></strong></td>
        <!-- Total average -->
        <td align="center"><strong><?=round($showaverage, 2)?></strong></td>
    </tr>
    <? } ?>
<? } ?>

因此该表目前如下所示:

enter image description here

从上图中可以看出,它似乎在没有Total的情况下复制前5行的相同结果,然后在表格下方打印出随机数字。

有人可以告诉我为什么它不会沿着每一行并打印出每个品牌的数字并返回到WHILE循环的顶部吗?

0 个答案:

没有答案