Sample Code 18: How to display a 2D array in table - Part 1? |
|
Written by kksou
|
|
Sunday, 17 September 2006 |
|
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));

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
| <?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
|
- Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
- Registration is free and immediate.
- Have some doubt about the registration? Please read this forum article.
Explanation
- 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.
- Wrote a small function to simplify the display of the 2D array in a table.
- Attach each data one by one to its corresponding cell using GtkTable::attach().
User reviews There are no user reviews yet. Note: You have to be a registered member to leave a comment. Free registration here. |