018. How to display a 2D array in table - Part 1?

Problem

You want to display a 2D array in a table as shown below:

$data = array(
array('', 'header1', 'header2', 'header3'),
array('row0', 1, 2, 3),
array('row1', 4, 5, 6));

How to display a 2D array in table - Part 1?


Solution

Use GtkTable

$table->attach(new GtkLabel($a[$row][$col]), $col, $col+1, $row, $row+1);

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
28   
29   
31   
32   
33   
34   
35   
36   
37   
38   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 200);
$window->connect_simple('destroy', array('Gtk','main_quit'));

$vbox = new GtkVBox();
$window->add($vbox);

// display title
$title = new GtkLabel('Display 2D Array in Table - Part 1');
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);

$table = new GtkTable(); // note 1
$vbox->pack_start($table);

$data = array(
array('', 'header1', 'header2', 'header3'),
array('row0', 1, 2, 3),
array('row1', 4, 5, 6));

display_table ($table, $data); // note 2

function display_table($table, $a) {
    for ($row=0; $row<count($a); ++$row) {
        for ($col=0; $col<count($a[$row]); ++$col) {
            $table->attach(new GtkLabel($a[$row][$col]), $col, $col+1, $row, $row+1); // note 3
        }
    }
}

$window->show_all();
Gtk::main();
?>

Output

As shown above.
 

Explanation

  1. Create the table. Note that table is dynamic in PHP-GTK, i.e. you do not need specify the dimension during creation. It can "auto-expand" on the fly.
  2. Wrote a small function to simplify the display of the 2D array in a table.
  3. Attach each data one by one to its corresponding cell using GtkTable::attach().

Add comment


Security code
Refresh