...

View Full Version : Undefined variable notice:



j05hr
11-03-2009, 11:54 AM
I'm getting this variable error and I'm not sure why,

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

Notice: Undefined variable: sel_page in C:\wamp\www\estateagent\content1.php on line 8

content1.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>
<br />
<h5><a href="new_subject.php">+ Add new subject</a></h5>
<?php navigation($sel_subject, $sel_page); ?>


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



includes/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>";

}
}
?>


It's being declared so I'm not sure why it's saying it's undefined?

Phil Jackson
11-03-2009, 12:41 PM
dont quote me be i think by setting it as NULL it as if it isnt set. Change to $sel_subject = "";

EDIT: i think due to isset goes on the basis of TRUE or FALSE, 1 or 0 and believe NULL == FLASE

and therefor if not set will return error

tomws
11-03-2009, 07:45 PM
It's not an error; it's a notice. There's a difference.

The notice means what it says: those variables are undefined at that point (line 8). You say that it's declared, but it's not. The declaration in within a function, so the variables aren't declared/defined until the function is called.

j05hr
11-03-2009, 08:30 PM
So how do I declare it before line 8?
Thanks,
Josh

tomws
11-03-2009, 08:39 PM
Easy way.

$sel_subject;
$sel_page;

Better, since your function doesn't care if it gets null arguments, change it:

//line 8
<?php navigation(); ?>


//functions.php
function navigation($sel_subject = null, $sel_page = null) {



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum