Geodjango Tutorials

Adding data to ExtJS grid from PostgreSQL

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.

I will demonstrate using PHP and ExtJS on how to query a database and return the data into a JSON format that will be used in our JavaScript application.
First, we write a script that will query our database in PHP (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);
This will return our result as a JSON file.We then define a path in our JavaScript file to point to the query results
var mydata = “data.php”;

Secondly, we create a JSON store to read our query result above.This will be done using the ExtJS library

var store = new{
                         proxy: {
                                          type: ‘ajax’,
                                          reader: {
                                                       type: ‘json’,
                         fields: [
                                     {name: ‘gid’, type: ‘int’},
                                       ‘cntry_name’, ‘sovereign’, ‘pop_cntry’,’curr_type’,’landlocked’
We then load our store with data from mydata
Lastly, we define our grid that will display the data
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,
                         title:’DB Grid’
The results resembles this;

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.

There are various ways to achieve this, and this is just one of them. Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Wanjohi Kibui

Passionate about harnessing the power of geospatial technology to create innovative solutions, I'm a GIS Consultant and Developer dedicated to building cutting-edge geospatial applications. With a keen eye for spatial analysis and a knack for problem-solving, I specialize in crafting solutions that seamlessly integrate technology and geography.

More Reading

Post navigation