Sencha ExtJS 5 and sencha touch Tutorial , by Hery Purnama , Sencha ExtJS Freelance Trainer - +62.81.223344.506 , http://freelance-it-trainer.blogspot.com

What Is Sencha ExtJS : Sencha Ext JS is the leading standard for business-grade web application development. With over 100 examples, 1000 APIs, hundreds of components, a full documentation suite and built in themes, Ext JS provides the tools necessary to build robust desktop applications.


Tuesday, April 7, 2015

Tutorial 3 - Create Data JSON for ExtJS using php Json_encode function

ExtJS using ajax for all data activities , you can use JSON (javascript Object Node) which can easily created by using json_encode function in PHP

  1. Let's start by creating folder data in your ExtJS web project .. wamp\www\ext5-aplikasiku\app\data
  2. Then create files that will be needed for your json
  3. Update your koneksi.php to access your dbextjs database in mysql
    <?php

    $dbhost = "localhost";
    $dbname = "dbextjs";
    $dbuser = "root";
    $dbpwd     = "" ; //my MySQL password is empty

    $koneksi     = mysql_connect($dbhost,$dbuser,$dbpwd) or die("gagal koneksi");
    $mydb         = mysql_select_db($dbname);

    ?>
  4. Update your getcategory.php file
    <?php

    include 'koneksi.php';

    //A) paging start and limit handling in ExtJS
    if(isset($_GET['start'])) {
        $start = $_GET['start'];
        } else {
            $start = 0;
        }

    if(isset($_GET['limit'])) {
    $limit = $_GET['limit'];
        } else {
            $limit= 10;
        }




    //B) Request data to MySQL and put the data in array
    $sql = "select * from tbproductcat order by catid desc Limit $start,$limit";
    $query = mysql_query($sql);

    $jsonRow = array();
    while ($row_data = mysql_fetch_assoc($query)) {
        $jsonRow[] = $row_data;
    };


    //C) Request total row for paging
    $total = mysql_query('select count(*) as totalCol from tbproductcat');
    $total_data = mysql_fetch_assoc($total);


    //D) using json_encode to convert to JSON format

    echo json_encode(array
        (
        "success" => mysql_errno()==0,    //callback
        "total" => $total_data['totalCol'],        //total all data for paging
        "category" => $jsonRow // selector as root in store
        )
    );

    ?>
    Run the JSON file using your browser  (http://localhost/ext5-aplikasiku/data/getcategory.php)
    the result will look like this


    {"success":true,"total":"2","category":[{"catid":"1","catname":"ELEKTRONIK","catstatus":"1"},{"catid":"2","catname":"OTOMOTIF","catstatus":"1"}]}
  5.  Update your getproduct.php script
    <?php

    include 'koneksi.php';

    //A) paging start and limit handling in ExtJS
    if(isset($_GET['start'])) {
        $start = $_GET['start'];
        } else {
            $start = 0;
        }

    if(isset($_GET['limit'])) {
    $limit = $_GET['limit'];
        } else {
            $limit= 10;
        }




    //B) Request data to MySQL and put the data in array
    $sql = "select * from tbproductsales order by prdid desc Limit $start,$limit";
    $query = mysql_query($sql);

    $jsonRow = array();
    while ($row_data = mysql_fetch_assoc($query)) {
        $jsonRow[] = $row_data;
    };


    //C) Request total row for paging
    $total = mysql_query('select count(*) as totalCol from tbproductsales');
    $total_data = mysql_fetch_assoc($total);


    //D) using json_encode to convert to JSON format

    echo json_encode(array
        (
        "success" => mysql_errno()==0,    //callback
        "total" => $total_data['totalCol'],        //total all data for paging
        "product" => $jsonRow // selector as root in store
        )
    );

    ?>
  6.  Update your getuser.php script
    <?php

    include 'koneksi.php';

    //A) paging start and limit handling in ExtJS
    if(isset($_GET['start'])) {
        $start = $_GET['start'];
        } else {
            $start = 0;
        }

    if(isset($_GET['limit'])) {
    $limit = $_GET['limit'];
        } else {
            $limit= 10;
        }




    //B) Request data to MySQL and put the data in array
    $sql = "select * from tbusers order by userid desc Limit $start,$limit";
    $query = mysql_query($sql);

    $jsonRow = array();
    while ($row_data = mysql_fetch_assoc($query)) {
        $jsonRow[] = $row_data;
    };


    //C) Request total row for paging
    $total = mysql_query('select count(*) as totalCol from tbusers');
    $total_data = mysql_fetch_assoc($total);


    //D) using json_encode to convert to JSON format

    echo json_encode(array
        (
        "success" => mysql_errno()==0,    //callback
        "total" => $total_data['totalCol'],        //total all data for paging
        "appuser" => $jsonRow // selector as root in store
        )
    );

    ?>


Copyright
Hery Purnama
http://freelance-it-trainer.blogspot.com

No comments:

Post a Comment