Jump to content

Undefined variable notice:


j05hr

Recommended Posts

I'm getting this undefined variable notice,

 

Notice: Undefined variable: sel_subject in C:\wamp\www\estateagent\content1.php on line 7

 

This is the code from line 7

<?php navigation($sel_subject, $sel_page); ?>

 

Which it gets from my include/function page, here is the code that it's talking about from the functions page.

 

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/179975-undefined-variable-notice/
Share on other sites

The problem isn't within the function.  The problem is that you're trying to send an undefined variable - $sel_subject - into the function.  In other words, you don't assign a value to $sel_subject before attempting to pass it into the function.  Which is exactly what the error message says.

I'll post my whole code as some of the other functions have $sel_subject and I'm not sure how to assign the value to the $sel_subject

 

content.php

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<div id="content">
<h2> Buying Page</h2>

<?php navigation($sel_subject, $sel_page); ?>


</div>
<?php require("includes/footer.php"); ?>

 

functions.php

<?php
//This is all the 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) {

	$subject_set = get_all_subjects();
	// 5. Use returned data
	while ($subject = mysql_fetch_array($subject_set)) { 


	echo "<div class=\"menu-name\">";  echo "<a href=\"page.php?page=" . urlencode($subject["id"]) .
		"\">{$subject["menu_name"]}</a>";  echo"</div>";
	echo "<div class=\"buying-text\">";  echo "{$subject["content"]}";  echo"</div>";

	echo "<div class=\"image\">"; echo"</div>";

	}
}
?>

That's what confused me because in the functions.php is...

 

$subject_set = get_all_subjects();

 

And the other guy said it wasn't from the functions page that was causing the error.  But the require_once is meant to call the functions page, so what am I doing wrong if it's been declared in the functions page?

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.