Loading data from a PostgreSQL database into an application is useful more so if there will be frequent changes on the data stored in the databases. The data is loaded from the database when the application is loading. Any changes on the data are reflected into the grid carrying our data in the application.
//data.php
$db = pg_connect(“host=somehost port=someport dbname=**** user=**** password=****”) or die(‘could not connect:’.pg_last_error());$result = pg_query($db, “SELECT *,ST_AsGeoJSON(geom) FROM Africa”) or die(“Data load failed:”.pg_last_error());$rows = array(‘type’ => ‘FeatureCollection’,‘features’ => array());while($r = pg_fetch_assoc($result)){$rows[] = $r;}echo json_encode($rows);?>
var mydata = “data.php”;
Secondly, we create a JSON store to read our query result above.This will be done using the ExtJS library Ext.data.JsonStore()
var store = new Ext.data.JsonStore({proxy: {type: ‘ajax’,url:mydata,reader: {type: ‘json’,}},fields: [{name: ‘gid’, type: ‘int’},‘cntry_name’, ‘sovereign’, ‘pop_cntry’,’curr_type’,’landlocked’]});
store.load();
var grid = new Ext.grid.GridPanel({store: store,columns: [{header: ‘gid’, width: 50, sortable: true, dataIndex: ‘gid’},{header: ‘Country Name’, width: 100, sortable: true, dataIndex: ‘cntry_name’},{header: ‘Sovereign’, width: 100, sortable: true, dataIndex: ‘sovereign’},{header: ‘Population’, width: 250, sortable: true, dataIndex: ‘pop_cntry’},{header: ‘Currency’, width: 250, sortable: true, dataIndex: ‘curr_type’},{header: ‘Landlocked’, width: 250, sortable: true, dataIndex: ‘landlocked’}],stripeRows: true,columnLines:true,title:’DB Grid’});
That was a short demonstration on how data can be loaded from a PostgreSQL database with PHP into our application and be displayed in a grid.
Adding data to ExtJS grid from PostgreSQL