Hallo Scripters,

Ik ben bezig met een soort blogsysteem maar nu heb ik een vraagje!

<?php

$post = json_decode( file_get_contents( 'json/post.json' ), true);

foreach ($post['post'] as $post) {
	echo '
		<p>
			', $post['title'], '<br />
			', $post['author'], '<br />
			', $post['content'],'
		</p>';
}

?>

<html>
<head>
	<title>Blog Posts With JSON</title>
</head>
<body>
<form action="getpost.php" method="get">
	<input type="text" name="post" />
	<input type="submit" value="Get" />
</form>

</body>
</html>


ik heb dit script geschreven maar het werkt niet!
Ik krijg steeds deze fout: Warning: Invalid argument supplied for foreach() in C:\localhost\project\blog-posts-with-json\index.php on line 5.

Wat kan ik hieraan doen?

Mijn JSON:

"post":{
    "0001": {
        "title":"A Nice Title #1",
        "author":"John Doe",
        "content":"Hey, You can edit all of this stuff in post.json!"
    },
    "0002": {
        "title":"Posts With JSON",
        "author":"John Smith",
        "content":"Hello, This is another post made with JSON!"
    }
}


Ik ben net een beginner als het ligt aan JSON in php te verwerken. Wat eigenlijk mijn bedoeling is dat hij voor elke post hetzelfde stukje laat zien maardan met andere content voor elke post.

- Pascal Gerrist
Een foreach moet een array krijgen als parameter. Je zou dus voorafgaand aan de foreach kunnen controleren of $post['post'] wel bestaat en of het wel een array is.

Daarnaast zal met de json die jij geeft $post['post'] nooit een array zijn maar een object. in json is {} een object en een array schrijf je tussen [ en ].
Hallo Frank,

Sorry maar ik snap er niet heel veel van omdat ik net een php beginner ben :)

Zou je misschien het voorbeeldje willen uitwerken? Ik hoop dat ik er dan nog wat van leer!

- Pascal Gerrist
<?php

$json = '[
{
"title":"A Nice Title #1",
"author":"John Doe",
"content":"Hey, You can edit all of this stuff in post.json!"
},
{
"title":"Posts With JSON",
"author":"John Smith",
"content":"Hello, This is another post made with JSON!"
}
]';


$arr = json_decode($json);

// laat de structuur eens zien
echo '<pre>';
print_r($arr);
echo '</pre>';

// foreach voorbeeld
foreach($arr as $item)
{
echo $item->title . '<br>';
echo $item->author . '<br>';
echo $item->content . '<br><br>';
}
?>

en voorbeeld 2:
<?php

$json = '{
"post":[
{
"title":"A Nice Title #1",
"author":"John Doe",
"content":"Hey, You can edit all of this stuff in post.json!"
},
{
"title":"Posts With JSON",
"author":"John Smith",
"content":"Hello, This is another post made with JSON!"
}
]
}';


$arr = json_decode($json);

// laat de structuur eens zien
echo '<pre>';
print_r($arr);
echo '</pre>';

// foreach voorbeeld
foreach($arr->post as $item)
{
echo $item->title . '<br>';
echo $item->author . '<br>';
echo $item->content . '<br><br>';
}
?>
Bedankt! Is het ook nog mogelijk dat alle posts een id hebben?
Oplopend van 1 tot het einde?

Dan kan je toch steeds $i++ doen in het foreach, en voor je foreach $i = 0; defineren.

Reageren