Search Webmaster Help and Solution

JSON and PHP encode array/dictionary

Hi All,

Just looking for some advice on a problem Im having please. The scenario is that I have a database with a list of blog postings. I want to extract that list using PHP and transfer it to an iPhone app using JSON (I think).

So Im trying to encode something like this into JSON, but dont know whether its possible or the best approach.

messageType : NewContentCheck
Post ID : 1
Date Modified : 8/20/10
Post ID : 2
Date Modified : 8/1/10
Post ID : 10
Date Modified : 8/17/10

etc. etc. Therell be a number of posts from 1 to X and I need the messageType to appear as a header.

Ive been playing around for a few hours now but either dont get a valid JSON string, or I get the messageType printed multiple times.

Code is based on variants of:

$returnedRows = mysql_num_rows($result);

$messageType = "NewContentCheck";
$postObject[messageType] = $messageType;


if ($returnedRows > 0) {
for ($i = 1; $i <= $returnedRows; $i++) {
// Get each individual row of data and create a JSON string of each
$row = mysql_fetch_object($result);
$postObject[posts] = array($row->ID, $row->post_modified);
echo json_encode($postObject);
}


}

Maybe its not possible to encode this as a JSON? I wondered about just sending through lots of individual JSON strings separated with a pipe | and splitting them at the other end.

Any advice gratefully received.

Thanks,

Gareth.

P.S. Fairly new to PHP... : ) Hi All,

Just looking for some advice on a problem Im having please. The scenario is that I have a database with a list of blog postings. I want to extract that list using PHP and transfer it to an iPhone app using JSON (I think).

So Im trying to encode something like this into JSON, but dont know whether its possible or the best approach.

messageType : NewContentCheck
Post ID : 1
Date Modified : 8/20/10
Post ID : 2
Date Modified : 8/1/10
Post ID : 10
Date Modified : 8/17/10

etc. etc. Therell be a number of posts from 1 to X and I need the messageType to appear as a header.

Ive been playing around for a few hours now but either dont get a valid JSON string, or I get the messageType printed multiple times.

Code is based on variants of:

$returnedRows = mysql_num_rows($result);

$messageType = "NewContentCheck";
$postObject[messageType] = $messageType;


if ($returnedRows > 0) {
for ($i = 1; $i <= $returnedRows; $i++) {
// Get each individual row of data and create a JSON string of each
$row = mysql_fetch_object($result);
$postObject[posts] = array($row->ID, $row->post_modified);
echo json_encode($postObject);
}


}

Maybe its not possible to encode this as a JSON? I wondered about just sending through lots of individual JSON strings separated with a pipe | and splitting them at the other end.

Any advice gratefully received.

Thanks,

Gareth.

P.S. Fairly new to PHP... : )

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).