How to load data in extjs using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vibhakhushi
    New Member
    • Mar 2010
    • 33

    How to load data in extjs using php

    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:
    Code:
    Ext.onReady(function(){
      alert("inside onReady");
      
      Ext.QuickTips.init();
       
      var employee = Ext.data.Record.create([
          {name:'firstname'},
          {name:'lastname'}]);
          
      //var myReader = new Ext.data.JsonReader({
          //root:"root"
          //},employee);
          
      var store = new Ext.data.JsonStore({
                id:'ID'
                ,root:'root'
                ,totalProperty:'totalCount'
                ,url:'test.php'
                ,autoLoad:true
                //,reader:myReader
                //,baseParams:{mod:'data',act:'getAllData'}
                ,fields:[
                        {name:'firstname', type:'string'}
                        ,{name:'lastname', type:'string'}
                    ]
                });
                
      alert("Before Displaying");
      var n = store.getTotalCount();
      alert(n);
      
      var myPanel = new Ext.grid.GridPanel({
             store: store
             ,columns:[{
                    dataIndex:'firstname'
                    ,header:'First Name'
                    ,width:145
                    ,sortable:true
                        },{
                    dataIndex:'lastname'
                    ,header:'Last Name'
                    ,width:145
                    ,sortable:true
                         }
                 ],
             viewConfig: {
                    autoFill: true,
                    forceFit: true  
                },
    
            listeners: 
             {
                render: function(grid)
                {
                    grid.store.load();
                }
             }
      });
      
      //store.load({params:{firstname:'Vibha',lastname:'Bhagath'}});
             
      var myWindow = new Ext.Window({
            width:300,
            height:300,
            layout:'fit',
            closable:false,
            resizable:false,
            items:[myPanel]
         });
         
       myWindow.show();
        
    });
    test.php file:
    Code:
    <?php
    session_start();
    $o = array(
       "success" => true,
       "records" => array( {"firstname" => "Vibha" ,
                        "lastname" => "Bhagath"},{"firstname" => "Santu" ,
                        "lastname" => "Sapi"},{"firstname" => "Shivoo" ,
                        "lastname" => "Koteshwar"})
       );
    $_SESSION["err"] = isset($_SESSION["err"]) ? !$_SESSION["err"] : true;
    header("Content-Type: application/json");
    print(json_encode($o));
    ?>
    Last edited by Atli; Apr 20 '10, 07:13 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.

    Comment

    • vibhakhushi
      New Member
      • Mar 2010
      • 33

      #3
      PHP formatted array

      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?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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:
        [code=php]<?php
        $myArray = array( 'value 1', 'value 2', 'value 3' );
        echo $myArray[0]; // value1
        echo $myArray[1]; // value2
        echo $myArray[2]; // value3
        ?>[/code]
        You can specify keys for the array elements by adding them in front of the value, separated by a the => operator.
        [code=php]<?php
        $myArray = array(
        'first' => 'value 1',
        'second' => 'value 2',
        'third' => 'value 3'
        );

        echo $myArray['first']; // value1
        echo $myArray['second']; // value2
        echo $myArray['third']; // value3
        ?>[/code]

        Because arrays are, themselves, PHP variables, array elements can be other arrays. This makes the array multidimensiona l.
        [code=php]<?php
        $myArray = array(
        'first' => array( '1x1', '1x2' ),
        'second' => array( '2x1', '2x2' )
        );
        echo $myArray['first'][0]; // 1x1
        echo $myArray['first'][1]; // 1x2
        echo $myArray['second'][0]; // 2x1
        echo $myArray['second'][1]; // 2x2
        ?>[/code]

        And there is (virtually) no limit how many arrays you can nest inside another array. You could, for example, do this:
        [code=php]<?php
        $myArray = array(
        '2009' => array( // Year
        '01' => array( // Month
        '01' => array( // Day
        '00' => array( // Hour
        '00' => array( // Minute
        // Seconds
        '01' => 'First second into 2009',
        '02' => 'Second second into 2009',
        '03' => 'Third second into 2009'
        ),
        '01' => array(
        '01' => 'Sixty first second into 2009',
        '02' => 'Sixty second second into 2009',
        '03' => 'Sixty third second into 2009'
        )
        )
        )
        )
        )
        );
        ?>[/code]

        You need to make your array fit into this syntax, or the PHP code won't execute and only give you an error.

        Comment

        Working...