Codeigniter表类垂直显示

时间:2019-02-27 16:37:20

标签: php html mysql codeigniter codeigniter-3

使用codeigniter 3,我从MySQL数据库中检索一行,该行位于名为$tableData的数组中,格式如下:

Array
(
    [0] => Array
        (
            [id] => 102
            [firstname] => Ross
            [lastname] => Bing
            [title] => Doctor
            [timestamp] => 2019-01-18 10:17:05
            [member_no] => 234
        )

)

使用CI table library如何在这样的垂直表中显示它;

+---------------+---------------------+
| id            | 102                 |
+---------------+---------------------+
| First Name    | Ross                |
+---------------+---------------------+
| Last Name     | Bing                |
+---------------+---------------------+
| Title         | Doctor              |
+---------------+---------------------+
| Timestamp     | 2019-01-18 10:17:05 |
+---------------+---------------------+
| Member Number | 234                 |
+---------------+---------------------+

我的PHP

foreach($tableData as $row) { 
    $this->table->add_row($row);
}

$data = array(
    'table' =>  $this->table->generate()
);

在我看来,echo $table和数据已成功显示-但它是水平的:/

1 个答案:

答案 0 :(得分:1)

也许这样可以工作。

$heading = array(
  'id' => 'custom name',
  'firstname' => 'custom name',
  'lastname' => 'custom name',
  'title' => 'custom name',
  'timestamp' => 'custom name',
  'member_no' => 'custom name'
);

$fields = array_keys($tableData[0]);
$rows = array();
foreach($fields as $key => $field) {
   $rows[$key][0] = $heading[$field];
   foreach($tableData as $key2 => $item) { 
        $rows[$key][$key2 +1] = $item[$field];
   }
}

foreach($rows as $row) {
    $this->table->add_row($row);
}

$data = array(
    'table' =>  $this->table->generate()
);