JavaScript
  Home arrow JavaScript arrow EXT JS Passing Live Data
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

EXT JS Passing Live Data
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    2009-07-27

    Table of Contents:
  • EXT JS Passing Live Data
  • MySQL
  • Adding Additional Configuration Properties
  • Working More Closely with the Back End

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    EXT JS Passing Live Data


    (Page 1 of 4 )

    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.

    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.

    More JavaScript Articles
    More By Dan Wellman


       · In terms of your PHP, the inbuilt JSON features are actually the easier way of...
     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek