...

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



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

$postArray[$x]=$row;
in function getBlogPosts()

to this:

$postArray[$x]=$myPost;

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)
{
if(!empty($inid))
{
$this->id=$inId;
}
if(!empty($inTitle))
{
$this->title=$inTitle;
}
if(!empty($inBody))
{
$this->body=$inBody;
}
if(!empty($inDatePosted))
{
$splitDate=explode("-",$inDatePosted);
$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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
include("conn.php");
include("classes.php");
function getBlogPosts ($inId = null)
{

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


$postArray = array();
$x=1;
while($row = mysql_fetch_assoc($query))
{
$myPost = new BlogPost($row["id"],$row["title"], $row["body"], $row["date_posted"]);
$postArray[$x]=$myPost;
$x++;
}
return $postArray;
}
$blogPosts = getBlogPosts();
?>
</head>

<body>
<?php
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 } ?>



</body>
</html>

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

CFMaBiSmAd
08-29-2009, 06: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.

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

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

Fou-Lu
08-29-2009, 10: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.

firetag
08-29-2009, 11:59 PM
Oh I see I was trying to do this => lol but thank-you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum