Jump to content

Undefined Variable


j05hr

Recommended Posts

I don't understand why it's causing an error when another page is linking to the exact same page and not causing an error.  I copy and pasted the code from my other page to make sure it wasn't a spelling mistake and it still doesn't work.

 

The error message...

 

Notice: Undefined variable: sel_subject in C:\wamp\www\widget_corp\new_subject.php on line 9

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?phpfind_selected_page();?>

<?php include("includes/header.php"); ?>
<table id="structure">
<tr>
	<td id="navigation">
		<?php echo navigation($sel_subject, $sel_page); ?>
		</td>
	<td id="page">
		<h2>Add Subject</h2>
		<form action="create_subject.php" method="post">
			<p>Subject name: 
				<input type="text" name="menu_name" value="" id="menu_name" />
			</p> 
			<p>postion:
				<select name="position">
					<?php
						$subject_set = get_all_subjects();
						$subject_count = mysql_num_rows($subject_set);
						for($count=1; $count <= $subject_count; $count++) {
							echo "<option value=\"{$count}\">{$count}</option>";
						}
					?>

				</select>
				</p>
				<p>Visible:
					<input type="radio" name="visible" value="0" /> No
					 
					<input type="radio" name="visbile" value="1" /> Yes
				</p>
				<input type="submit" value="Add subject" />
			</form>

		<br />
		<a href="content.php">Cancel</a>

	</td>
</tr>
</table>
<?php require("includes/footer.php"); ?>

 

The functions page that it links to

<?php
// This file is the place to store all basic functions

function confirm_query($result_set) {
	if (!$result_set) {
		die("Database query failed: " . mysql_error());
	}
}

function get_all_subjects() {
	global $connection;
	$query = "SELECT * 
			FROM subjects 
			ORDER BY position ASC";
	$subject_set = mysql_query($query, $connection);
	confirm_query($subject_set);
	return $subject_set;
}

function get_pages_for_subject($subject_id) {
	global $connection;
	$query = "SELECT * 
			FROM pages 
			WHERE subject_id = {$subject_id} 
			ORDER BY position ASC";
	$page_set = mysql_query($query, $connection);
	confirm_query($page_set);
	return $page_set;
}

function get_subject_by_id($subject_id) {
	global $connection;
	$query = "SELECT * ";
	$query .= "FROM subjects ";
	$query .= "WHERE id=" . $subject_id ." ";
	$query .= "LIMIT 1";
	$result_set = mysql_query($query, $connection);
	confirm_query($result_set);
	// REMEMBER:
	// if no rows are returned, fetch_array will return false
	if ($subject = mysql_fetch_array($result_set)) {
		return $subject;
	} else {
		return NULL;
	}
}

function get_page_by_id($page_id) {
	global $connection;
	$query = "SELECT * ";
	$query .= "FROM pages ";
	$query .= "WHERE id=" . $page_id ." ";
	$query .= "LIMIT 1";
	$result_set = mysql_query($query, $connection);
	confirm_query($result_set);
	// REMEMBER:
	// if no rows are returned, fetch_array will return false
	if ($page = mysql_fetch_array($result_set)) {
		return $page;
	} else {
		return NULL;
	}
}

function find_selected_page() {

	global $sel_subject;
	global $sel_page;
	if (isset($_GET['subj'])) {
		$sel_subject = get_subject_by_id($_GET['subj']);
		$sel_page = NULL;
	} elseif (isset($_GET['page'])) {
		$sel_subject = NULL;
		$sel_page = get_page_by_id($_GET['page']);
	} else {
		$sel_subject = NULL;
		$sel_page = NULL;
	}
}
function navigation($sel_subject, $sel_page) {

	$output = "<ul class=\"subjects\">";
	$subject_set = get_all_subjects();
	while ($subject = mysql_fetch_array($subject_set)) {
		$output .= "<li";
		if ($subject["id"] == $sel_subject['id']) { $output .=  " class=\"selected\""; }
		$output .=  "><a href=\"content.php?subj=" . urlencode($subject["id"]) . 
			"\">{$subject["menu_name"]}</a></li>";
		$page_set = get_pages_for_subject($subject["id"]);
		$output .=  "<ul class=\"pages\">";
		while ($page = mysql_fetch_array($page_set)) {
			$output .=  "<li";
			if ($page["id"] == $sel_page['id']) { $output .=  " class=\"selected\""; }
			$output .=  "><a href=\"content.php?page=" . urlencode($page["id"]) .
				"\">{$page["menu_name"]}</a></li>";
		}
		$output .=  "</ul>";
	}


			$output .=  "</ul>";
			return $output;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/177420-undefined-variable/
Share on other sites

$sel_subject is undefined. i.e It has no value, it has not been set. Consider the following

print foobar($x);

$x = true;
print foobar($x);

The first example will throw the notice that $x is undefined. I am using an undefined variable in a function call. As it is only a notice it is not a fatal error, however your function: navigation($sel_subject, $sel_page); may produce unexpected results.

You can turn notice errors of by changing the level of error reporting:

ini_set('error_reporting', E_ALL & ~E_NOTICE); 

Link to comment
https://forums.phpfreaks.com/topic/177420-undefined-variable/#findComment-935485
Share on other sites

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.