Advanced Usage

Prev

Next

Advanced Usage

Advanced Usage – Advance usage of MDB2_TableBrowser

Full example use the MDB2_TableBrowser API

The example source below contains all the different methods available to tableBrowser object beyond the features explained in the basic usagedocumentation.

<?php
require_once "MDB2.php";

define('DSN', 'mysql://username:pass@localhost/animal_db');

/**
* The example below relies on the following data from the table tbl_animals
* ID NAME TYPE LIFESPAN
* 1 dog mammal 12
* 2 cat mammal 30
* 3 parrot bird 60
* 4 shark fish 30
* 5 dolphin mammal 50
* 6 crocodile reptile 50
* 7 snake reptile 20
* 8 spider arachnid 1
* 9 housefly insect 1
* 10 ostrich bird 35
* 11 bat mammal 6
* 12 human mammal 100
*/

$dsn = 'mysql://username:pass@localhost/animal_db';
$options = array(
'debug' => 2,
'result_buffering' => false,
);

$mdb2 = MDB2::singleton($dsn, $options);

//Create the table
setupDb($mdb2);

$mdb2->loadModule('TableBrowser');

//Create a table browser for the tbl_animals table, and specify id as the primary key
$browser = $mdb2->tableBrowserFactory('tbl_animals', 'id');

//The data browsing object is now ready
//First insert the needed data
$data = array(
array(1,'dog','mammal',12),
array(2,'cat','mammal',30),
array(3,'parrot','bird',60),
array(4,'shark','fish',30),
array(5,'dolphin','mammal',50),
array(6,'crocodile','reptile',50),
array(7,'snake','reptile',20),
array(8,'spider','arachnid',1),
array(9,'housefly','insect',1),
array(10,'ostrich','bird',35),
array(11,'bat','mammal',6),
array(12,'human','mammal',100)
);
$browser->insertRows(array('id', 'name','type','lifespan'), $data);

//Get info on a single animal, getRow returns a hash array and getRows returns
//an MDB2_Result object
$browser->getRow(1);
print "\n" . $browser->getLastSQL();
//Prints: SELECT `ID`,`NAME`,`TYPE`,`LIFESPAN` FROM tbl_animals WHERE (`id` = 1)

//Get info on 3 animals in the table sorted by name starting with the 5th animal
$browser->getRows('name', 3, 5);
print "\n" . $browser->getLastSQL();
//The limits/offsets are not shown below as they are set by mdb2 library
//Prints: SELECT `ID`,`NAME`,`TYPE`,`LIFESPAN` FROM tbl_animals

//Hide the ID column and rename the column "TYPE" to "Animal Type"
$browser->selectColumns(array('name','type','lifespan'));
$browser->setColumnAlias('TYPE', 'ANIMAL TYPE');
$browser->getRows('name', 3, 5);
print "\n" . $browser->getLastSQL();
//Prints:SELECT `NAME`,`TYPE` AS `ANIMAL TYPE`,`LIFESPAN` FROM tbl_animals

//Get the different kinds of animal types in the table eg: mammal, reptile,...
$browser->getColumnValues('type');
print "\n" . $browser->getLastSQL();
//Prints:SELECT DISTINCT `TYPE` FROM tbl_animals

//This also works with aliases you have set up
$browser->getColumnValues('ANIMAL TYPE');
print "\n" . $browser->getLastSQL();
//Prints:SELECT DISTINCT `TYPE` FROM tbl_animals

/*
* Example using filters, look for mammals with a lifespan <60 years Multiple
* filters can be added and removed. This functionality can be used to quickly
* build a browsing application that gives the user the freedom to traverse the
* table data in different ways.
*/
$browser->addFilter('MaxAge', 'lifespan', '<=', 60);

続き…

ページ上段