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 11 of 11
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Accessing an int from array

    I'm building my own blog, and need to use the id number of a blog post as part of a link to take you to a page with the full blog post.

    I have a php file with the following bit of code:
    Code:
    function GetBlogPosts($inId=null, $inTagId =null)
    
    *irrelevant code omitted*
    
    	else
    	{
    		$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['post'], $row['postfull'], $row["author_id"], $row['dateposted']);
    		array_push($postArray, $myPost);
    	}
    	return $postArray;
    }
    On my index page i've used the following to access the blog title and post and store in a variable:
    Code:
    	$blogPosts = GetBlogPosts();
    	
    	foreach ($blogPosts as $post)
    	{
                    $title = stripslashes($post->title);
                    $post = stripslashes($post->post);
    I can't work out how to do the same with id.

    Any help would be greatly appreciated!
    Thanks.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Given that the two here for title and post are public properties and are named the same as the corresponding database fields, the only assumption we can make is its under $post->id.
    You can post the definition of BlogPost.

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

    Cobb (01-31-2012)

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I've tried using $id = $post->id; but nothing seems to get stored in $id from that.

    What do you mean by the definition of BlogPost?

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by Cobb View Post
    What do you mean by the definition of BlogPost?
    Source code.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Cobb View Post
    I've tried using $id = $post->id; but nothing seems to get stored in $id from that.

    What do you mean by the definition of BlogPost?
    BlogPost is a class. We need the source definition of that class to see a property name and how to access it.

  • #6
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    The div to display the blog posts in index.php.

    Code:
    <div id="blogPosts">
    	<?php
    	include ("includes/includes.php");
    	
    	$blogPosts = GetBlogPosts();
    	
    	foreach ($blogPosts as $post)
    	{
                    $title = stripslashes($post->title);
                    $post = stripslashes($post->post);
                    $id = $post->id;
                    if (strlen($post) > 1) {
                        $post = substr($post, 0, 90);
                        $post = "$post... <br /><br /><a href=\"posts.php?id=" . $id . "\">read more</a>";
                     }
    	?>
    
        <p><strong><?php echo $title; ?></strong><br /><br />
        <?php echo $post; ?><br /><br />
        Posted on <?php echo $date; ?>
        ID: <?php echo $id; ?>
        <hr /></p>
        <?php
    }
    ?>
    </div>
    The full function in other php file.
    Code:
    function GetBlogPosts($inId=null, $inTagId =null)
    {
    	if (!empty($inId))
    	{
    		$query = mysql_query("SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"); 
    	}
    	else if (!empty($inTagId))
    	{
    		$query = mysql_query("SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC");
    	}
    	else
    	{
    		$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['post'], $row['postfull'], $row["author_id"], $row['dateposted']);
    		array_push($postArray, $myPost);
    	}
    	return $postArray;
    }
    ?>

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    BlogPost is a class. We need the source definition of that class to see a property name and how to access it.
    Ahh yeah sorry, being a bit slow! I'll just get the code for that!

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This isn't what we need. We need the class file for BlogPost.

  • #9
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yeah, I realised that after.

    Code:
    <?php
    
    class BlogPost
    {
    
    public $id;
    public $title;
    public $post;
    public $author;
    public $tags;
    public $datePosted;
    
    function __construct($inId=null, $inTitle=null, $inPost=null, $inPostFull=null, $inAuthorId=null, $inDatePosted=null)
    {
    	if (!empty($inId))
    	{
    		$this->id = $inId;
    	}
    	if (!empty($inTitle))
    	{
    		$this->title = $inTitle;
    	}
    	if (!empty($inPost))
    	{
    		$this->post = $inPost;
    	}
    
    	if (!empty($inDatePosted))
    	{
    		$splitDate = explode("-", $inDatePosted);
    		$this->datePosted = $splitDate[1] . "/" . $splitDate[2] . "/" . $splitDate[0];
    	}
    
    	if (!empty($inAuthorId))
    	{
    		$query = mysql_query("SELECT first_name, last_name FROM people WHERE id = " . $inAuthorId);
    		$row = mysql_fetch_assoc($query);
    		$this->author = $row["first_name"] . " " . $row["last_name"];
    	}
    
    	$postTags = "No Tags";
    	if (!empty($inId))
    	{
    		$query = mysql_query("SELECT tags.* FROM blog_post_tags LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId);
    		$tagArray = array();
    		$tagIDArray = array();
    		while($row = mysql_fetch_assoc($query))
    		{
    			array_push($tagArray, $row["name"]);
    			array_push($tagIDArray, $row["id"]);
    		}
    		if (sizeof($tagArray) > 0)
    		{
    			foreach ($tagArray as $tag)
    			{
    				if ($postTags == "No Tags")
    				{
    					$postTags = $tag;
    				}
    				else
    				{
    					$postTags = $postTags . ", " . $tag;
    				}
    			}
    		}
    	}
    	$this->tags = $postTags;
    }
    
    }
    
    ?>

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    According to this, its found under BlogPost::$id. Using $post->id should work just fine.
    Where are you assigning the $id value?

    Edit:
    Wait a sec, I see it.
    This is a tremendous problem too:
    PHP Code:
    foreach ($blogPosts as $post)
        {
                    
    $title stripslashes($post->title);
                    
    $post stripslashes($post->post); 
    You have reassigned post to that of $post->post. Therefore you will not be able to extract anything out of it as this is a string.
    Enable your error reporting:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    this should now indicate you are trying to access a property id on a non-object.

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

    Cobb (01-31-2012)

  • #11
    New Coder
    Join Date
    Aug 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Of course! Can't believe I missed that! Working fine now, 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
    •