Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Di PHP, cara menampilkan konten array dalam tabel

Sesuatu yang mirip dengan klien mysql keluaran:

$data = array(
    array(
        'group_id'            => '1',
        'group_supergroup_id' => '4',
        'group_deletable'     => '0',
        'group_label'         => 'default',
    ),
    array(
        'group_id'            => '8',
        'group_supergroup_id' => '1',
        'group_deletable'     => '1',
        'group_label'         => 'dbdfg',
    ),
);

if ( empty($data) ) {
    echo "Empty set";
} else {
    // determine widths of titles
    $colWidths = array();
    foreach ( $data[0] as $title => $value ) {
        $colWidths[$title] = strlen($title);
    }
    // determine widths of columns
    foreach ( $data as $row ) {
        foreach ( $row as $title => $value ) {
            if ( is_null($value) ) {
                $value = 'NULL';
            }
            if ( $colWidths[$title] < strlen($value) ) {
                $colWidths[$title] = strlen($value);
            }
        }
    }
    // generate horizontal border
    $horizontalBorder = '+';
    foreach ( $colWidths as $title => $width ) {
        $horizontalBorder .= str_repeat('-', $width + 2) . "+";
    }
    $horizontalBorder .= "\n";
    // print titles
    echo $horizontalBorder;
    echo '|';
    foreach ( $data[0] as $title => $value ) {
        printf(" %-{$colWidths[$title]}s |", $title);
    }
    echo "\n";
    echo $horizontalBorder;
    // print contents
    foreach ( $data as $row ) {
        echo "|";
        foreach ( $row as $title => $value ) {
            if ( is_null($value) ) {
                $value = 'NULL';
            }
            printf(" %-{$colWidths[$title]}s |", $value);
        }
        echo "\n";
    }
    echo $horizontalBorder;
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana cara menyalin file data csv ke Amazon RedShift?

  2. Praktik terbaik di PHP dan MySQL dengan string internasional

  3. Kode Kesalahan:2013. Koneksi terputus ke server MySQL selama kueri

  4. Apakah mungkin untuk mengeksekusi string di MySQL?

  5. Permintaan offset MySql tidak berfungsi