Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Populating a JSON array with external PHP.

Hey everyone.

I am having an issue with my application. I have a populated JSON with static data, and it works properly. I have a PHP script working where it creates a JSON, but I need to return it into the JSON on my existing page.

Here is my code:

<?php
//$link line here, it connects properly to the database.
mysql_select_db("erichol2_fitmakr") or die("Could not select database");

$userID = $_GET['userID'];
$arr = array();
$rs = mysql_query("SELECT * FROM tbl_routines WHERE user_id=".$userID);

while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}

echo '{"routines":'.json_encode($arr).'}';

?>

This creates a JSON.


This is the codebit from my .js file:

var data = [
{title:'Eric: Balanced Fitness', hasChild:true, test:'../windows/listDays.js'},
{title:'Eric: Cutting', hasChild:true, test:'../windows/listDays.js'},
{title:'Eric: Mass Gain', hasChild:true, test:'../windows/listDays.js'}
];

I tried using this code, but I am not sure if I am on the right track..

var data2 = $.get('http://ericholmes.ca/getRoutines.php', data: {'userID': 14});

Thanks in advance!

— asked April 19th 2010 by Eric Holmes
  • ajax
  • dynamic
  • external
  • json
  • php
  • population
0 Comments

4 Answers

  • Hello Eric,

    does the php-file return your result? I've just written a similar application:

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function() {
        var data=[];
        var json = JSON.parse(this.responseText);               
        var jsnrows = json.rows;
    };
    xhr.open("GET", "http://localhost/ajax/pdo.php");
    xhr.send();
    

    Hopefully I could help you :)

    — answered April 19th 2010 by Claus Moritz Gräf
    permalink
    0 Comments
  • The PHP file does return the values successfully in JSON format. Where would I put this code in? Would it be replacing where I declare the values?

    — answered April 19th 2010 by Eric Holmes
    permalink
    0 Comments
  • You put this js-code in the app.js. And this file fetches the JSON from the php file. Maybe you try it?!

    — answered April 20th 2010 by Claus Moritz Gräf
    permalink
    0 Comments
  • Sorry Clause, I ran into some issues where my project actually was erased, and I cannot find my backup USB drive anywhere. I have been working on getting it back to its original state.

    I tried the code, but it is saying it has issues finding the variable 'data'. Where does 'data' get populated in this code? Should I be using 'json' instead, to populate my array?

    — answered April 21st 2010 by Eric Holmes
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.