Page 1 of 1

Using varibles from one file on an included file

Posted: Tue May 25, 2010 4:48 pm
by tco2388
I'm still getting the hang of OOP in PHP. Here's what I'm trying to do...

Find the info using the function and the GET variable and then displaying the results in the included template file. Few snippets from each file...without the html structure

view_details.php file:

Code: Select all

$id = $_GET['fi'];		
$feedback = Feedback::find_by_id($id);


include('feedback_details.php');
feedback_details.php file:

Code: Select all

         echo $feedback->name;
         echo $feedback->email;
Am I way off? Is it because I'm using the static function? Is it just not the way you do it? I know you define $id = 3 and then include a file that echoes $id and it'll work, so I thought it would work with objects/classes.

Please help.

Re: Using varibles from one file on an included file

Posted: Tue May 25, 2010 6:35 pm
by Christopher
What specifically is not working?

Re: Using varibles from one file on an included file

Posted: Tue May 25, 2010 6:41 pm
by tco2388
Sorry for not including that.

When the script runs I get an error stating that...

Undefined variable: feedback
Trying to get property of non-object

I did some reading and thought it may be because I used a static function, so it wasn't an instance of the class. I changed it to a public function and instantiated the class first, then called the function like...

Code: Select all

$feedback = new Feedback();
$feedback->find_by_id($id);
But it didn't work.

Re: Using varibles from one file on an included file

Posted: Tue May 25, 2010 7:49 pm
by Jonah Bron
Do you still get the same error?

Re: Using varibles from one file on an included file

Posted: Wed May 26, 2010 12:09 am
by tco2388
Yeah, still got the same error. I tried moving the script logic around and came up with some different errors.

If I defined $id in the view_details.php file:

Code: Select all

$id = $_GET['fi'];

include('feedback_details.php');
and then moved the script that finds the data into the template file with the html (feedback_details.php):

Code: Select all

$feedback = new Feedback();
$feedback->find_by_id($id);
it then says the id is undefined.


So it makes me think it's still a problem of instantiating. If someone knows the fix or correct way to structure where each part of the code goes...please tell me.

Re: Using varibles from one file on an included file

Posted: Wed May 26, 2010 12:02 pm
by tco2388
Not sure if I'm explaining this the wrong way, so I'll try again.

I believe this is how the Model/View/Controller pattern would work.

Model (feedback.php) <- Class file:

Code: Select all

Class Feedback
{
	public static function find_by_id($id = 0) 
	{
		global $database;
		$result_array = self::find_by_sql("SELECT * FROM " . self::$table_name . " WHERE id = {$id} LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
	} 
}

Controller (view_details.php):

Code: Select all

<?php
$id = $_GET['fi'];		
$feedback = Feedback::find_by_id($id);
?>
<?php include('header.php'); ?>
<?php include('feedback_details.php'); ?>
<?php include('footer.php'); ?>

View (feedback_details.php) <- template file included in view_details.php:

Code: Select all

<p>Name: <?php echo $feedback->name; ?></p>
<p>Email: <?php echo $feedback->email; ?></p>
Once again, my problem is that I'm getting an error stating:
"Undefined variable: feedback
Trying to get property of non-object"

So to my understanding the variable/instance of $feedback from the view_details.php file isn't carrying over into the included feedback_details.php file. How do I do this properly?