jeni
jeni

Reputation: 983

Invalid argument supplied for foreach() codeigniter

Am getting the error in the code igniter view page as

Invalid argument supplied for foreach() codeigniter in the following code;

<html>
<head>
    <title><?=$page_title?></title>
</head>
<body>
    <?php foreach($result as $row):?>
    <h3><?=$row->title?></h3>
    <p><?=$row->text?></p>
    <br />
    <?php endforeach;?>
</body>

Upvotes: 3

Views: 11655

Answers (2)

aorcsik
aorcsik

Reputation: 15552

Test the $result if it is an array, before using foreach on it. Your result may be false since your database query failed, or returned no result.

if (is_array($result))
{
    foreach($result as $row)
    {
        /* ... */
    }
}

Upvotes: 7

Shakti Singh
Shakti Singh

Reputation: 86406

$result is not an array at all.

You should check the $result construction code. you are not setting $result properly.

If $result is supposed to hold database rows, checkout the database query if that is returning result properly.

Upvotes: 1

Related Questions