By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,936 Members | 1,396 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,936 IT Pros & Developers. It's quick & easy.

How to load data in extjs using php

P: 33
Hi, I'm trying to load data to my grid panel which resides in php using JsonStore object. But the problem is JsonStore is not getting data from php only. Can anyone tell me where i've gone wrong. Is my php file is proper or not? I'm very new to both php and extjs. please help me out.
i'm uploading the both .js and .php files. please help me out.

test.js file:
Expand|Select|Wrap|Line Numbers
  1. Ext.onReady(function(){
  2.   alert("inside onReady");
  3.  
  4.   Ext.QuickTips.init();
  5.  
  6.   var employee = Ext.data.Record.create([
  7.       {name:'firstname'},
  8.       {name:'lastname'}]);
  9.  
  10.   //var myReader = new Ext.data.JsonReader({
  11.       //root:"root"
  12.       //},employee);
  13.  
  14.   var store = new Ext.data.JsonStore({
  15.             id:'ID'
  16.             ,root:'root'
  17.             ,totalProperty:'totalCount'
  18.             ,url:'test.php'
  19.             ,autoLoad:true
  20.             //,reader:myReader
  21.             //,baseParams:{mod:'data',act:'getAllData'}
  22.             ,fields:[
  23.                     {name:'firstname', type:'string'}
  24.                     ,{name:'lastname', type:'string'}
  25.                 ]
  26.             });
  27.  
  28.   alert("Before Displaying");
  29.   var n = store.getTotalCount();
  30.   alert(n);
  31.  
  32.   var myPanel = new Ext.grid.GridPanel({
  33.          store: store
  34.          ,columns:[{
  35.                 dataIndex:'firstname'
  36.                 ,header:'First Name'
  37.                 ,width:145
  38.                 ,sortable:true
  39.                     },{
  40.                 dataIndex:'lastname'
  41.                 ,header:'Last Name'
  42.                 ,width:145
  43.                 ,sortable:true
  44.                      }
  45.              ],
  46.          viewConfig: {
  47.                 autoFill: true,
  48.                 forceFit: true  
  49.             },
  50.  
  51.         listeners: 
  52.          {
  53.             render: function(grid)
  54.             {
  55.                 grid.store.load();
  56.             }
  57.          }
  58.   });
  59.  
  60.   //store.load({params:{firstname:'Vibha',lastname:'Bhagath'}});
  61.  
  62.   var myWindow = new Ext.Window({
  63.         width:300,
  64.         height:300,
  65.         layout:'fit',
  66.         closable:false,
  67.         resizable:false,
  68.         items:[myPanel]
  69.      });
  70.  
  71.    myWindow.show();
  72.  
  73. });
test.php file:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. $o = array(
  4.    "success" => true,
  5.    "records" => array( {"firstname" => "Vibha" ,
  6.                     "lastname" => "Bhagath"},{"firstname" => "Santu" ,
  7.                     "lastname" => "Sapi"},{"firstname" => "Shivoo" ,
  8.                     "lastname" => "Koteshwar"})
  9.    );
  10. $_SESSION["err"] = isset($_SESSION["err"]) ? !$_SESSION["err"] : true;
  11. header("Content-Type: application/json");
  12. print(json_encode($o));
  13. ?>
Apr 20 '10 #1
Share this Question
Share on Google+
3 Replies


Atli
Expert 5K+
P: 5,058
Hey.

The PHP code you are using is invalid.

The arrays should be valid PHP arrays, not JSON formatted objects. The json_encode function handles the conversion later.

More specifically, the "firstname" and "lastname" groups are invalid. They are JSON formatted, which is not valid PHP syntax. You need to re-format them as valid PHP arrays.
Apr 20 '10 #2

P: 33
how to make them php formattes array Atli.... I'm a newbie... Dont know how to make it. Can you provide me with some example or tutorial for this?
Apr 20 '10 #3

Atli
Expert 5K+
P: 5,058
You could start by reading the manual entry for arrays. That should get you started.

But, simply put, PHP arrays start with the word "array" followed by a list of variables that the array is made up of, enclosed in parenthesis.
For example:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myArray = array( 'value 1', 'value 2', 'value 3' );
  3. echo $myArray[0]; // value1
  4. echo $myArray[1]; // value2
  5. echo $myArray[2]; // value3
  6. ?>
You can specify keys for the array elements by adding them in front of the value, separated by a the => operator.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myArray = array( 
  3.     'first' => 'value 1', 
  4.     'second' => 'value 2', 
  5.     'third' => 'value 3' 
  6. );
  7.  
  8. echo $myArray['first']; // value1
  9. echo $myArray['second']; // value2
  10. echo $myArray['third']; // value3
  11. ?>
Because arrays are, themselves, PHP variables, array elements can be other arrays. This makes the array multidimensional.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myArray = array(
  3.     'first' => array( '1x1', '1x2' ),
  4.     'second' => array( '2x1', '2x2' )
  5. );
  6. echo $myArray['first'][0]; // 1x1
  7. echo $myArray['first'][1]; // 1x2
  8. echo $myArray['second'][0]; // 2x1
  9. echo $myArray['second'][1]; // 2x2
  10. ?>
And there is (virtually) no limit how many arrays you can nest inside another array. You could, for example, do this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $myArray = array(
  3.     '2009' => array( // Year
  4.         '01' => array( // Month
  5.             '01' => array( // Day
  6.                 '00' => array( // Hour
  7.                     '00' => array( // Minute
  8.                         // Seconds
  9.                         '01' => 'First second into 2009',
  10.                         '02' => 'Second second into 2009',
  11.                         '03' => 'Third second into 2009'
  12.                     ),
  13.                     '01' => array(
  14.                         '01' => 'Sixty first second into 2009',
  15.                         '02' => 'Sixty second second into 2009',
  16.                         '03' => 'Sixty third second into 2009'
  17.                     )
  18.                 )
  19.             )
  20.         )
  21.     )
  22. );
  23. ?>
You need to make your array fit into this syntax, or the PHP code won't execute and only give you an error.
Apr 20 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.