在foreach

时间:2016-09-04 08:25:41

标签: php mysql foreach

所以我需要从数据库中获取每个部分的部分,项目名称和小计等数据,并获得所有项目的总计。 我偶然发现下面的代码,并根据我的要求进行修改。 代码完美无缺,但我需要显示更多数据,如项目描述,数量,价格,状态等。 下面的代码工作,没有任何问题,但我需要更多的数据(描述,数量,价格等),以显示在我的表中,但我无法弄清楚如何做到这一点。 我需要显示的数据也位于projectscostbreakdown_areaname,projectscostbreakdown_itemname和projectscostbreakdown_totalcost所在的行中。

<?php
$projectsid = $_GET['projectrfpid'];
$itemdeleted = 1;
$itemfirstothers = 'OTHERS';
$query = $conn->prepare('SELECT projectscostbreakdown_id,
projectscostbreakdown_projectid,
projectscostbreakdown_areaname,
projectscostbreakdown_itemname,
projectscostbreakdown_itemdescription,
projectscostbreakdown_qty,
projectscostbreakdown_costpiece,
projectscostbreakdown_budgeted,
projectscostbreakdown_totalcost,
projectscostbreakdown_note,
projectscostbreakdown_addedby,
projectscostbreakdown_addeddate,
projectscostbreakdown_deleted,
projectscostbreakdown_lastedit,
projectscostbreakdown_lasteditby
FROM projectscostbreakdown WHERE projectscostbreakdown_projectid=:projectsid && projectscostbreakdown_deleted=:itemdeleted ORDER BY projectscostbreakdown_areaname=:itemfirstothers, projectscostbreakdown_areaname ASC, projectscostbreakdown_id DESC');
$query->bindParam(':projectsid', $projectsid, PDO::PARAM_INT);
$query->bindParam(':itemdeleted', $itemdeleted, PDO::PARAM_INT);
$query->bindParam(':itemfirstothers', $itemfirstothers, PDO::PARAM_INT);
$query->execute();

$data = array();
$data2 = array();

$numbering = 0; //for item count
$count = 1; //counter use for background color
while ( $row2 = $query->fetch() ) {

if ( empty($data[ $row2['projectscostbreakdown_areaname'] ]) ) {
    $data[ $row2['projectscostbreakdown_areaname'] ]= array();
    $data2[ $row2['projectscostbreakdown_itemdescription'] ][ $row2['projectscostbreakdown_qty'] ]= array();
}

if ( empty( $data[ $row2['projectscostbreakdown_areaname'] ][ $row2['projectscostbreakdown_itemname'] ] ) ) {
    $data[ $row2['projectscostbreakdown_areaname'] ][ $row2['projectscostbreakdown_itemname'] ] = array();
    $data2[ $row2['projectscostbreakdown_itemdescription'] ][ $row2['projectscostbreakdown_qty'] ]= array();
}
    $data[ $row2['projectscostbreakdown_areaname'] ][ $row2['projectscostbreakdown_itemname'] ][] = $row2['projectscostbreakdown_totalcost'];
    $data2[ $row2['projectscostbreakdown_itemdescription'] ][ $row2['projectscostbreakdown_qty'] ]= array();
}
print '<table width="100%" border="0"><tbody>';
$totalSum = 0;

foreach ( $data as $area => $item ) {
print '<tr style="background-color: white;"><td colspan="7" style="text-align: left;"><br /><br /><b><u>'. $area .'</u></b></td></tr>';
print '<tr style="background-color: #AAAAAA; text-align: center;">
  <td width="20%""><b>Item name</b></td>
  <td width="30%"><b>Description</b></td>
  <td width="5%"><b>Qty.</b></td>
  <td width="10%"><b>Cost/Piece</b></td>
  <td width="10%"><b>Subtotal</b></td>
  <td width="10%"><b>Budget</b></td>
  <td width="15%"><b>Note /<br / >Entered by</b></td>
</tr>';
$totalArea = 0;

foreach ( $item as $item => $totalcost ) {
    //while ( $data = $query->fetch() ) {
    $numbering++;
    $count++;
    $class = ($count%2 == 0)? 'white': '#CCCCCC';
    $sum = array_sum( $totalcost );
    print '<tr style="background-color: '.$class.'">';
    print '<td style="vertical-align: top; text-align: left;">'. $numbering .'. '. $item . '</td>';
    print '<td style="vertical-align: top; text-align: left;">';
    print_r($data2);
    print '</td>';
    print '<td style="vertical-align: top; text-align: right;"></td>';
    print '<td style="vertical-align: top; text-align: right;"></td>';
    print '<td style="vertical-align: top; text-align: right;">'. number_format($sum, 2,'.', ',') . '</td>';
    print '<td style="vertical-align: top; text-align: right;"></td>';
    print '<td style="vertical-align: top; text-align: left;"></td>';
    print '</tr>';
    $totalArea += $sum;
}
print '<tr style="background-color: lightgray; text-align: right;" ><td colspan="6">Section Total: </td><td>'. number_format($totalArea, 2,'.', ',') . '</td></tr>';
$totalSum += $totalArea;
}
print '<tr style="background-color: lightblue; text-align: right;" ><td colspan="6"><b>Grand Total: </b></td><td><b>'. number_format($totalSum, 2,'.', ',') . '</b></td></tr>';
echo '</tbody>
</table>';
?>

UPDATE 输出现在看起来像这样。 我需要在输出中包含Description和Qty,但不知道如何完成。

SECTION1
项目名称-----说明-----数量-----小计
第1项--------------------------------------------- 1000
第2项--------------------------------------------- 2000
----------------------------------科目总数:3000

第2节
项目名称-----说明-----数量-----小计
第3项--------------------------------------------- 1000
----------------------------------科目总数:1000
------------------------------------总计:4000

1 个答案:

答案 0 :(得分:2)

完整代码(更有条理,更固定的错误):

<?php

$projectsid = $_GET['projectrfpid'];
$itemdeleted = 1;
$itemfirstothers = 'OTHERS';
$query = $conn->prepare('SELECT projectscostbreakdown_id,
projectscostbreakdown_projectid,
projectscostbreakdown_areaname,
projectscostbreakdown_itemname,
projectscostbreakdown_itemdescription,
projectscostbreakdown_qty,
projectscostbreakdown_costpiece,
projectscostbreakdown_budgeted,
projectscostbreakdown_totalcost,
projectscostbreakdown_note,
projectscostbreakdown_addedby,
projectscostbreakdown_addeddate,
projectscostbreakdown_deleted,
projectscostbreakdown_lastedit,
projectscostbreakdown_lasteditby
FROM projectscostbreakdown WHERE projectscostbreakdown_projectid=:projectsid && projectscostbreakdown_deleted=:itemdeleted ORDER BY projectscostbreakdown_areaname=:itemfirstothers, projectscostbreakdown_areaname ASC, projectscostbreakdown_id DESC');
$query->bindParam(':projectsid', $projectsid, PDO::PARAM_INT);
$query->bindParam(':itemdeleted', $itemdeleted, PDO::PARAM_INT);
$query->bindParam(':itemfirstothers', $itemfirstothers, PDO::PARAM_INT);
$query->execute();

$data = array();
$data2 = array();

$numbering = 0; //for item count
$count = 1; //counter use for background color
$itemData = $query->fetchAll();
$query->execute();

while ($row2 = $query->fetch()) {
    if (empty($data[$row2['projectscostbreakdown_areaname']]) ) {
        $data[$row2['projectscostbreakdown_areaname']]= array();
        $data2[$row2['projectscostbreakdown_itemdescription']][$row2['projectscostbreakdown_qty']] = array();
    }

    if (empty($data[$row2['projectscostbreakdown_areaname']][$row2['projectscostbreakdown_itemname']])) {
        $data[$row2['projectscostbreakdown_areaname']][ $row2['projectscostbreakdown_itemname']] = array();
        $data2[$row2['projectscostbreakdown_itemdescription']][$row2['projectscostbreakdown_qty']]= array();
    }

    $data[$row2['projectscostbreakdown_areaname']][$row2['projectscostbreakdown_itemname']][] = $row2['projectscostbreakdown_totalcost'];
    $data2[$row2['projectscostbreakdown_itemdescription']][$row2['projectscostbreakdown_qty']] = array();
}

print '<table width="100%" border="0"><tbody>';
$totalSum = 0;

foreach ( $data as $area => $item ) {
    print '<tr style="background-color: white;"><td colspan="7" style="text-align: left;"><br /><br /><b><u>'. $area .'</u></b></td></tr>';
    print '<tr style="background-color: #AAAAAA; text-align: center;">
      <td width="20%""><b>Item name</b></td>
      <td width="30%"><b>Description</b></td>
      <td width="5%"><b>Qty.</b></td>
      <td width="10%"><b>Cost/Piece</b></td>
      <td width="10%"><b>Subtotal</b></td>
      <td width="10%"><b>Budget</b></td>
      <td width="15%"><b>Note /<br / >Entered by</b></td>
    </tr>';
    $totalArea = 0;

    foreach ( $item as $item => $totalcost ) {
        $count++;
        echo $numbering.' ';
        $class = ($count % 2 == 0) ? 'white' : '#CCCCCC';
        $sum = array_sum($totalcost);
        print '<tr style="background-color: '.$class.'">';
        print '<td style="vertical-align: top; text-align: left;">'. $numbering .'. '. $item . '</td>';
        print '<td style="vertical-align: top; text-align: left;">'.$itemData[$numbering]['projectscostbreakdown_itemdescription'].'</td>';
        print '<td style="vertical-align: top; text-align: right;"></td>';
        print '<td style="vertical-align: top; text-align: right;"></td>';
        print '<td style="vertical-align: top; text-align: right;">'. number_format($sum, 2,'.', ',') . '</td>';
        print '<td style="vertical-align: top; text-align: right;"></td>';
        print '<td style="vertical-align: top; text-align: left;"></td>';
        print '</tr>';
        $numbering++;
        $totalArea += $sum;
    }

    print '<tr style="background-color: lightgray; text-align: right;" ><td colspan="6">Section Total: </td><td>'. number_format($totalArea, 2,'.', ',') . '</td></tr>';
    $totalSum += $totalArea;
}
print '<tr style="background-color: lightblue; text-align: right;" ><td colspan="6"><b>Grand Total: </b></td><td><b>'. number_format($totalSum, 2,'.', ',') . '</b></td></tr>';
echo '</tbody></table>';
?>

基本上,我所做的(可能不是最好的选择,但我想不出更好的)是我在执行$itemData = $query->fetchAll();后使用$query->execute();。现在,如果我们将所有内容保持不变,则表将为空,因为指针位于最后一个元素。因此,我们必须在$query->execute();之后写一个额外的$itemData = $query->fetchAll();再次执行相同的查询。

现在,我们与您的示例基本相同,但有一个额外的变量$itemData,其中包含有关项目的所有信息。为了显示描述,数量等,现在我们可以使用$itemData[$numbering]['projectscostbreakdown_itemdescription'] $numbering代表数组的索引号。