View Full Version : Resolved Cannot use object of type blogpost as an array

08-29-2009, 06:50 PM
I had it working but not the way i wanted it to work. I changed this line:

in function getBlogPosts()

to this:


Which is the way I want it otherwise the function doesn't return the objects, it returns the raw sequel data instead. Now I am having trouble outputting it again.

Here is the error I am getting:

Fatal error: Cannot use object of type blogpost as array in C:\wamp\www\blog\index.php on line 34

Here is the class code:

class blogpost{

public $id;
public $title;
public $body;
public $dateposted;

function __construct($inId = null,$inTitle = null, $inBody = null, $inDatePosted = null)
$this->datePosted=$splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];

And here is the index page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
function getBlogPosts ($inId = null)

$query = mysql_query("SELECT * FROM blog_posts ORDER BY id DESC");

$postArray = array();
while($row = mysql_fetch_assoc($query))
$myPost = new BlogPost($row["id"],$row["title"], $row["body"], $row["date_posted"]);
return $postArray;
$blogPosts = getBlogPosts();

foreach($blogPosts as $row){?>

<h2><?php echo $row["id"]; ?></h2>
<h5><?php echo $row["date_posted"]; ?></h5>
<p><?php echo $row["body"]; ?></p>
<hr />
<?php } ?>


Also should the getBlogPosts() function be included in the class or in another include like how I have it now?

08-29-2009, 07:01 PM
$blogPosts is an array of arrays (each inner array is one row from the result set.)

To see what exactly is in $blogPosts, try the following code -

echo "<pre>",print_r($blogPosts,true),"</pre>";

Depending on what you want to do, you would need to iterate over the outer array using a foreach() loop, then each element of the outer array would be a row from the result set that you could then access the elements of.

08-29-2009, 07:17 PM
Thanks that worked! And I just use pointers like this $row=>["title"].

08-29-2009, 08:24 PM
Bump had it working but now have a new problem figured I would reuse the same thread...

08-29-2009, 11:42 PM
Post the question then :P
This here is you're problem:

<h2><?php echo $row["id"]; ?></h2>
<h5><?php echo $row["date_posted"]; ?></h5>
<p><?php echo $row["body"]; ?></p>

$row in this context (within the foreach) represents an object of type blogPost. You're trying to index into it using an array style input.
There are two solutions, the first is to change the accessors from $row['id'] to $row->id, and the same with the other two.
The other is to implement ArrayAccess on you're class, though I recommend not doing that since blogPost itself is not a collection of any sorts.

As for you're question for if you should include the function, yes, I would make it a static method in the class blogPost, or create a new class (that is iterable with ArrayAccess on it) that does the same.

08-30-2009, 12:59 AM
Oh I see I was trying to do this => lol but thank-you.