Jump to content


Photo

Undefined Variables -,-


  • Please log in to reply
1 reply to this topic

#1 CLU

CLU

    Member

  • Member
  • PipPip
  • 24 posts

Posted 16 February 2012 - 03:29 PM

Hi Guys ok,

Im starting to get the hang of PHP OOP and MVC, what i cannot seem to prevent are Notices: Undefined Variables. I understand i can switch this off, but i just want to know why proper way to get this done. Does anyone know.

Here is my code:

From my model m_settings:
function siteSettings() 
	{
		if ($stmt = $FS->Database->query("SELECT * FROM site_settings WHERE id='1'"))
		$row = mysqli_fetch_array($stmt);
	}

This is just grabbing the results from a database and relaying them onto the view... which is the following:

v_index.php:

	<tr><td>Browser Title:
		<input type="text" name="browser_title" value="<?php echo $row['browser_title'];?>" placeholder="Enter your sites browser title here.">									
	</td></tr>

	<tr><td>Site Meta-Tags:
		<input type="text" name="met_tag" value="<?php echo $row['meta_tag'];?>" placeholder="Site Meta-Tags go here.">					
	</td></tr>

	<tr><td>Description:
		<input type="text" name="description" value="<?php echo $row['description'];?>" placeholder="Site Description.">					
	</td></tr>

All the code above is placed within a table.

This is the controller

<?php
include('../init.php'); 
include("models/m_settings.php");
$Settings = new Settings();

$FS->Template->load(APP_PATH . 'settings/views/v_index.php');


I can't understand why I'm getting the following Notices :

<br /><b>Notice</b>: Undefined variable: row in <b>/Applications/MAMP/htdocs/ukmocks/app/settings/views/v_index.php</b> on line <b>26</b><br />

The above happens for every $row echoed out...

I know i can add some code to the top of the page that will get rid of the notices, but i love to learn and want to know why this si happening :)

If anyone can help give me a shout

Thanks again
  • 0

#2 Ben

Ben

    Administrator

  • Administrators
  • 5,656 posts
  • LocationChico, CA

Posted 18 February 2012 - 08:23 PM

I unfortunately don't have the time to give you a full response, but just for a quick reply: I believe you are getting undefined errors because you are trying to access a variable that is out of the scope. Your $row variable within siteSettings() only exists within that function -- it isn't automatically shared everywhere in the web app. I'd suggest that you do some research on "variable scope" -- understanding this is pretty important and will help you to fix or avoid a lot of bugs.
  • 0
Benjamin Falk
Falken Creative : Twitter




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users