PDA

View Full Version : Dynamic query from PHP?



cosmos411
21 Jul 2007, 6:46 AM
Hi everyone,

I'm pretty new to web development and extjs so please excuse my question if it's a dumb one. I was wondering if it's possible to get a dynamic query string into a grid without using the HttpProxy method. I don't want to have to read from a php page in order to get the information I want. This means that I would have to write to a PHP file every time the user changes their selection options. I hope that it would be possible to somehow create this query dynamically and have the grid data store component use it.

I don't know if I have been clear with this question. I tried my best :)

Thanks for any help you can offer.

Gord

cosmos411
21 Jul 2007, 9:50 AM
I don't know if this is a smart solution or not, but I have found something that seems to work.

What I have done is get a result set in my php code, then looping through that result set I build up a <script type="javascript .. > </script> string formatting it so that it can be used by Ext.data.MemoryProxy. The code looks like this:

<?php
function createJavaScript($dataSource,$objName='gridData'){
// validate variable name
if(!is_string($objName)){
die('Invalid variable name');
}

// initialize JavaScript string
$javascript='<script type="text/javascript" language="javascript">var ' . $objName . '=[';

// check if we have a valid result set
if(!$numRows=mysql_num_rows($dataSource)){
die('Invalid result set parameter');
}

for($i=0;$i<$numRows;$i++){
// build JavaScript array from result set
$javascript.='[';
$tempOutput='';
foreach($row=mysql_fetch_array($dataSource,MYSQL_ASSOC) as $column){
$tempOutput.='"'.$column.'",';
}
$javascript.=trim($tempOutput).'],';
}
$javascript = str_replace(',]', ']', $javascript);
$javascript = substr_replace($javascript,"",-1);
$javascript.='];</script>'."\n";

// return JavaScript code
return $javascript;
}
?>

The parameter $objName is the name of the variable you use to retrieve for you Ext grid:

new Ext.data.MemoryProxy(gridData)

You can then use an array reader to get the information from this:
new Ext.data.ArrayReader(setup column information)

The rest of the code, ie. columnModel is the same as you would normally do.

This may or may not have helped anyone. I hope it did.

Regards,

G

Wolfgang
21 Jul 2007, 10:22 AM
Hi everyone,
...
I was wondering if it's possible to get a dynamic query string into a grid without using the HttpProxy method. I don't want to have to read from a php page in order to get the information I want. This means that I would have to write to a PHP file every time the user changes their selection options.
...
I hope that it would be possible to somehow create this query dynamically and have the grid data store component use it.
...
Gord
Some comments:
- you need to create a XHR request that returns either XML or JSON formated data. Wether this request targets php, java or something like a shell script is upto you.
- you can pass user defined data within this request using the POST or GET method.

Here you find an example that explains in more detail what i mean:
Refers to forms but the technique is the same
http://extjs.com/forum/showthread.php?t=4741
Really great screencast for grids
http://extjs.com/forum/showthread.php?t=5014

Regards

Wolfgang