ANdrww Posted December 12, 2013 Share Posted December 12, 2013 Hello, I have yet another problem I am trying to understand. The thing I don't get is the foreach loop. Here's my code. I have a function to get the posts from the db as follows: function get_posts($connection) { $posts = array(); $sql = "SELECT * FROM posts ORDER BY stamp DESC"; $result = mysqli_query($connection, $sql); while ($post = mysqli_fetch_object($result)) { $posts = array('body' => $post->body, 'stamp' => $post->stamp, 'post_id' => $post->id, 'user_id' => $post->user_id); } return $posts; } And I'm (incorrectly) using a foreach loop to display the results like this: $posts = get_posts($connection); foreach ($posts as $key => $value) { echo $posts['body'] . "<hr>"; } If I echo $posts['body'] I get the first record showing up 4 times. I've used var_dump($posts) to make sure it's an array, and it is, and it shows the same first record 4 times. Any idea? I'm currently re-reading http://www.php.net/manual/en/control-structures.foreach.php to try and understand this. Thanks, Andrei Link to comment https://forums.phpfreaks.com/topic/284730-foreach-loop-problem/ Share on other sites More sharing options...
Ch0cu3r Posted December 12, 2013 Share Posted December 12, 2013 Change $posts = array('body' => $post->body, to $posts[] = array('body' => $post->body, And change the foreach loop to $posts = get_posts($connection); foreach ($posts as $post) { echo $post['body'] . "<hr>"; } Link to comment https://forums.phpfreaks.com/topic/284730-foreach-loop-problem/#findComment-1462192 Share on other sites More sharing options...
JIXO Posted December 12, 2013 Share Posted December 12, 2013 Try foreach ($posts as $key => $value) { echo $value . "<hr>"; } Link to comment https://forums.phpfreaks.com/topic/284730-foreach-loop-problem/#findComment-1462193 Share on other sites More sharing options...
ANdrww Posted December 12, 2013 Author Share Posted December 12, 2013 Change $posts = array('body' => $post->body, to $posts[] = array('body' => $post->body, And change the foreach loop to $posts = get_posts($connection); foreach ($posts as $post) { echo $post['body'] . "<hr>"; } Thanks a lot. I knew there was a simple way. Someone on stackoverflow suggested this but couldnt explain to me why he used a foreach loop twice foreach($posts as $postInfo) { foreach($postInfo as $key => $val) { echo '<p>' .$key . ': ' . $val. '</p>'; } } Link to comment https://forums.phpfreaks.com/topic/284730-foreach-loop-problem/#findComment-1462207 Share on other sites More sharing options...
Ch0cu3r Posted December 12, 2013 Share Posted December 12, 2013 The first foreach loops through each post array within $posts. The second foreach loops over the elements (body, stamp, post_id, user_id) in the post. Link to comment https://forums.phpfreaks.com/topic/284730-foreach-loop-problem/#findComment-1462209 Share on other sites More sharing options...
ANdrww Posted December 12, 2013 Author Share Posted December 12, 2013 I see. Thanks a lot for the quick replies. Link to comment https://forums.phpfreaks.com/topic/284730-foreach-loop-problem/#findComment-1462210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.