我在 php 中有几个嵌套循环来生成如下内容:
我的问题出在这个查询上:
$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_Category. Cost_Category_ID = $cost_current_ccr
。
如下表所示:
这在技术上是正确的,但我想要发生的情况与显示的顶部表格类似:我希望匹配的行填充右侧的数据表,但如果没有匹配,我仍然想要成本要显示的中心。
我尝试省略 $cost_inner_query
中的第一个 AND 子句,该子句填充所有成本中心,但与数据不正确匹配。
我的查询是需要更改的内容还是循环结构或其他类似数据库结构的内容?
最佳答案
正如我在评论中提到的,您的查询存在轻微缺陷。
试试这个:
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){
行开头的代码不完整,因此很难识别更多错误。
也就是说,我很高兴我不必维护采用此命名策略的数据库 - 也不必管理与“$cost_CC_results”和“$cost_CCC_results”略有不同的变量
关于php - 是我的循环逻辑错误还是我的查询错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40181138/