phpjsonextjsextjs5extjs-mvc

Ext js Grid with PHP no data output


hope can you help me out to my problem i dont know what to add code to create a simple grid with PHP, no data will output to the grid i saw these in tutorial so i create a php file that output json then i create a grid and store here is my code

//here is my store

Ext.define('Active.store.Employee', {
      extend: 'Ext.data.JsonStore',

      alias: 'store.employees',

    proxy: {

        type: 'ajax',
        url: 'test.php',
        reader: {
            type: 'json',
            rootProperty: 'data'
        }
    },

       fields: ['name', 'bounty', 'power' ],
       autoLoad:true,
});

//here is my grid

     Ext.define('Active.view.main.Grid', {
    extend: 'Ext.Panel',
    xtype: 'grid',

require:['Active.view.main.Employee','Active.store.Employee'],

 items:[{
            style: 'padding-top: 10px;',
            xtype: 'gridpanel',
            style: 'margin-top:5px;margin-left:10px;',
            columns : {
                defaults: {
                    sortable: false,
                    menuDisabled: true
                },
                items: [
                    { text: 'Name', dataIndex: 'name',width:'20%' },
                    { text: 'Bounty', dataIndex: 'bounty',width:'20%'},
                    { text: 'Power', dataIndex: 'power',width:'20%'},

                        ]
            },
            store: {type: 'employees',},
            width: '100%'

        }],


  });   

//here is my PHP code

<?php

 mysql_connect("localhost", "root", "") or
  die("Could not connect: " . mysql_error());
  mysql_select_db("test_db");




$query= "SELECT * FROM pirate";
$result= mysql_query($query);

    $return_arr= array();

    while($rows = mysql_fetch_array($result, MYSQL_ASSOC)){
            $row_array['id']=$rows['id'];
            $row_array['name']=$rows['name'];
            $row_array['bounty']=$rows['bounty'];
            $row_array['power']=$rows['power'];


            array_push($return_arr, $row_array);
}




$ans = array();
$ans['data'] = $return_arr;
header('Content-Type: application/json');
print json_encode($ans);
exit;

?>

Solution

  • You're almost there. data should also be part of your json answer. Also you might want to include the content-type in your reply header:

    $query = "SELECT * FROM pirate";
    $result = mysql_query($query);
    
    $return_arr = array();
    
    while($rows = mysql_fetch_array($result, MYSQL_ASSOC)){
        $row_array['id'] = $rows['id'];
        $row_array['name'] = $rows['name'];
        $row_array['bounty'] = $rows['bounty'];
        $row_array['power'] = $rows['power'];
        array_push($return_arr, $row_array);
    }
    
    $ans = array();
    $ans['data'] = $return_arr;
    header('Content-Type: application/json');
    print json_encode($ans);
    exit;