pivot html table using php

后端 未结 1 1975
悲哀的现实
悲哀的现实 2021-01-24 03:46

I have this php code:

$query_production = \"SELECT uploadedby as name, sum(points) as points,
date_format(uploaddate,\'%Y-%m-%d\') as date FROM imsexport 
WHERE          


        
相关标签:
1条回答
  • 2021-01-24 04:12

    This will get you most of the way there; what I've left to you is how to get the left offset correct (or the date/points column alignment; hint, you'll need to keep the date with the name/points pair and know it's offset in $cols).

    Obviously you're dealing with rows out of a database, so it'll be a little bit different.

    See the codepad demo link below the code.

    <?php
    
    $data = "John 147 2014-01-01
    Bob 79 2014-01-01
    Joe 156 2014-01-01
    Sue 116 2014-01-01
    John 117 2014-01-02
    Bob 186 2014-01-02
    Sue 74 2014-01-02
    Bob 233 2014-01-03
    John 159 2014-01-03
    Sue 162 2014-01-03
    Bob 162 2014-01-04
    Sue 38 2014-01-05";
    
    $data = explode("\n", $data);
    
    $cols = array();
    $pivot = array();
    
    while ($line = array_shift($data)) {
        list($name, $points, $date) = explode(' ', trim($line));
    
        if (!$pivot[$name]) $pivot[$name] = array();
    
        array_push($cols, $date);
    
        array_push($pivot[$name], array('date' => $date, 'points' => $points));
    }
    
    $cols = array_unique($cols);
    
    print_r($pivot);
    
    echo implode('|', $cols);
    
    echo PHP_EOL;
    
    foreach ($pivot as $name => $row) {
        while ($entry = array_shift($row)) {
            echo str_pad($name, 7, ' ') . str_pad($entry['points'], 3, ' ', STR_PAD_LEFT) . '|';
        }
    
        echo PHP_EOL;
    }
    
    ?>
    

    http://codepad.org/WqqpKwn3

    0 讨论(0)
提交回复
热议问题