...

View Full Version : Accessing an int from array



Cobb
01-31-2012, 09:29 PM
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:


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:

$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.

Fou-Lu
01-31-2012, 09:57 PM
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.

Cobb
01-31-2012, 10:07 PM
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?

kbluhm
01-31-2012, 10:18 PM
What do you mean by the definition of BlogPost?
Source code.

Fou-Lu
01-31-2012, 10:20 PM
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.

Cobb
01-31-2012, 10:23 PM
The div to display the blog posts in index.php.


<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.

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;
}
?>

Cobb
01-31-2012, 10:25 PM
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!

Fou-Lu
01-31-2012, 10:27 PM
This isn't what we need. We need the class file for BlogPost.

Cobb
01-31-2012, 10:28 PM
Yeah, I realised that after.


<?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;
}

}

?>

Fou-Lu
01-31-2012, 11:08 PM
According to this, its found under BlogPost::$id. Using $post->id should work just fine.
Where are you assigning the $id value?


Wait a sec, I see it.
This is a tremendous problem too:


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:


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.

Cobb
01-31-2012, 11:20 PM
Of course! Can't believe I missed that! Working fine now, thank you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum