1. #1
    Sencha User ThrowsException's Avatar
    Join Date
    Sep 2011
    Posts
    16
    Vote Rating
    0
    ThrowsException is on a distinguished road

      0  

    Default returning json_encode

    returning json_encode


    Hey everyone I was hoping someone could help solve a problem where I'm trying to return a query using json_encode. I'm using the MVC tutorial on the site and trying to use the direct classes. my query looks like this
    Code:
    $rows = array();
            while ($row = mysql_fetch_assoc($result)) {
                $rows[] = $row;
            }
            $data = json_encode($rows);
            return array(
                'data' => $data
            );
    and my store looks like
    Code:
    Ext.define('Application.store.Scorecards',{
        extend: 'Ext.data.Store',
        model: 'Application.model.Scorecard',
        autoLoad: true,
        proxy: {
            type: 'direct',
            directFn: Ext.emptyFn,
            reader: {
                type: 'json',
                root: 'data',
            }
        }
    });
    and my model
    Code:
    // JavaScript Document
    Ext.define('Application.model.Scorecard' ,{
        extend: 'Ext.data.Model',
        fields: ['Id','Name','Owners'],
        validations: [{
            type: 'length',
            field: 'Name',
            min: 1
        }, {
            type: 'length',
            field: 'Owners',
            min: 1
        }], 
    });
    when I run the app I just get a grid with a bunch of empty rows and my router class returns this

    [{"type":"rpc","tid":1,"action":"Scorecard","method":"getScorecards","result":{"data":"[{\"Id\":\"1\",\"Name\":\"First Scorecard\",\"Owners\":\"Me\"}]"}},{"type":"rpc","tid":2,"action":"Scorecard","method":"getScorecards","result":{"data":"[{\"Id\":\"1\",\"Name\":\"First Scorecard\",\"Owners\":\"Me\"}]"}}]

  2. #2
    Sencha User ThrowsException's Avatar
    Join Date
    Sep 2011
    Posts
    16
    Vote Rating
    0
    ThrowsException is on a distinguished road

      0  

    Default Tweaks

    Tweaks


    So I'm working on this more now and I notice the JSON object my router returns is

    "{"Id":"1","Name":"First Scorecard","Owners":"Me"}"

    Thats the entry that is in the 'data' array that comes back.

    However I noticed some tutorials the JSON returned inside the array looks like

    Object {id=1, name="John Doe", email="john@doe.com", ...}

    Instead of a bunch of blank rows I'm now getting the correct number of rows, 1 in this case, but it's still blank.

  3. #3
    Sencha User ThrowsException's Avatar
    Join Date
    Sep 2011
    Posts
    16
    Vote Rating
    0
    ThrowsException is on a distinguished road

      0  

    Default Workaround for now....or is this what everyone else does?

    Workaround for now....or is this what everyone else does?


    so instead of trying to use Json_encode and having no luck I just changed my query to this.

    Code:
    $rows = array();
    while ($row = mysql_fetch_assoc($result)) {
            $rows[] = array('Id' => $row["Id"],
                                'Name' => $row["Name"],
                                'Owners' => $row["Owners"]);
            }
            return array(
                'data' =>  $rows,
                'success' => TRUE
            );
    So its working now I just feel like there's a better way to do it. If anyone has any improvements and ideas feel free to share.

  4. #4
    Sencha User
    Join Date
    Jun 2009
    Posts
    102
    Vote Rating
    6
    campersau1 is on a distinguished road

      0  

    Default


    I think that should work:

    Code:
    $rows = array();
    while ($row = mysql_fetch_assoc($result)) {
      $rows[] = $row;
    }
    return array(
      'data' =>  $rows,
      'success' => TRUE
    );
    havent't programmed in php for quite some time.

Thread Participants: 1

Tags for this Thread