我的循环逻辑是错误还是我的查询?

时间:2016-10-21 16:08:01

标签: php html mysql loops

我在php中有几个嵌套循环来生成这样的东西:

Cost table screenshot

我的问题在于这个查询:

$cost_inner_query = "SELECT Cost_Center_Category.Cost_Center_Category_Name, 
        Cost_Centers.Cost_Center_Number AS l, Cost_Centers.Cost_Center_Name, 
        Locations.Location_Abbrev, Locations.Location, Cost_Category.Cost_Category_Name, Cost_Planning.*  
        FROM Cost_Centers 
        LEFT JOIN Cost_Center_Category 
        ON Cost_Centers.Cost_Center_Category = Cost_Center_Category.Cost_Center_Category_ID 
        LEFT JOIN Locations 
        ON Cost_Centers.Location = Locations.Location_Abbrev 
        LEFT JOIN Cost_Planning 
        ON Cost_Centers.Cost_Center_Number = Cost_Planning.Cost_Center_Number
        LEFT JOIN Cost_Category 
        ON Cost_Planning.Cost_Category = Cost_Category.Cost_Category_ID
        WHERE Cost_Centers.Location = '$cost_current_location'
        AND Cost_Category.Cost_Category_ID = $cost_current_ccr
        AND Cost_Centers.Cost_Center_Category = $cost_current_ccc_id;";

或我的循环结构:

foreach($cost_loc_results as $cost_loc){ //loop to populate data table with locations
        $cost_CC_results = array();
        echo '<tr align="right" class="cost_loc"><td class="plus_minus" width="20" align="center" bordercolor="#000000"
            style="cursor:pointer;font-size:10pt;font-weight:bold;border-style:solid;border-width:1pt">+</td>
            <td width="20" align="left" bordercolor="#000000" bgcolor="#00FFFF"
            style="font-size:8pt;border-style:solid;border-width:1pt">'.$cost_loc['Location_Abbrev'].'</td> 
            <td width="120" align="left" 
            bgcolor="#00FFFF" style="border-top-style:solid;border-top-width:1pt;border-right-style:solid;
            border-right-width:1pt;border-bottom-style:solid;border-bottom-width:1pt" >'.$cost_loc['Location'].'</td>
            <td width="50" align="left" 
            bgcolor="#00FFFF" style="border-top-style:solid;border-top-width:1pt;border-right-style:solid;
            border-right-width:1pt;border-bottom-style:solid;border-bottom-width:1pt" >'.$cost_loc['Hours'].'</td>
            <td width="50" align="left" 
            bgcolor="#00FFFF" style="border-top-style:solid;border-top-width:1pt;border-right-style:solid;
            border-right-width:1pt;border-bottom-style:solid;border-bottom-width:1pt" >'.$cost_loc['LC'].'</td>
            <td width="50" align="left" 
            bgcolor="#00FFFF" style="border-top-style:solid;border-top-width:1pt;border-right-style:solid;
            border-right-width:1pt;border-bottom-style:solid;border-bottom-width:1pt" >'.$cost_loc['FX'].'</td>';
        for($i=0; $i<10; $i++){ 
            if($i%2 == 0){
                echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$i.'" bgcolor="#e3e4e5" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
            }
            else {
                echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$i.'" bgcolor="#b2b2b2" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';

            }
        }
        echo '<td bgcolor="#FFFFFF"></td>';
        for($i=10; $i<20; $i++){
            if($i%2 == 0){
                echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$i.'" bgcolor="#e3e4e5" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
            }
            else {
                echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$i.'" bgcolor="#b2b2b2" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';

            }
        }
        foreach ($cost_category_results as $ccr){
            echo '<tr align="right" class="ccr" style="display:none;"><td class="plus_minus" width="20" align="center" bordercolor="#000000"
            style="cursor:pointer;font-size:10pt;font-weight:bold;border-style:solid;border-width:1pt">-</td>
            <td colspan="5" width="20" align="left" bordercolor="#000000" bgcolor="#a8fff3"
            style="font-size:8pt;border-style:solid;border-width:1pt">'.$ccr['Cost_Category_Name'].'</td>';
            for($i=0; $i<10; $i++){
                if($i%2 == 0){
                    echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$ccr['Cost_Category_Name'].''.$i.'" bgcolor="#e3e4e5" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                }
                else {
                    echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$ccr['Cost_Category_Name'].''.$i.'" bgcolor="#b2b2b2" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                }
            }
            echo '<td bgcolor="#FFFFFF"></td>';
            for($i=10; $i<20; $i++){
                if($i%2 == 0){
                    echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$ccr['Cost_Category_Name'].''.$i.'" bgcolor="#e3e4e5" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                }
                else {
                    echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$ccr['Cost_Category_Name'].''.$i.'" bgcolor="#b2b2b2" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                }
            } 

            foreach($cost_CCC_results as $cost_ccc){
                $cost_inner_results    = array();
                $cost_current_location = $cost_loc['Location_Abbrev'];
                $cost_current_ccc_id   = $cost_ccc['Cost_Center_Category_ID'];
                $cost_current_ccr      = $ccr['Cost_Category_ID'];
                echo '<tr align="right" class="cost_ccc" style="display:none;"><td width="20" align="center"
                    style="font-size:10pt;font-weight:bold;"></td>
                    <td colspan="5"width="150" align="left"
                    bgcolor="#FFCC00" 
                    style="border-top-style:solid;border-top-width:1pt;border-right-style:solid;
                    border-right-width:1pt;border-bottom-style:
                    solid;border-left-style:solid;border-left-width:
                    1pt;border-bottom-width:1pt">'.$cost_ccc['Cost_Center_Category_Name'].'</td>';
                for($i=0; $i<10; $i++){
                    if($i%2 == 0){
                        echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$cost_ccc['Cost_Center_Category_Name'].''.$i.'" bgcolor="#e3e4e5" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                    }
                    else {
                        echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$cost_ccc['Cost_Center_Category_Name'].''.$i.'" bgcolor="#b2b2b2" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                    }
                }
                echo '<td bgcolor="#FFFFFF"></td>';
                for($i=10; $i<20; $i++){
                    if($i%2 == 0){
                        echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$cost_ccc['Cost_Center_Category_Name'].''.$i.'" bgcolor="#e3e4e5" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                    }
                    else {
                        echo '<td id="Sum'.$cost_loc['Location_Abbrev'].''.$cost_ccc['Cost_Center_Category_Name'].''.$i.'" bgcolor="#b2b2b2" width="20" style="font-size:8pt;border-style:solid;border-width:1pt"></td>';
                    }
                }
                $cost_inner_query = "SELECT Cost_Center_Category.Cost_Center_Category_Name, 
                            Cost_Centers.Cost_Center_Number AS l, Cost_Centers.Cost_Center_Name, 
                            Locations.Location_Abbrev, Locations.Location, Cost_Category.Cost_Category_Name, Cost_Planning.*  
                            FROM Cost_Centers 
                            LEFT JOIN Cost_Center_Category 
                            ON Cost_Centers.Cost_Center_Category = Cost_Center_Category.Cost_Center_Category_ID 
                            LEFT JOIN Locations 
                            ON Cost_Centers.Location = Locations.Location_Abbrev 
                            LEFT JOIN Cost_Planning 
                            ON Cost_Centers.Cost_Center_Number = Cost_Planning.Cost_Center_Number
                            LEFT JOIN Cost_Category 
                            ON Cost_Planning.Cost_Category = Cost_Category.Cost_Category_ID
                            WHERE Cost_Centers.Location = '$cost_current_location'
                            AND Cost_Category.Cost_Category_ID = $cost_current_ccr
                            AND Cost_Centers.Cost_Center_Category = $cost_current_ccc_id;";

                $cost_inner_result = $mysqli->query($cost_inner_query); 

                while($row = $cost_inner_result->fetch_assoc()){
                    $cost_inner_results[] = $row;
                }
                foreach($cost_inner_results as $cost_inner){
                    echo '<tr class="inner" style="display:none;"><td> </td><td bordercolor="#000000" style="font-size:8pt;border-style:solid;border-width:1pt">
                    '.$cost_inner['l'].'</td>
                    <td bordercolor="#000000" colspan="4" style="font-size:8pt;border-style:solid;border-width:1pt">'.$cost_inner['Cost_Center_Name'].'</td>';
                    echo '<td name="LC'.$cost_loc['Location_Abbrev'].''.$cost_inner['Cost_Category_Name'].''.$cost_ccc['Cost_Center_Category_Name'].''.$cost_inner['Cost_Center_Name'].'2015Q1" id="LC'.$cost_loc['Location_Abbrev'].''.$cost_inner['Cost_Category_Name'].''.$cost_ccc['Cost_Center_Category_Name'].''.$cost_inner['Cost_Center_Name'].'2015Q1" bgcolor="#FFFFFF" align="right" width="20" style="font-size:8pt;border-style:solid;border-width:1pt;display:none" contenteditable="true">'.$cost_inner['2015_Q_1'].'</td>';
//past this point it just continues doing this for all <td>'s in the table.

我知道我刚刚在那里发布了很多代码,但要点是我使用嵌套循环来形成位置行,然后是成本类别行,成本中心类别行,然后是成本中心行。 (我知道,这些的命名令人困惑) 上面我当前查询的结果是,在形成Cost Center行的最内层循环(包含所有数据的行)中,显示的唯一行是与查询匹配的行,Cost_Category.Cost_Category_ID = $cost_current_ccr 。 如下表所示:

help me

这在技术上是正确的,但我想要发生的事情类似于显示的顶层表:我希望匹配的行能够向右填充数据表,但如果没有匹配,我仍然需要成本要显示的中心。

我尝试省略$cost_inner_query中的第一个AND子句,该子句填充所有成本中心但与数据不正确。

我的查询是需要更改的内容还是循环结构,还是其他类似数据库结构的东西?

2 个答案:

答案 0 :(得分:0)

正如我在评论中提到的那样,您的查询存在轻微缺陷。

请改为尝试:

SELECT x.Cost_Center_Category_Name
     , y.Cost_Center_Number 
     , y.Cost_Center_Name
     , l.Location_Abbrev
     , l.Location
     , z.Cost_Category_Name
     , p.*  
  FROM Cost_Centers y
  LEFT 
  JOIN Cost_Center_Category x
    ON c.Cost_Center_Category_ID = y.Cost_Center_Category 
  LEFT 
  JOIN Locations l
    ON l.Location_Abbrev = y.Location
  LEFT 
  JOIN Cost_Planning p
    ON p.Cost_Center_Number = y.Cost_Center_Number
  LEFT 
  JOIN Cost_Category z
    ON z.Cost_Category_ID = p.Cost_Category
   AND z.Cost_Category_ID = $cost_current_ccr
 WHERE y.Location = '$cost_current_location'
   AND y.Cost_Center_Category = $cost_current_ccc_id

由于以foreach($cost_CCC_results as $cost_ccc){行开头的代码不完整,因此很难识别出更多错误。

那就是说,我很高兴我不必使用这个命名政策来维护一个数据库 - 也不会像在&$ 39; $ cost_CC_results&#39;中那样微妙地管理变量。和&#39; $ cost_CCC_results&#39;

答案 1 :(得分:0)

我不知道我是否真的明白你想要什么,因为你的代码不完整。

但是,试着这样做:

1)在SELECT字段中添加要返回的Cost_Category.Cost_Category_ID。例如:

SELECT 
    ....
    Cost_Category.Cost_Category_ID, 
    ....
FROM Cost_Centers
....

2)在foreach($cost_CCC_results as $cost_ccc){

结束后添加此代码
if (count($cost_CCC_results) == 0) {
    echo '<tr align="right" class="cost_ccc" style="display:none;">';
    echo '<td>' . $ccr['Cost_Category_ID'] . '</td>';
    echo '</tr>';
}

这样,即使没有匹配项,也应该显示一个显示成本中心的空行。