Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts

    Undefined variable notice:

    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
    Code:
    <?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
    Code:
    <?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?

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    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
    Last edited by Phil Jackson; 11-03-2009 at 11:44 AM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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.
    Are you a Help Vampire?

  • #4
    Regular Coder
    Join Date
    Nov 2008
    Posts
    175
    Thanks
    4
    Thanked 6 Times in 6 Posts
    So how do I declare it before line 8?
    Thanks,
    Josh

  • #5
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Easy way.
    PHP Code:
    $sel_subject;
    $sel_page
    Better, since your function doesn't care if it gets null arguments, change it:
    Code:
    //line 8
    <?php navigation(); ?>
    PHP Code:
    //functions.php
    function navigation($sel_subject null$sel_page null) { 
    Are you a Help Vampire?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •