j05hr Posted November 2, 2009 Share Posted November 2, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/ Share on other sites More sharing options...
KevinM1 Posted November 2, 2009 Share Posted November 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949404 Share on other sites More sharing options...
j05hr Posted November 2, 2009 Author Share Posted November 2, 2009 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>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949411 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 <?php navigation($sel_subject, $sel_page); ?> You have to define $sel_subject in this line. You aren't defining it any where I can see Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949518 Share on other sites More sharing options...
j05hr Posted November 2, 2009 Author Share Posted November 2, 2009 How would I define it in that line? Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949534 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 you define it like any other variable? Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949536 Share on other sites More sharing options...
j05hr Posted November 2, 2009 Author Share Posted November 2, 2009 I'm not really sure where to do it, I'm just following tutorials trying to follow along how it works. Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949542 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 well whatever value that variable is supposed to have, you set the variable before you call the function $sel_subject = "whatever"; Perhaps you should read up on more basic tutorials Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949545 Share on other sites More sharing options...
j05hr Posted November 2, 2009 Author Share Posted November 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949550 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 oh i see. get_all_subjects() defines sel_subject. it sets it as a global variable, which is terrible, but im not going to get into that. you may just have to call get_all_subjects before you call the other function Quote Link to comment https://forums.phpfreaks.com/topic/179975-undefined-variable-notice/#findComment-949576 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.