Local Array Data Source

phpGrid now supports local array data source (version 5.5+). No database is required for local data. So it’s NOT necessary to define PHPGRID_DB_* variables in conf.php when using local array. Simply pass a PHP array as the first parameter to the phpGrid constructor. Everything else is virtually the same.

In the below example, the first segment creates a local PHP array, namely $data1, will be used as the data source for phpGrid. The second segment demonstrates passing the $data1 to the phpGrid constructor and call its methods. All existing phpGrid methods can be used the same way as a database-driven datagrid*.

Make sure to check out the live example!

Local Array (PHP)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$name = array('Bonado', 'Sponge', 'Decker', 'Snob', 'Kocoboo');
for ($i = 0; $i < 200; $i++)
{
    $data1[$i]['id']    = $i+1;
    $data1[$i]['foo']    = md5(rand(0, 10000));
    $data1[$i]['bar1']    = 'bar'.($i+1);
    $data1[$i]['bar2']    = 'bar'.($i+1);
    $data1[$i]['cost']    = rand(0, 100);
    $data1[$i]['name']    = $name[rand(0, 4)];
    $data1[$i]['quantity'] = rand(0, 100);
    $data1[$i]['discontinued'] = rand(0, 1);
    $data1[$i]['email'] = 'grid_'. rand(0, 100) .'@example.com';
    $data1[$i]['notes'] = '';
}

phpGrid Code ($data1 is the local array created above)

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
$dg = new C_DataGrid($data1, "id", "data1");   // $data1 is the local array
$dg->set_col_title("id", "ID")->set_col_width('id', 20);
$dg->set_col_title("foo", "Foo");
$dg->set_col_title("bar", "Bar");
$dg->set_col_title('discontinued', 'disc.')
    ->set_col_width('discontinued', 35);
$dg->set_col_align('cost', 'right')
    ->set_col_currency('cost', '$');
$dg->set_col_width('bar1', 40);
$dg->set_col_width('quantity', 220);
$dg->set_row_color('lightblue', 'yellow', 'lightgray');
$dg->set_databar('quantity', 'blue');
$dg->enable_search(true);
$dg->enable_edit('FORM', 'CRUD');
$dg->enable_resize(true);
$dg->set_col_format('email', 'email');
$dg->set_col_dynalink('name', 'http://example.com', array("id", "name"));
$dg->set_caption('Array Data Test');
$dg->set_col_hidden('bar2');
$dg->set_col_property('notes', array('edittype'=>'textarea','editoptions'=>array('cols'=>40,'rows'=>10)))
    ->set_col_wysiwyg('notes');
$dg->set_dimension(900, 400);
//$dg->set_multiselect(true);
$dg->set_conditional_value('discontinued', '==1',  array("TCellStyle"=>"tstyle"));
$dg->set_theme($theme_name);
$dg->display();

Screenshot

local_array

* Please note that master detail, subgrid, export and file uploads are not yet available when using local array data source.

** Local array data source currently is only available to Enterprise and Universal Licensees.

See Live Example!

JSON Data Source

It’s also possible to use JSON string as a data source with one extra step: use json_decode and set the second parameter to true to return the decoded JSON string to an associative array.

Once you have the array, it can be passed to the phpGrid constructor as if it’s a local array data source. This is useful when your data is real-time or loaded from a remote source such as stock quote and RSS etc.

1
2
3
4
5
6
$url = "http://myurl.com/json_string";
$json = file_get_contents($url);
$json_output = json_decode($json, true);

$dg = new C_DataGrid($json_output['items'], "id", "items");
$dg->display();

No related content found.

Tags: , , , ,