Как отобразить записи MySql в таблице с многоуровневыми заголовками в PHP. Динамическая сводная таблица в PHP с использованием MySqli - PullRequest
0 голосов
/ 12 сентября 2018

Как создать динамическую сводную таблицу в PHP, используя MySqli

У меня есть записи в базе данных MySql. Структура таблицы показана здесь

Теперь я хочу отобразить записи в таблице с многоуровневыми заголовками . , например

Многоуровневая таблица, как я хочу отобразить запись.

Как мне решить эту проблему? Пожалуйста помоги. заранее спасибо Я нашел этот код, но не отображается так, как я хочу. <?php $output = array(); $c=1; while($result = mysqli_fetch_array( $res )) { $output[$c]['headname'] = $result['headname']; $output[$c]['accyear'] = $result['accyear']; $output[$c]['fundamt'] = $result['fundamt']; $output[$c]['fundutiamt'] = $result['fundutiamt']; $c++; } ?>

<table border="1">
<tr>
<th></th>
<?php
foreach ($output as $key => $html)
{
echo "<th>Solution ".$key."</th>";
}
?>
</tr>
<tr>
    <td>Shipping Line</td>
<?php
foreach ($output as $key => $html)
{
echo "<td>".$html['accyear']."</td>";
}
?>
</tr>
<tr>

<?php
foreach ($output as $key => $html)
{
echo "<td>".$html['headname']."</td>";
}
?>
<td>POL</td>
</tr>   
</table>

Ответы [ 2 ]

0 голосов
/ 12 сентября 2018

Вы можете создать новый массив, используя результат mysql

    <?php
       $output = array();
       while($result = mysqli_fetch_array( $res )) 
       {
           $key=$result['accyear'];
           if(!array_key_exists($key,$output)){
               $output[$key]=array();
               array_push($output[$key],array(
                     $result['headcode'] =array(
                         "fundamt" =>  $result['fundamt'],
                         "fundutiamt" =>  $result['fundutiamt'],  
                     ) 
               ));
           }
       }
    ?>
    print_r($output);

Результат

2019 => [ 061182 => [fundamt => 12, fundutiamt => 23] ]
        [ 961182 => [fundamt => 2, fundutiamt => 23] ]

Вы можете использовать этот массив для создания таблицы в HTML

0 голосов
/ 12 сентября 2018

Используйте атрибут colspan.

td,
th {
    border: 1px solid rgb(190, 190, 190);
    padding: 10px;
}

td {
    text-align: center;
}

tr:nth-child(even) {
    background-color: #eee;
}

th[scope="col"] {
    background-color: #696969;
    color: #fff;
}

th[scope="row"] {
    background-color: #d7d9f2;
}

caption {
    padding: 10px;
    caption-side: bottom;
}

table {
    border-collapse: collapse;
    border: 2px solid rgb(200, 200, 200);
    letter-spacing: 1px;
    font-family: sans-serif;
    font-size: .8rem;
}
<table>
    <caption>Alien football stars</caption>
    <tr>
        <th scope="col">Player</th>
        <th scope="col" colspan=2>Gloobles</th>
        <th scope="col" colspan=2>Za'taak</th>
    </tr>
    <tr>
        <th scope="col"></th>
        <th scope="col">2016</th>
        <th scope="col">2017</th>
        <th scope="col">2016</th>
        <th scope="col">2017</th>
    </tr>
    <tr>
        <th scope="row">TR-7</th>
        <td>7</td>
        <td>4,569</td>
              <td>7</td>
        <td>4,569</td>
    </tr>
    <tr>
        <th scope="row">Khiresh Odo</th>
        <td>7</td>
        <td>7,223</td>
              <td>7</td>
        <td>7,223</td>
    </tr>
    <tr>
        <th scope="row">Mia Oolong</th>
        <td>9</td>
        <td>6,219</td>
              <td>9</td>
        <td>6,219</td>
    </tr>
</table>

(исходная таблица от https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...