Jump to content

[SOLVED] My first OOP script, not outputting value


keeps21

Recommended Posts

Hi, I'm trying to get my head around OOP and have created the following script. All i want it to do at the minute is for  the getComments method to return a value, which can then be echoed onto the screen.

 

I can't figure out why it's not working, I'm sure I'm missing something really simple.

 

Thanks for any help.

<?php
/**
* Filename: class.comment.php
* Date: 22 October 2008
**/

// Include config.php - allows access to database.
include('config.php');

class Comment {

	private $in_CommentId;
	private $in_Name;
	private $in_Email;
	private $in_Comment;
	public $in_Comments;

	public function __construct() {

			$this->in_Name = $name;
			$this->in_Email = $email;
			$this->in_Comment = $comment;
	}

	public function addComment()
	{
			$query = "INSERT INTO comments (name, email, comment) VALUES ( '{$this->in_Name}', '{$this->in_Email}', '{$this->in_Comment}')";
			$result = mysql_query($query) or die ("MySQL Error");

			if ($result) {
				echo 'Success!';
			} else {
				echo 'Failed to add comment!';
			}
	}

	public function getComments() 
	{
			$this->in_Comments = "Why isn't this displaying?";					 
	        
			return $this->in_Comments;
	}								

}


// call get comments function and output result.
$in_comment = new Comment(); 
$in_comment->getComments();

$value = $in_comment->getComments->$this->in_Comments;

echo 'You said ' . $value;

?>

Hi, I'm trying to get my head around OOP and have created the following script. All i want it to do at the minute is for  the getComments method to return a value, which can then be echoed onto the screen.

 

I can't figure out why it's not working, I'm sure I'm missing something really simple.

 

Thanks for any help.

<?php
/**
* Filename: class.comment.php
* Date: 22 October 2008
**/

// Include config.php - allows access to database.
include('config.php');

class Comment {

	private $in_CommentId;
	private $in_Name;
	private $in_Email;
	private $in_Comment;
	public $in_Comments;

	public function __construct() {

			$this->in_Name = $name;
			$this->in_Email = $email;
			$this->in_Comment = $comment;
	}

	public function addComment()
	{
			$query = "INSERT INTO comments (name, email, comment) VALUES ( '{$this->in_Name}', '{$this->in_Email}', '{$this->in_Comment}')";
			$result = mysql_query($query) or die ("MySQL Error");

			if ($result) {
				echo 'Success!';
			} else {
				echo 'Failed to add comment!';
			}
	}

	public function getComments() 
	{
			$this->in_Comments = "Why isn't this displaying?";					 
	        
			return $this->in_Comments;
	}								

}


// call get comments function and output result.
$in_comment = new Comment(); 
$in_comment->getComments();

$value = $in_comment->getComments->$this->in_Comments;

echo 'You said ' . $value;

?>

 

You're invoking the method wrong, and you're invoking it twice, which is unnecessary.  The '$this' keyword should only be used in your class code.  It should never be used within your general script.  Furthermore, since getComments() returns a value, and does nothing else, you gain nothing by simply calling it without any kind of assignment to a variable.  So your line of

 

$in_comment->getComments();

 

Doesn't do anything of value at all.

 

Try the following lines out for size:

 

$value = $in_comment->getComments();
echo "You said: $value";

Thanks for the replies. I've now amended my code to the following which works as I expect it to.

 

<?php
/**
* Filename: class.comment.php
* Date: 22 October 2008
**/

// Include config.php - allows access to database.
include('config.php');

class Comment {

	private $in_CommentId;
	private $in_Name;
	private $in_Email;
	private $in_Comment;

	public function __construct() {

			$this->in_Name = $name;
			$this->in_Email = $email;
			$this->in_Comment = $comment;
	}

	public function addComment()
	{
			$query = "INSERT INTO comments (name, email, comment) VALUES ( '{$this->in_Name}', '{$this->in_Email}', '{$this->in_Comment}')";
			$result = mysql_query($query) or die ("MySQL Error");

			if ($result) {
				echo 'Success!';
			} else {
				echo 'Failed to add comment!';
			}
	}

	public function getComments() 
	{
			$query = "SELECT name, email, comment FROM comments";
			$result = mysql_query($query);

			$this->in_Comments = array(); // Initialise array

			while ($row = mysql_fetch_assoc($result)) // Loop through query results and add rows into array
			{
					$this->in_Comments[] = $row['name'] . ' - ' . $row['email'] . ' - ' . $row['comment'];
			}

			return $this->in_Comments;
	}								

}


// Call get comments function and output result.
$in_comment = new Comment;
$in_comment->getComments();

$comment_array = $in_comment->getComments(in_Comments);

foreach ($comment_array as $row ) // Output each row
{
		echo $row . '<br />';
}


?>

 

The only other question I have about it is this: At the minute I'm concatenating the rows returned by the query $row['name'] . $row['email'] . $row['comment']

 

How would I got about creating an array where the first row would contain an individual value for name, one for email and one for comment, I guess what I want to do is create a multi dimensional array.

Solved this myself.

 

The code is shown below. If there are any better ways to do this then I am open to advice.

 

<?php
/**
* Filename: class.comment.php
* Date: 22 October 2008
**/

// Include config.php - allows access to database.
include('config.php');

class Comment {

	private $in_CommentId;
	private $in_Name;
	private $in_Email;
	private $in_Comment;

	public function __construct() {

			$this->in_Name = $name;
			$this->in_Email = $email;
			$this->in_Comment = $comment;
	}

	public function addComment()
	{
			$query = "INSERT INTO comments (name, email, comment) VALUES ( '{$this->in_Name}', '{$this->in_Email}', '{$this->in_Comment}')";
			$result = mysql_query($query) or die ("MySQL Error");

			if ($result) {
				echo 'Success!';
			} else {
				echo 'Failed to add comment!';
			}
	}

	public function getComments() 
	{
			$query = "SELECT name, email, comment FROM comments";
			$result = mysql_query($query);

			$this->in_Comments = array(); // Initialise array

			while ($row = mysql_fetch_assoc($result)) // Loop through query results and add rows into array
			{
					$this->in_Comments[] = array ($row['name'], $row['email'], $row['comment']);
			}

			return $this->in_Comments;
	}								

}
// Call get comments function and output result.
$in_comment = new Comment;
$in_comment->getComments();

$comment_array = $in_comment->getComments(); // Get array from method

$size = count($comment_array); // Get size of array

for ($row = 0; $row < $size; $row++) // Loop through array and output values
{
    echo $comment_array[$row][0] . $comment_array[$row][1].$comment_array[$row][2];
    	echo "<br />";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.