Hello I am wondering how I would attempt to parse the data I retrieved from the database:
[
{
"q_id":"1",
"type":"for",
"author":"kappa420",
"question":"what is the meaning of life?",
"answer":"good question."
},
{
"q_id":"2",
"type":"recursive",
"author":"kappa420",
"question":"If only you knew",
"answer":"That I was right behind you!"
},
{
"q_id":"3",
"type":"while",
"author":"kappa420",
"question":"who are you?",
"answer":"ha-HA!"
},
{
"q_id":"5",
"type":"testtyte",
"author":"testauthor",
"question":"testquestion",
"answer":""
},
{
"q_id":"6",
"type":"testtyte",
"author":"testauthor",
"question":"testquestion",
"answer":"testanswer"
},
{
"q_id":"7",
"type":"public int add( int num1,",
"author":"",
"question":"Given two numbers the user must be able to add these numbers. ",
"answer":" add(5,4) = 9
add(2,3) = 5"
},
{
"q_id":"8",
"type":"public int add( int num1,",
"author":"",
"question":"Given two numbers the user must be able to add these numbers. ",
"answer":" add(5,4) = 9
add(2,3) = 5"
},
{
"q_id":"9",
"type":"dasdsa",
"author":"",
"question":" adddfa",
"answer":" asdfs"
},
{
"q_id":"10",
"type":"adfafd",
"author":"",
"question":" dsadsf",
"answer":" saddasf"
},
{
"q_id":"11",
"type":"adsf",
"author":"",
"question":" adsfs",
"answer":" dsadsf"
}
]
Is there way to get certain elements like q_id and author and put them in html attributes?
You can parse your JSON into a JavaScript array using the command JSON.parse('{JSON HERE}')
and you could do a for loop of your array and create an HTML element for them like so:
$.each(arrayVar, function(i, obj) {
$element = $('<div></div>');
$element.attr('id') = obj['q_id'];
$('body').append($element);
});
I hope this is what you are looking for!
$results = //db result
foreach($results as $result){
echo $result['q_id'];
}
Check http://www.w3schools.com/php/php_arrays_multi.asp Array => key => value
// if its json
$results = json_decode(dbresult);
foreach($results as $result){
echo $result['q_id'];
}
This is a json string, it is very easy to parse this into an array using php.
$DataArray = json_decode($MyDatabaseString,True);