PHP通过循环变量从关联数组中获取值

时间:2017-09-11 14:22:51

标签: php html sql

我不确定我的问题是对的。

我制作了一个html表,并用数据库中的测试数据填充它。

表格中的图片:https://imgur.com/a/UVH1e

这是我的表:

<table class="table table-striped table-bordered turnover_list">
<thead>
    <tr class="sorting_1">
        <th>Company</th>
        <th>Year</th>
        <th>January</th>
        <th>February</th>
        <th>March</th>
        <th>April</th>
        <th>May</th>
        <th>June</th>
        <th>July</th>
        <th>August</th>
        <th>September</th>
        <th>October</th>
        <th>November</th>
        <th>December</th>
        <th>Total</th>
    </tr>
</thead>
<tbody>

    <?php

        $currentYear = date('Y');

        $check = "SELECT * FROM turnover WHERE year = '" . $currentYear . "'";
        $result = mysql_query($check) or die(mysql_error());

        foreach ($allCompanies as $companyName) {

            $companyTurnoverTotal = 0;
            $turnoverTotal = 0;

            echo "<tr>";
            echo "<td style='width:7%;'>" . $companyName . "</td>";
            echo "<td style='width:7%;'>" . $currentYear . "</td>";

            $monthTotal = array("January"=>"0", "February"=>"0", "March"=>"0", "April"=>"0", "May"=>"0", "June"=>"0", "July"=>"0", "August"=>"0", "September"=>"0", "October"=>"0", "November"=>"0", "December"=>"0");

            foreach ($months as $month) {
                $sql = "SELECT SUM(mob + www) AS 'turnover' FROM turnover WHERE year = '" . $currentYear . "' AND companyID = '" . getCompanyID($companyName) . "' AND month = '" . $month . "'";
                $result = mysql_query($sql) or die(mysql_error());

                $row = mysql_fetch_assoc($result);
                $companyTurnoverMonth = $row['turnover'];

                $monthTotal[$month] += $companyTurnoverMonth;
                echo "<script>alert('" . $month . ": " . $monthTotal[$month] . "')</script>";

                $companyTurnoverTotal += $companyTurnoverMonth;
                $turnoverTotal += $companyTurnoverTotal;

                echo "<td style='width:7%;'>" . make_format($companyTurnoverMonth) . "</td>";
            }
            echo "<td style='width:7%; font-weight: bold; background-color:#d3d3d3;'>" . make_format($companyTurnoverTotal) . "</td>";
            echo "</tr>";
        }

        echo "<tr style='font-weight: bold; background-color:#d3d3d3;'>";
        echo "<td style='width:7%;'>" . "Total" . "</td>";
        echo "<td style='width:7%;'>" . $currentYear . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['January']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['February']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['March']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['April']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['May']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['June']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['July']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['August']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['September']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['October']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['November']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($monthTotal['December']) . "</td>";
        echo "<td style='width:7%;'>" . make_format($turnoverTotal) . "</td>";
        echo "</tr>";

    ?>

</tbody>

我想在右边有一个表格列,显示每家公司的总数。 在表格的底部,我想要一个每月有一列的行,显示每个月的总数。

我试图制作一个包含所有月份的数组,并在同一个月的下一列中增加值。 $monthTotal[$month] += $companyTurnoverMonth;

foreach ($months as $month) {
                $sql = "SELECT SUM(mob + www) AS 'turnover' FROM turnover WHERE year = '" . $currentYear . "' AND companyID = '" . getCompanyID($companyName) . "' AND month = '" . $month . "'";
                $result = mysql_query($sql) or die(mysql_error());

                $row = mysql_fetch_assoc($result);
                $companyTurnoverMonth = $row['turnover'];

                $monthTotal[$month] += $companyTurnoverMonth;

                $companyTurnoverTotal += $companyTurnoverMonth;
                $turnoverTotal += $companyTurnoverTotal;

                echo "<td style='width:7%;'>" . make_format($companyTurnoverMonth) . "</td>";
            }

0 个答案:

没有答案