Home arrow JavaScript arrow EXT JS Passing Live Data
JAVASCRIPT

EXT JS Passing Live Data


Welcome to part two of the EXT-JS live data tutorial. In part one we created the underlying page and added the code to create the JsonStore, ColumnModel and GridPanel. Our GridPanel, however, currently has no data, so the first thing we’re going to do in this part of the tutorial is add the PHP code that will pass this data back to the JsonStore.

Author Info:
By: Dan Wellman
Rating: 3 stars3 stars3 stars3 stars3 stars / 3
July 27, 2009
TABLE OF CONTENTS:
  1. · EXT JS Passing Live Data
  2. · MySQL
  3. · Adding Additional Configuration Properties
  4. · Working More Closely with the Back End

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
EXT JS Passing Live Data
(Page 1 of 4 )

For reference, PHP is not the only server-side language that could be used to accomplish this task, but I’ve chosen it because it’s free, powerful and easy to use. Alternatives include Java and ASP.NET, among others.

Some PHP

In a new file in your text editor, then, add the following code:


<?php


// set content-type header

header('Content-type: application/json');


// connection information

$host = "localhost";

$user = "root";

$password = "yourPasswordHere";

$database = "shop";

 

//make connection

$server = mysql_connect($host, $user, $password) or die("Could not connect");

$connection = mysql_select_db($database, $server) or die("Could not select database");

 

//build query

$query = "SELECT * FROM products";


//query databse

$result = myql_query($query);

 

//create json data structure

$startJsonObject = "{ products: [";

$endJsonObject = "]}";

 

//build json object with stuff from db

for ($x = 0; $x < mysql_num_rows($result); $x++) {

$row = mysql_fetch_assoc($result);

$data.= "{title:'".$row['title']."',image:'".$row['image']."',inStock:'".$row['inStock']."',price:'".$row['price']."',category:'".$row['category']."',manufacturer:'".$row['manufacturer']."'},";

}


//remove comma from final object in array

$noFinalComma = substr_replace($data, "", -1);

 

//output complete json object

echo $startJsonObject . $noFinalComma . $endJsonObject;


//close server connection

mysql_close($server);

 

?>


Save this as productSearch.php. The first thing we do in this file is set the content-type header to application/json. Next we define the information needed to access the MySQL database, which consists of the host and usernames, and the password and database name.

Following this we attempt to connect and try to select the specified database. If this is possible, we then select all of the data in the database. So far, all of this is pretty standard stuff, but it soon starts to get interesting.

We want our JSON object to have a specific structure, as it must match the structure of the object that our JsonStore is expecting. We first define the beginning and end of the JSON object as strings. We then use the information contained in our query (which will be a series of rows containing our data, again very much like an associate array). For each row in the query results we add a new item to our JSON array, using text to specify the opening and closing curly braces and the object properties, and using references to our data as the property values.

The next part of the code isn’t strictly necessary, but I always prefer to be as tidy as possible. Because each item within our JSON array will be exactly the same, the last item will be followed by a comma separator, even though it doesn’t actually require one. To tidy things up we can easily remove it using the substr PHP function.

Now that we have all of the data within our JSON object, we can build the object using the start and end strings and the data from the database. This is passed back to the client using a simple echo, but because we set the content type header to JSON it will be recognized and treated as a JSON object instead of a simple string. We then close the database connection, as we have the data we need. You should note that PHP5 has functions for building JSON objects built in to it. I’ve chosen to use strings in this example because it’s probably the quickest and simplest way to get the object in the structure that we want.

Next: MySQL >>

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks
- Dynamic jQuery Styling

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials