I was trying to copy this code:
<?php
foreach ($products as $product) {
$id = $product['id'];
$name = $product['name'];
$description = $product['description'];
$price = $product['price'];
?>
<tr>
<td><img src="<?php echo $product['picture']; ?>" /></td>
<td><b><?php echo $name; ?></b><br />
<?php echo $description; ?><br />
Price:<big style="color:green">
$<?php echo $price; ?></big><br /><br />
<?php
echo form_open('cart/add');
echo form_hidden('id', $id);
echo form_hidden('name', $name);
echo form_hidden('price', $price);
echo form_submit('action', 'Add to Cart');
echo form_close();
?>
</td>
</tr>
<tr><td colspan="2"><hr size="1" /></td>
<?php
}
?>
and here is my code:
<?php
foreach ($blogs as $blog) {
$id = $blog['id'];
$title = $blog['title'];
$content = $blog['content'];
?>
<h1><?php echo $title; ?></h1>
<h1> <?php echo $content; ?> </h1>
<?php
}
?>
I get this error every time I run my code: "Cannot use object of type stdClass as array"
There might two issues
1) $blogs may be a stdObject
or
2) The properties of the array might be the stdObject
Try using var_dump($blogs) and see the actual problem if the properties of array have stdObject try like this
$blog->id;
$blog->content;
$blog->title;
If you're iterating over an object instead of an array, you'll need to access the properties using:
$id = $blog->id;
$title = $blog->title;
$content = $blog->content;
That, or change your object to an array.
$blog is an object, not an array, so you should access it like so:
$blog->id;
$blog->title;
$blog->content;
StdClass object is accessed by using ->
foreach ($blogs as $blog) {
$id = $blog->id;
$title = $blog->title;
$content = $blog->content;
}
$blog is an object not an array
try using $blog->id
instead of $blog['id']
Source: Stackoverflow.com