Reputation:
I have a PHP file that returns a JSON array. I need to extract the array name, in this instance, *Regulatory_Guidance_Library* with jQuery as a var to $('#navHeaderTitle').text(arrayName);
of my function. I don't know how this is done.
Thnx for your help
My Function in the HTML Doc:
function loadNav(url, container, appendE) {
$.getJSON(url, function(data){
var arrayName = "";
$.each(data.Regulatory_Guidance_Library, function(){
var newItem = $('#' + container).clone();
// Now fill in the fields with the data
newItem.find("h1").text(this.label);
newItem.find("h2").text(this.title);
newItem.find("p").text(this.description);
// And add the new list item to the page
newItem.children().appendTo('#' + appendE)
});
$('#navHeaderTitle').text(arrayName);
//transition(pageName, "show");
});
};
The served PHP:
<?php
//MySQL Database Connect
include 'andaerologin.php';
mysql_select_db("andaero");
$sql=mysql_query("select * from regulatory_list");
$output = new stdClass();
while($row = mysql_fetch_assoc($sql)) {
$output->Regulatory_Guidance_Library[] = $row;
}
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
echo (json_encode($output));
mysql_close();
?>
Upvotes: 0
Views: 236
Reputation: 171690
Modify php:
$output = array();
/* used "key" as name...could change to suit your app*/
$output['key']='Regulatory_Guidance_Library';
while($row = mysql_fetch_assoc($sql)) {
$output['items'][]= $row;
}
AJAX:
$.getJSON(url, function(data){
$.each(data.items, function(){
var newItem = $('#' + container).clone();
// Now fill in the fields with the data
newItem.find("h1").text(this.label);
newItem.find("h2").text(this.title);
newItem.find("p").text(this.description);
// And add the new list item to the page
newItem.children().appendTo('#' + appendE)
});
$('#navHeaderTitle').text(data.key);
//transition(pageName, "show");
});
Upvotes: 1
Reputation: 929
The other answer is probably more helpful, but if you want to get the names of an object's properties with javascript you can do:
for (var name in data) {
alert(name);
}
In your case, since you have only one property you could do:
for (var name in data) {
$('#navHeaderTitle').text(name);
}
Upvotes: 1