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!
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 :)
-
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?
-
You put this js-code in the app.js. And this file fetches the JSON from the php file. Maybe you try it?!
-
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?