简体   繁体   中英

Codeigniter Table Class Vertical Display

Using codeigniter 3, I am retrieving a single row from a MySQL database, this is in an array called $tableData in the following format;

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

)

Using the CI table library How can I display this in a vertical table like this;

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

My PHP

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

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

In my view I echo $table and the data is displayed successfully - but it's horizontal :/

Maybe something like this can work.

$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()
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM