1

** Updated Code and included full code**

I am currently learning PHP and i have came across this Invalid argument supplied for foreach() error, i have tried everything and dont seem to be getting anywhere, considering i'm learning PHP please be gentle.

    <?php
    require('config/config.php');
    require('config/db.php');

    // Create Query
    $query = 'SELECT * FROM posts ORDER BY created_at DESC';

    // Get Result
    $result = mysqli_query($conn, $query);

    // Fetch Data
    //$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
    var_dump($posts);

    // Free Result
    mysqli_free_result($result);

    // Close Connection
    mysqli_close($conn);
?>





<?php include('inc/header.php'); ?>
    <div class="container">
        <h1>Posts</h1>

        <?php foreach ($posts as $post) : ?>
            <div class="well">
                <h3><?php echo $post['title']; ?></h3>
                <small>Created on <?php echo $post['created_at']; ?> by <?php echo $post['author']; ?></small>
                <p><?php echo $post['body']; ?></p>
                <a class="btn btn-default" href="<?php echo ROOT_URL; ?>post.php?id=<?php echo $post['id']; ?>">Read More</a>
            </div>
        <?php endforeach; ?>
    </div>
<?php include('inc/footer.php'); ?>
Andy Terry
  • 53
  • 6
  • 2
    `$posts` needs to be an array or an object that implements Traversable. You can `var_dump($posts)` to see what's actually in it. – Alex Howansky Mar 23 '18 at 15:09
  • Possible duplicate of [Invalid argument supplied for foreach()](https://stackoverflow.com/questions/2630013/invalid-argument-supplied-for-foreach) – Kasia Gogolek Mar 23 '18 at 15:10
  • now, after your edit, i am missing your question..+ we beside the code, we do need some error messages...but based on your given code, at least you need to remove the uncomment this line "//$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);" – bob Mar 26 '18 at 07:19

3 Answers3

0

Most probably you aren't getting an array in $posts. Try var_dumping it.

And a clean code to handle such situation gracefully would be...

if (is_array($posts) || is_object($posts))
{
    foreach ($posts as $post)
    {
        ...
    }
}
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
0

I guess you forgot to set each element you are trying to display. Make sure you do have a valid array like

<?php
$posts[0]['title']      = '1';
$posts[0]['created_at'] = '2';
$posts[0]['author']     = '3';
$posts[0]['body']       = '4';
?>

One of those ('title', 'created_at', 'author', 'body') is not set in your array.

bob
  • 595
  • 2
  • 18
  • It is connected to a mysql database. – Andy Terry Mar 23 '18 at 15:15
  • as said by me and the other guys..var_dump() the $posts array (or even print_r() them) and you will see which element is missing – bob Mar 23 '18 at 15:22
  • I have added full code for the page. I have var_dump(), i then get Warning: Invalid argument supplied for foreach() in /home/aterry/public_html/index.php on line 26 – Andy Terry Mar 23 '18 at 15:42
0
<?php
    require('config/config.php');
    require('config/db.php');

    // Create Query
    $query = 'SELECT * FROM posts ORDER BY created_at DESC';

    // Get Result
    $result = mysqli_query($conn, $query);

    // Fetch Data

    ////////////////////////////////////////////////////////
    ////    REMOVE THE COMMENT MARK ON THE LINE BELOW   ////
    ////////////////////////////////////////////////////////
    $posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
    var_dump($posts);

?>

If the

$post

is not set, the

foreach

on line 26 will perhaps return the error.

yuko
  • 325
  • 1
  • 8