1. #1
    Sencha User roymj88's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    19
    Vote Rating
    0
    roymj88 is on a distinguished road

      0  

    Wink Unanswered: How to get values from database??

    Unanswered: How to get values from database??


    Hi,

    I am just exploring the sencha world.I am creating my first app using sencha. Can someone point me as to how i can get the data from the database using php and json. Ive got a code which i will write down.

    Code:
    function showRecent() {
                Ext.Ajax.request({
                    url: 'server.php?action=get',
                    success: function(e) {
                        var obj = Ext.util.JSON.decode(e.responseText);
                        var guests = obj.guests;					
                        if (guests) {						
                            var html = tpl.applyTemplate(guests);
                            Ext.getCmp('recentTab').update(html);
                        }
                    }
                });
            }
    Code:
    if (isset($_GET['action']) && $_GET['action'] == "get") {
            $sql = "SELECT * FROM messages";
            $guests = array();
            foreach ($dbh->query($sql) as $row)
            {
                $guests[] = array(
                    'id' => $row['id'],
                    'name' => $row['name'],
                    'email' => $row['email'],
                    'url' => $row['url'],
                    'message' => $row['message'],
                    'date' => $row['date_entry']
                );
            }
            echo json_encode(array('success' => true, 'guests' => $guests));
        }

    I am not able to get individual date from the array. Its always saying undefined.

    Also can i declare a global variable and use it throughout the project?

    I am creating a login page and using that information which i get from login, i need to fill up contents in the app. So i would need the id throughout. Or is it possible to send the value from one panel to another.

    Thanks

    Roy M J

  2. #2
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,681
    Answers
    130
    Vote Rating
    112
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    as a first step: think about which framework you are using:
    Sencha Touch 1
    ST 2
    Ext JS 3
    Ext JS 4

    for example! then please use the matching forum(s) to ask questions there.
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

  3. #3
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,681
    Answers
    130
    Vote Rating
    112
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    do you have a success property set, like success:true inside of your JSON?
    if yes, post your json-serverresponse.
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

  4. #4
    Sencha User roymj88's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    19
    Vote Rating
    0
    roymj88 is on a distinguished road

      0  

    Default


    Hi,

    Thanks for replying..

    Sorry I am using secha touch 2.

    And yes i am setting success as true. But in the js file im not sure how to get values from the array.

    Code:
    echo json_encode(array('success' => true, 'guests' => $guests));
    The js part is as follows :

    Code:
    
    function showRecent() {
                Ext.Ajax.request({
                    url: 'server.php?action=get',
                    success: function(e) {
                        var obj = Ext.util.JSON.decode(e.responseText);
                        var guests = obj.guests;					
                        if (guests) {						
                            var html = tpl.applyTemplate(guests);
                            Ext.getCmp('recentTab').update(html);
                        }
                    }
                });
            }
    Many thanks

    Roy

  5. #5
    Sencha User roymj88's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    19
    Vote Rating
    0
    roymj88 is on a distinguished road

      0  

    Default


    What i would like to know is how i can get the individual values from variable guests. Till now i could not figure out how to get the values.

    Another thing is once i get the value will it be possible to declare it as a global variable so that i can use that value throughout the app?. Or is it good for me to use php session variablesfor this purpose.

    Thanks

    Roy

  6. #6
    Sencha User tobiu's Avatar
    Join Date
    May 2007
    Location
    Munich (Germany)
    Posts
    2,681
    Answers
    130
    Vote Rating
    112
    tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all tobiu is a name known to all

      0  

    Default


    what do you get when logging the response, like:

    Code:
    success: function(response) {
        console.log(response.responseText);
    }
    global vars are a quite bad coding style.

    in general, after taking a closer look at your usecase.
    when you want to get an array of guests from your server,
    a single ajax call is not the best way to go.

    did you take a look at:
    http://docs.sencha.com/touch/2-0/#!/api/Ext.data.Store

    with using a store you can easily access your data, sort, filter, modify etc. it.
    Best regards
    Tobias Uhlig
    __________

    S-CIRCLES Social Network Engine

  7. #7
    Sencha User roymj88's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    19
    Vote Rating
    0
    roymj88 is on a distinguished road

      0  

    Default


    Hi,

    Yes i got the complete list of data from the database.

    The problem is i'm new to this and bit hesitant into jumping inside everything as i may loose track of the flow easily. I am in the process of creating a simple login and i just want to store one id in a variable throughout the app. So should i go for store or should i go for normal variable declaration?


    I am in the process of learning sencha and im loving it to core. But please bear my questions as my knowledge is limited at this time.

    Thanks

    Roy M J

  8. #8
    Sencha User
    Join Date
    Apr 2013
    Posts
    2
    Vote Rating
    0
    hwanile is on a distinguished road

      0  

    Default


    Hi, I am sorry for taking a time of any one in this blog. I am new with sencha touch and I want to create a sencha project inside a visual studio. I know how to create a sencha project but not in visual studio. Within the project I need to have a database where I can connect and or login to move data around. I have been working with this for over a week now but no luck. Please any help will be appreciated, thanks in advance

Thread Participants: 2