PDA

View Full Version : php postresql extjs



leunamme
12 May 2011, 6:21 AM
Greetings!
I am making a web application based on php and posgresql. I'd like to use extjs to make some forms but I have no clue how to insert data into postgresql from the extjs forms.
Please, any one to help me?

drian
12 May 2011, 9:19 PM
you send the data from client-side (extjs) to the server side through ajax calls.

Check out the tutorials page. http://www.sencha.com/learn/Tutorials

leunamme
16 May 2011, 1:06 AM
thank you!
but I have no example in the tutorial using postgresql database. My concern is to be able to insert data into a pgsql database or retrieving data from a php-ext form.
thank you for your help!

drian
16 May 2011, 1:22 AM
You need to send the data from extjs using Ext.Ajax to a php script that will process your data and insert it in your database.

You need to write your own code in php to insert the data into the database.

leunamme
22 May 2011, 9:36 AM
Hello!
Thank you for this helpful tutorial you advised me.
Through the tutorials I found this important one; see url: http://www.sencha.com/learn/Tutorial:Grid_PHP_SQL_Part4 . It's running with MySQL and I've redone it with PostgreSQL and it works very well. As you can see, a combobox is used and static data are predefined in the combo.
But now I'd like to know how to connect the combobox to the datastore so that my data in the combobox should be dynamic (i mean from my database).
In the tutorial, the static one is like this:

editor: new Ext.form.ComboBox({
typeAhead: true,
triggerAction: 'all',
store:new Ext.data.SimpleStore({
fields:['partyValue', 'partyName'],
data: [['1','No Party'],['2','Federalist'],['3','Democratic-Republican'],['4','Democratic'],['5','Whig'],['6','Republican']] (//this is static
}),
mode: 'local',
displayField: 'partyName',
valueField: 'partyValue',
listClass: 'x-combo-list-small'
})

Here, a predefined list is set: *data: [['1','No Party']...*

But I'd like someone to help me through connecting the combobox to the datastore (dynamic data). I mean the code to do it.

I saw this is possible through this tutorial; they said:
"You can create a new DataStore that will connect to database.php and ask for a list of parties to display. Simply connect the combobox to that datastore and you're done. This is mostly useful when the data changes during runtime, which is not the case for our program." My question is about how to perform this.

Thank you for your help!

drian
22 May 2011, 8:31 PM
You basically need a datastore that has similar configuration to the datastore you use for the grid (like the PresidentsDataStore).

leunamme
23 May 2011, 4:23 AM
I have configured the PartyDataStore like this:

PartyDataStore = new Ext.data.Store({
id: 'PartyDataStore',
proxy: new Ext.data.HttpProxy({
url: 'database.php',
method: 'POST'
}),
baseParams:{task: "LIST"}, // this parameter is passed for any HTTP request
reader: new Ext.data.JsonReader({
root: 'result',
totalProperty: 'tot',
id: 'id'
},[
{name: 'IDparty', type: 'int', mapping: 'IDparty'},
{name: 'PartyName', type: 'string', mapping: 'name'}
]),
sortInfo:{field: 'PartyName', direction: "ASC"}
});

And I connect it to the combobox like:

{
header: 'Party',
dataIndex: 'PartyName',
width: 150,
editor: new Ext.form.ComboBox({
typeAhead: false,
triggerAction: 'all',
store:PartyDataStore,
mode: 'local',
displayField: 'partyName',
valueField: 'partyValue',
lazyRender:true,
listClass: 'x-combo-list-small'
})
}

But it still doesn't work!!! the list (combo) is empty!!!

What should be the problem???

Angilo
23 May 2011, 5:01 AM
In your script database.php do a print_r($_POST); and see what is being posted.

Here is a working combobox script for you, based on codeigniter:

Clientside


var companiesDataStore = new Ext.data.JsonStore({
proxy: new Ext.data.HttpProxy({
url: Base_URL + 'index.php/relations/listCompanies/',
method: 'POST'
}),
root: 'results',
fields: ['companies_id', 'companies_name'],
autoLoad: true
})
var Companies = new Ext.form.ComboBox({
fieldLabel: 'Companies',
hiddenName:'companies_id',
store: companiesDataStore,
valueField:'companies_id',
displayField:'companies_name',
typeAhead: true,
mode: 'local',
triggerAction: 'all',
emptyText:'Select a company...',
selectOnFocus:true,
width:130
});



Serverside:



function listCompanies(){
$cache = '';
$companies_sql = "select
companies_id,
companies_name,
companies_phone,
companies_street_address,
companies_street_address_second,
companies_postcode,
companies_city,
companies_country,
companies_status
from
".TABLE_COMPANIES."
where
".TABLE_COMPANIES.".offices_id = '".(int)$this->session->userdata('office_id')."'";

$companies_is_supplier = $this->input->post('companies_is_supplier');
if (isset($companies_is_supplier) && !empty($companies_is_supplier))
$companies_sql .= ' and companies_is_supplier = 1';

if (!empty($_POST['fields']))
{
$fields = json_decode(stripslashes($_POST['fields']));
$count = 0;
$companies_sql .= " and (";
foreach ($fields as $field)
{
$count++;

$companies_sql .= $field . "::text ilike '%".$_POST['query']."%'";
if ($count < count($fields))
$companies_sql .= " or ";

}

$companies_sql .= ")";
}

if (isset($_POST['sort']) && isset($_POST['dir']))
{
$order_by = " order by ".TABLE_COMPANIES."." . $_POST['sort'] . " " . $_POST['dir'];


}
else
{
$order_by = " order by ".TABLE_COMPANIES.".companies_name asc";
}
$companies_sql .= $order_by;

$companies_query = $this->db->query($companies_sql);
$RecordCount = $companies_query->num_rows();

if (isset($_POST['start']) || isset($_GET['start']))
$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
if (isset($_POST['limit']) || isset($_GET['limit']))
$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);

if (isset($end) && isset($start))
{
$limit = " LIMIT ".$end." OFFSET ".$start;
$companies_sql .= $limit;
}
$companies_query = $this->db->query($companies_sql);

foreach ($companies_query->result_array() as $companies)
{
$cache[] = $companies;
}
echo json_encode(array("total" => $RecordCount, "results" => $cache));
}

leunamme
23 May 2011, 6:30 AM
I've done something like that but I don't see where I missed.
If you'd send a complete running tutorial so I check what's missing!
Thanks!

leunamme
25 May 2011, 6:03 AM
Thanks for your help!
Finally, it's working!

davedran
25 Jun 2014, 11:10 PM
Hi! Can you please share your php extjs postgresql connection source code. I have working php extjs MySQL crud working aplicaiton that I need to get working in Postgresql. Any help is greatly appreciate, I just need a working sample of Extjs with Postgresql in PHP Thank you. David