Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to display data(json) in my site using AngularJs . here's what i did :
Create a database in phpmyAdmin .
Create a table with 2 row , subject and body . Should i create an id ?
After doing with PHP and angular , I got JSON like this :

[{
    "0":"Soheil","subject":"Soheil",
    "1":"Sadeghbayan","body":"Sadeghbayan"}
    ,{"0":"","subject":"","1":"","body":""}
    ,{"0":"","subject":"","1":"","body":""}
    ,{"0":"dasdasd","subject":"dasdasd","1":"qe","body":"qe"}
    ,{"0":"Hello","subject":"Hello","1":"This is chandler !","body":"This is chandler !"}
    ,{"0":"","subject":"","1":"","body":""},
    {"0":"Something new in website","subject":"Something new in website","1":"oh Awsome !","body":"oh Awsome !"
}]

I think this is invalid JSON because when I replace it with custom JSON that I wrote it work .

Json valid

{
    "fruits": [
        {
            "id": "1",
            "name": "Apple"
        },
        {
            "id": "2",
            "name": "Orange"
        }
    ]
}  

AngularJS

var fruitsApp = angular.module('fruitsApp', []);

fruitsApp.factory('fruitsFactory', function($http) {
    return {
        getFruitsAsync: function(callback) {
            $http.get('fruits.json').success(callback);
        }
    };
});

fruitsApp.controller('fruitsController', function($scope, fruitsFactory) {
    fruitsFactory.getFruitsAsync(function(results) {
        console.log('fruitsController async returned value');
        $scope.fruits = results.fruits;
    });
});

Html

<ul>
            <li ng-repeat="fruit in fruits">
                {{fruit.subject}} is {{fruit.body}}
            </li>
</ul>  

php

    include('config.php');



$data = json_decode(file_get_contents("php://input"));
$subject = mysql_real_escape_string($data->subject);
$body = mysql_real_escape_string($data->body);
mysql_select_db("angular") or die(mysql_error());
mysql_query("INSERT INTO newstory (subject,body) VALUES ('$subject', '$body')");
Print "Your information has been successfully added to the database.";



$query = "SELECT * FROM newstory";
$result = mysql_query($query);

$arr = array();
while ($row = mysql_fetch_array($result)) {
    $subject = $row['subject'];
    $body = $row['body'];
    $arr[] = $row;
}
echo json_encode($arr);

Any idea ? Thx in advance

share|improve this question
    
I tested your json using the following url and it is valid: jsonformatter.curiousconcept.com Just as Puttzy just stated in an answer. So seeing as the json is valid, perhaps your question is different. What is it you wish to do with this json and what did you already make in javascript? –  Blackunknown Oct 7 '14 at 12:40
    
Your json is valid: jsonlint.com. Also are you using $resource? beause if your php is returning an array, the normal query need an object. so either you can change 'query': {method:'GET', isArray:true} in your js or you can do a exit(json_encode($value,JSON_FORCE_OBJECT)); in the php –  Stefanos Chrs Oct 7 '14 at 12:42
    
Thx guys,i updated my question with json valid –  user3856699 Oct 7 '14 at 12:45
    
Both your jsons are valid. However if you wrote code to work with the 2nd one, of course your first one isn't going to get through it correctly. –  Blackunknown Oct 7 '14 at 12:45
    
why ?? maybe this is my mistake !! i wrote angular code in my question –  user3856699 Oct 7 '14 at 12:47

1 Answer 1

Your JSON is a valid. Refer to this for information on JSON and this to check/validate a JSON object.

The data coming back from your $http.get / database data does not have a fruits attribute and you expect that when you set your $scope.fruits (the below snippet is taken from your code):

 $scope.fruits = results.fruits;

The structure of the data that is being returned by the $http.get call is different than the format of your sample data.

Here's your $http.get / database data (I shortened it for brevity):

[
{
    "0": "Soheil",
    "1": "Sadeghbayan",
    "subject": "Soheil",
    "body": "Sadeghbayan"
},
{
    "0": "Hello",
    "1": "This is chandler !",
    "subject": "Hello",
    "body": "This is chandler !"
},
{
    "0": "",
    "1": "",
    "subject": "",
    "body": ""
}
]

And here's your sample / mock data:

{
"fruits": [
    {
        "id": "1",
        "name": "Apple"
    },
    {
        "id": "2",
        "name": "Orange"
    }
]
}

The former is an array of objects with keys: 0, 1, subject and body. The latter is an object with keys: fruits.

They are both valid JSON objects with different object structures. But, you expect a fruits attribute where there isn't one. Also, your HTML/UI might be expecting the data format to look like what is in your mock data. So check that too.

share|improve this answer
    
thx maaan for doing this , i see what you mean , i changed $scope.fruits = results.fruits; to $scope.fruits = results and it worked correctly , but when i replace fruit.json to insert.php it just showed bullet from li in html –  user3856699 Oct 7 '14 at 13:27
    
That's probably b/c your html logic is expecting "id" and "name" (the attributes in the fruits array from your mock data). But, instead your attributes are 0, 1, subject and body. –  whyceewhite Oct 7 '14 at 13:37
    
would you please see my question,i updated with html and php –  user3856699 Oct 7 '14 at 13:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.