Notice: Undefined index. Can't see what's wrong

Hi,

I have the following page template:


<?php
require 'functions.php';
$type = get_type($_GET['type']);
?>
<!doctype html>
<html>
<head>
	<meta charset="UTF-8" />
	<link rel="stylesheet" type="text/css" href="/style.css" />
	<title><?php echo $type['type_title'].' - '.$site_name ?></title>
</head>
<body>
	<div id="container">
		<?php
			echo '<h1>'.$type['type_title'].'</h1>';
		?>
	</div>
</body>
</html>

And it gives “Undefined index” notice on line 15. The title in the title tag works fine but the title in the body doesn’t show up. I have created similar page templates many times but never came cross this. Am I missing something?

Below
$type = get_type($_GET[‘type’]);

enter this:
echo “<pre>”;print_R($type);echo “</pre>”;

Run the script again and see if type_title shows up.

I added that line, it displays $type array elements at the top of the page but type_title does not show up in the body where I echo it. Still the same notice.

When I add this once more:
$type = get_type($_GET[‘type’]);

just above this:
echo ‘<h1>’.$type[‘type_title_long’].‘</h1>’;

like the following:

<?php 
require 'functions.php'; 
$type = get_type($_GET['type']); 
?> 
<!doctype html> 
<html> 
<head> 
    <meta charset="UTF-8" /> 
    <link rel="stylesheet" type="text/css" href="/style.css" /> 
    <title><?php echo $type['type_title'].' - '.$site_name ?></title> 
</head> 
<body> 
    <div id="container"> 
        <?php
            $type = get_type($_GET['type']); 
            echo '<h1>'.$type['type_title'].'</h1>';
        ?> 
    </div> 
</body> 
</html>

it shows the type_title without a notice. But I don’t understand because I already defined $type at the top.

Good to know that you did get an array.

Can you show us functions.php? It might help me to clear things up.

Here is the functions.php:

function get_type($type) {
	$type = mysql_real_escape_string(strip_tags(trim($type)));
	if (!valid_entry($type,'type','types')) {
		header('HTTP/1.1 404 Not Found');
		header('Location: /404.php');
	} else {
		$query = mysql_query("SELECT `type_name`, `type_title` FROM `types` WHERE `type_name` = '$type'");
		$type = mysql_fetch_assoc($query);
		return $type;
	}
}
function valid_entry($entry,$type,$table) {
	$entry = mysql_real_escape_string(strip_tags(trim($entry)));
	$id = $type.'_id';
	$name = $type.'_name';
	$result = mysql_query("SELECT `$id` FROM `$table` WHERE `$name` = '$entry'");
	$total = mysql_num_rows($result);
	if ($total == 0) {
		return false;
	} else { 
		return true;
	}
}

The function get_type returns type_name and type_title from the type you provided. If the type is not in the database it would return NULL value, and so you get the Undefined index notice. Make sure you provide a valid Get type when calling the script; if the value is not given, use a default value.

Example:
[FONT=Courier New]<?php
require ‘functions.php’;

$my_type = $_Get[‘type’];
if(!$my_type) $my_type = “My Type”;
$type = get_type($my_type);
if($type[‘type_name’]) $type[‘type_name’] = “Name not defined”;
if($type[‘type_title’]) $type[‘type_title’] = “Title not defined”;

?> [/FONT]

Try it and see if it works.

Ok, I solved it. The problem was that I have another included file sidebar.php before this line “$type = get_type($_GET[‘type’]);” in the body. The sidebar.php also has $type as a variable and it stores types in an array. I changed the name of that variable and now all works fine as it should. Learned another thing today:

Same variable names in included files will conflict.

Thanks for your help and advice.