dsk95913 2018-08-13 23:39
浏览 109
已采纳

重建PHP数组以正确地将数据拟合到html表中

I'm trying to display data that shows the count for each month for locations in Jan-December.

I wrote the query to get the necessary data from the server but when I display it in the view in my table the layout for the data, doesn't look the way I want it to look like in my table. I believe I have to reconstruct the array that I wrote in the query for the right format first and then I can display it in my table?

Right now, the data that comes back from the server looks like so

<table id="registeredTable" class="table table-striped table-bordered table-hover">
<thead>
    <tr>
        <th>Locations</th>
        <th>January</th>
        <th>Feburary</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>
    </tr>
</thead>
<tbody>
    <?php
      foreach($selectAllmonthlylocations as $item) {
        echo '
    <tr>
      <td>'.$item["locations"].'</td>
      <td>'.$item["Total"].'</td>
    </tr>
        ';
  }
    ?>
</tbody>
</table>

So my question is how can I format my array properly to display it in my table how I want?

Array
(
    [0] => Array
        (
            [usergroup] => Austin_Domestic
            [DateAdded] => 2018-01-03
            [Total] => 15
        )

    [1] => Array
        (
            [usergroup] => Austin_International
            [DateAdded] => 2018-01-25
            [Total] => 2
        )

    [2] => Array
        (
            [usergroup] => BayArea_Domestic
            [DateAdded] => 2018-01-16
            [Total] => 192
        )

    [3] => Array
        (
            [usergroup] => BayArea_International
            [DateAdded] => 2018-01-05
            [Total] => 28
        )

    [4] => Array
        (
            [usergroup] => Bengaluru_Domestic
            [DateAdded] => 2018-01-10
            [Total] => 2
        )

    [5] => Array
        (
            [usergroup] => Bengaluru_International
            [DateAdded] => 2018-01-05
            [Total] => 1
        )

    [6] => Array
        (
            [usergroup] => Cork
            [DateAdded] => 2018-01-02
            [Total] => 31
        )

    [7] => Array
        (
            [usergroup] => CulverCity
            [DateAdded] => 2018-01-10
            [Total] => 3
        )

    [8] => Array
        (
            [usergroup] => Denver
            [DateAdded] => 2018-01-10
            [Total] => 1
        )

    [9] => Array
        (
            [usergroup] => Hyderabad
            [DateAdded] => 2018-01-05
            [Total] => 3
        )

    [10] => Array
        (
            [usergroup] => London
            [DateAdded] => 2018-01-02
            [Total] => 10
        )

    [11] => Array
        (
            [usergroup] => Macau
            [DateAdded] => 2018-01-17
            [Total] => 1
        )

    [12] => Array
        (
            [usergroup] => Munich
            [DateAdded] => 2018-01-02
            [Total] => 6
        )

    [13] => Array
        (
            [usergroup] => Sacramento_Domestic
            [DateAdded] => 2018-01-04
            [Total] => 1
        )

    [14] => Array
        (
            [usergroup] => Shanghai
            [DateAdded] => 2018-01-12
            [Total] => 2
        )

    [15] => Array
        (
            [usergroup] => Singapore
            [DateAdded] => 2018-01-03
            [Total] => 8
        )

    [16] => Array
        (
            [usergroup] => Sydney
            [DateAdded] => 2018-01-21
            [Total] => 1
        )

    [17] => Array
        (
            [usergroup] => Tokyo
            [DateAdded] => 2018-01-04
            [Total] => 3
        )

    [18] => Array
        (
            [usergroup] => Austin_Domestic
            [DateAdded] => 2018-02-01
            [Total] => 31
        )

    [19] => Array
        (
            [usergroup] => Austin_International
            [DateAdded] => 2018-02-19
            [Total] => 2
        )

    [20] => Array
        (
            [usergroup] => Bangkok
            [DateAdded] => 2018-02-07
            [Total] => 1
        )

    [21] => Array
        (
            [usergroup] => BayArea_Domestic
            [DateAdded] => 2018-02-08
            [Total] => 165
        )

    [22] => Array
        (
            [usergroup] => BayArea_International
            [DateAdded] => 2018-02-12
            [Total] => 29
        )

Here is my code:

$selectallmonthlysql = 'SELECT locations, DateAdded, COUNT(*) as Total
                    FROM testserverdataSignup
                    WHERE DateAdded > "2017-12-31"
                    GROUP BY month(DateAdded), locations';

    $selectAllmonthlystmt = $dbo->prepare($selectallmonthlysql);

    $selectAllmonthlystmt->execute();

    $selectAllmonthlylocations = $selectAllmonthlystmt->fetchAll(PDO::FETCH_ASSOC);

Screenshot of what the table currently looks like: I want the individual count for each location to correlate to the month and go across instead of down

enter image description here

</div>
  • 写回答

2条回答 默认 最新

  • douqie3391 2018-08-13 23:55
    关注

    First change your query

    $selectallmonthlysql = '
    SELECT
        locations,
        MONTHNAME(DateAdded) AS month,
        COUNT(*) AS total
    FROM    
        testserverdataSignup
    WHERE
        DateAdded > "2017-12-31"
    GROUP BY month(DateAdded),locations';
    

    I lowercased Total to total, its something that was irritating me.

    With PDO then you can do

    $results = $selectAllmonthlystmt->fetchAll(PDO::FETCH_GROUP);
    foreach($results AS $location => $row ){
    

    FETCH_GROUP is poorly documented but this will organize your data with the first column as the Key of the nested array, in this case the location, which is exactly what we want. One of the reasons I use PDO instead of that other DB library I won't mention by name but it starts with MySql and ends with i.

    MONTHNAME (click for documentation) will return the full name of the month like "February" which although is not necessary (just a number would do) its much easier to read when outputting the array for debugging.

    You'll wind up with something like this

    //canned example data, notice one month is out of order and the big gap
    // between February to October, this is to show it properly orders and
    //fills those in. Always best to test this situations.
    $result = array(
       "Austin_Domestic" => array(
    
           0 => Array(
                "month" => "February",
                "total" => 5
            ),
           1 => Array(
                "month" => "January",
                "total" => 15
            ),
            2 => Array(
                "month" => "October",
                "total" => 8
            ),
    
        ),
    );
    //$results = $selectAllmonthlystmt->fetchAll(PDO::FETCH_GROUP);
    
    //then putting it all together
    
    //we'll need the months in an array anyway so we can saves some space with them in the HTML  too.
    $months = array(    
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    )
    
    ?>
    
    <table id="registeredTable" class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th>Locations</th>
            <?php foreach($months as $month){
                 //Output the names of the months
                 echo "<th>{$month}</th>
    ";
            } ?>
            </tr>
        </thead>
        <tbody>
        <?php 
            foreach( $result as $location => $rows ){
                 echo "<tr>
    ";
                 echo "<td>{$location}</td>
    ";
                //simplifies lookup by month (indexes are correlated)
                //it returns column 'month' from $rows as a flat array.
                //we can save ourselves an external loop by doing this "trick"
                $m = array_column($rows, 'month');
                //for example, in the case: [0=>February,1=>January,2=>October]
                //because this is created from $rows, with a natural number index
                //we can simply do array_search on $m to get the index we need in $rows (see below)
                foreach($months as $month){
                    $index = array_search($month, $m);
                    if(false === $index){
                        echo "<td>0</td>
    ";
                    }else{
                       echo "<td>{$rows[$index]['total']}</td>
    "; 
                    }                 
                }
                echo "</tr>
    ";
            }
        ?>
        </tbody>
    </table>
    

    Output (I did format it to look a bit nicer):

    <table id="registeredTable" class="table table-striped table-bordered table-hover">
        <thead>
            <tr>
                <th>Locations</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>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Austin_Domestic</td>
                <td>15</td>
                <td>5</td>
                <td>0</td>
                <td>0</td>
                <td>0</td>
                <td>0</td>
                <td>0</td>
                <td>0</td>
                <td>0</td>
                <td>8</td>
                <td>0</td>
                <td>0</td>
            </tr>
        </tbody>
    </table>
    

    Sandbox

    One note and benefit of using an array for the months is you spelled Feburary wrong it should be February something I've been guilty of many many times.

    The only real downside to this, is the month name has to match the month name returned from the DB, which shouldn't be an issue if they are spelled correctly (which is how I found the mistake). Otherwise this can applied to the month as a number just as easily, same idea.

    Cheers!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?