0

I'm fairly new to Json and arrays and I'm just wondering whether someone can correct the code I've tried that is still outputting an invalid string.

$json = array(
    'posts' => array(),
);

    while($row = mysqli_fetch_array($result)) {
        $posts = array();
        $posts['count'] = $rowcount;
     $posts['streamitem_id'] = $row['streamitem_id'];


$json['posts'] = $posts;
echo json_encode($json);
}

I have used http://jsonlint.com/ and posted my returned data from firebug errors on lines 6,11 and 16 } {

  {
    "posts": {
        "count": 4,
        "streamitem_id": "1976"
    }
} {
    "posts": {
        "count": 4,
        "streamitem_id": "1980"
    }
} {
    "posts": {
        "count": 4,
        "streamitem_id": "1099"
    }
} {
    "posts": {
        "count": 4,
        "streamitem_id": "1178"
    }
}
4
  • Your $rowcount variable hasn't been initialised. Commented Feb 27, 2017 at 22:00
  • Where are lines 4, 7 and 10? Please post the entire json. Commented Feb 27, 2017 at 22:00
  • $rowcount has been initialised.by $rowcount = mysqli_num_rows($result); just below my query - It wasn't relevent, just added it as it was a varaible that needed to be passed through the json array. Also the lines can be counted from the first line to the last and it will give you the problem lines which are the opening and closing brackets which are now lines 6,11 and 16 Commented Feb 27, 2017 at 22:07
  • Accepted answer below. Thank you everyone. Commented Feb 27, 2017 at 22:11

3 Answers 3

2

Some have already commented with a valid answer however, I believe array_push is the most efficient method rather than redefining the $posts variable over and over.

$json = array(
    'posts' => array()
);

while($row = mysqli_fetch_array($result)) {
    array_push($json['posts'], $array(
        'count' => $rowcount,
        'streamitem_id' => $row['streamitem_id']
        )
    );
}
echo json_encode($json);

PS: I noticed that you're putting $rowcount in each post array. If this is not intended, I recommend the following change:

$json = array(
    'posts' => array(),
    'count' => $rowcount
);

And then in the while loop:

    array_push($json['posts'], $array(
        'streamitem_id' => $row['streamitem_id']
        )
    );

This way, you can reference the post count and it won't be a waste of memory.

console.log(json['count']);
Sign up to request clarification or add additional context in comments.

6 Comments

@Gateway No problem. I made an edit to account for one more thing.
Nice little edit there. Returned string now looks like this ` { "posts": [{ "streamitem_id": "1976" }, { "streamitem_id": "1980" }, { "streamitem_id": "1099" }, { "streamitem_id": "1178" }], "count": 4 }`
Accepted your answer. Brownie points for going the extra mile with the other part.
Probably easier: $json['posts'][] = array('streamitem_id' => $row['streamitem_id']);
Another way to do this (for even further optimization) is to avoid even storing the 'streamitem_id' key at all. Simply: array_push($json['posts'], $row['streamitem_id']); I only say this because you already know that 'posts' contains the stream item ids. Your js object should then look like this: {"posts":["1976","1980","1099","1178"], "count":4} PS: It appears that $json['posts'][] may be faster by microseconds. I recommend doing some research on this if you're interested.
|
1

I think you want this:

$posts = array();
$rowcount = 0;
while ($row = mysqli_fetch_array($result)) {
    $posts[] = array(
       'count' => $rowcount,
       'streamitem_id' = $row['streamitem_id'],
    );
    $rowcount++;
}

$json['posts'] = $posts;
echo json_encode($json);

Comments

1

Put this outside of loop, you are overwriting and echoing each value instead whole at once

Inside loop

      $posts[] = ["count"=> $rowcount,"streamitem_id"=>$row['streamitem_id']);

After loop

  $json['posts'] = $posts;
  echo json_encode($json);

2 Comments

Hi @Niklesh this is now valid json but only returns one record instead of the 4 I've requested within the loop. How can I fix this? Thank you
Updated my answer

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.