Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post

    Cannot use object of type blogpost as an array

    I had it working but not the way i wanted it to work. I changed this line:
    PHP Code:
    $postArray[$x]=$row
    in function getBlogPosts()

    to this:
    PHP Code:
    $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:
    Code:
    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:

    PHP 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:

    Code:
    <!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?
    Last edited by firetag; 08-30-2009 at 02:12 AM.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,059
    Thanks
    2
    Thanked 317 Times in 309 Posts
    $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 -

    PHP 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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    firetag (08-29-2009)

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post
    Thanks that worked! And I just use pointers like this $row=>["title"].

  • #4
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post
    Bump had it working but now have a new problem figured I would reuse the same thread...

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Post the question then :P
    This here is you're problem:
    PHP Code:
        <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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    firetag (08-30-2009)

  • #6
    New Coder
    Join Date
    Jul 2009
    Posts
    52
    Thanks
    6
    Thanked 1 Time in 1 Post
    Oh I see I was trying to do this => lol but thank-you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •