Using varibles from one file on an included file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tco2388
Forum Newbie
Posts: 4
Joined: Tue May 25, 2010 12:42 pm
Location: Portland, Oregon, USA

Using varibles from one file on an included file

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using varibles from one file on an included file

Post by Christopher »

What specifically is not working?
(#10850)
tco2388
Forum Newbie
Posts: 4
Joined: Tue May 25, 2010 12:42 pm
Location: Portland, Oregon, USA

Re: Using varibles from one file on an included file

Post 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.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Using varibles from one file on an included file

Post by Jonah Bron »

Do you still get the same error?
tco2388
Forum Newbie
Posts: 4
Joined: Tue May 25, 2010 12:42 pm
Location: Portland, Oregon, USA

Re: Using varibles from one file on an included file

Post 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.
tco2388
Forum Newbie
Posts: 4
Joined: Tue May 25, 2010 12:42 pm
Location: Portland, Oregon, USA

Re: Using varibles from one file on an included file

Post 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?
Post Reply