Jump to content
Killersites Forums

Undefined Variables -,-


Recommended Posts

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:




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

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

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


All the code above is placed within a table.


This is the controller


$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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...