0

I have the following code which generates a good json output but it has '[' and ']' at the start and end respectively. You can also check the output at

http://android.ezinfotec.com/functions.php?method=getquestions

$query = mysql_query("select * from questions");
$i = 0;
        while($fetch = mysql_fetch_array($query))
        {

$output[] = array (
            "row".$i => array   (
                "id" => $fetch['id'],
                "answers" => $fetch['answers'],
                "status" => $fetch['ans_status'],
                "postedon" => substr($fetch['month'],0,3).' '.$fetch['day'].' '.$fetch['year'],
                "question" => $fetch['question'],
                "category" => $fetch['category'],
                "parent" => $fetch['parentcategory'],
                "authorid" => $fetch['author'],
                "authorname" => $fetch['author_name']   
                                )                   
                );
            $i++;           
        }
echo json_encode($output);

Here is my html page which generate [Object object] when I try to get the data from the url

$(function() {
$('#getdata').click(function(){
        $.ajax({
        url: 'http://localhost:8080/android/functions.php',
        type : 'GET',
        data : 'method=getquestions',           
        dataType : 'json',
        success : function(s) {
            alert(s);
        },
        error: function(XMLHttpRequest,textStatus,errorThrown)
        {
            console.log(XMLHttpRequest+' '+textStatus+' '+errorThrown);
        }
    });
});

});

I think it is because of the '[' and ']' I'm not able to access data using ajax. IN the success function i get [Object object]

3 Answers 3

1

ok i got it you have to change your javascript to this code as below and let your php code to be run as it is you have written

success : function(s) {
                        for(var i =0 ;i < s.length; i++){
                              alert(s[i]["row"+i].id);
                              alert(s[i]["row"+i].answers);
                              alert(s[i]["row"+i].status);
                              alert(s[i]["row"+i].postedon);
                              alert(s[i]["row"+i].question);
                              alert(s[i]["row"+i].category);
                              alert(s[i]["row"+i].parent);
                              alert(s[i]["row"+i].authorid);
                              alert(s[i]["row"+i].authorname);
                       } 
                     }
Sign up to request clarification or add additional context in comments.

14 Comments

since $Output is not a variable only the last record is shown. Still the '[' and ']' persist
Please check it i have change my answer as above
ok add i as index to row as above and try it such as alert(s[i].row[i].id);
no just add index to row as above and try it such as alert(s[i].row[i].id); As you are providing index i to row in your php code. Please see i have edit the answer as above.
i got it working this is the code which works alert(t[i]["row"+i].id);. Sorry for the late reply but i was testing the code and its working
|
1

Your output is currently of the form:

[
    {
        row0:
        {
            id: "17",
            answers: "0",
            status: "0",
            postedon: "Dec 4 2013",
            question: "How many courses within Aqeedah?",
            category: "91",
            parent: "89",
            authorid: "14",
            authorname: "Mehrakbar"
        }
    },
    {
        row1:
        {
            id: "18",
            answers: "0",
            status: "0",
            postedon: "Dec 10 2013",
            question: "what is jinn?",
            category: "97",
            parent: "89",
            authorid: "53",
            authorname: "Jack"
        }
    },
    etc
]

If the beginning and ending [ ] brackets were switched to { } braces, then your output will no longer be valid JSON. What do you want the output to look like?

If it's

{
    row0:
    {
        blah: blah
        blee: blah
    },
    row1:
    {
        blah: blah
        blee: blah
    }
}

that you are looking for, then change the

$output[] = array (
        "row".$i => array   (

to a

$output['row'.$i] = array (

and clean up the the corresponding parantheses.

1 Comment

Cool, now '[' and ']' are removed. But i'm not able to access the data via s.row0.id
0
$query = mysql_query("select * from questions");
$i = 0;
$output = array(); // add this line

1 Comment

do you get any data of variable s in success : function(s)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.