Jump to content

Undefined Index


bravo14

Recommended Posts

Hi Guys

 

I have a page with the following code when I open the page I get a message saying

 

Notice: Undefined index: cid in C:\wamp\www\renovations\includes\viewgallery.php on line 8

 

 

<?php
include("config.inc.php");

// initialization
$result_array = array();
$counter = 0;

$cid = (int)($_GET['cid']);
$pid = (int)($_GET['pid']);

// Category Listing

if( empty($cid) && empty($pid) )
{
	$number_of_categories_in_row = 4;

	$result = mysql_query( "SELECT c.category_id, c.category_name, COUNT( photo_id ) 
							FROM gallery_category AS c
							LEFT JOIN gallery_photos AS p ON p.photo_category = c.category_id
							WHERE c.category_id <>1
							AND c.category_id <>2
							GROUP BY c.category_id" );
	while( $row = mysql_fetch_array( $result ) )
	{
		$result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")";
	}
	mysql_free_result( $result );	

	$result_final = "<tr>\n";

	foreach($result_array as $category_link)
	{
		if($counter == $number_of_categories_in_row)
		{	
			$counter = 1;
			$result_final .= "\n</tr>\n<tr>\n";
		}
		else
		$counter++;

		$result_final .= "\t<td>".$category_link."</td>\n";
	}

	if($counter)
	{
		if($number_of_categories_in_row-$counter)
		$result_final .= "\t<td colspan='".($number_of_categories_in_row-$counter)."'> </td>\n";

		$result_final .= "</tr>";
	}
}


// Thumbnail Listing

else if( $cid && empty( $pid ) )
{
	$number_of_thumbs_in_row = 5;

	$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" );
	$nr = mysql_num_rows( $result );

	if( empty( $nr ) )
	{
		$result_final = "\t<tr><td>No Category found</td></tr>\n";
	}
	else
	{
		while( $row = mysql_fetch_array( $result ) )
		{
			$result_array[] = "<a href='viewgallery.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";
		}
		mysql_free_result( $result );	

		$result_final = "<tr>\n";

		foreach($result_array as $thumbnail_link)
		{
			if($counter == $number_of_thumbs_in_row)
			{	
				$counter = 1;
				$result_final .= "\n</tr>\n<tr>\n";
			}
			else
			$counter++;

			$result_final .= "\t<td>".$thumbnail_link."</td>\n";
		}

		if($counter)
		{
			if($number_of_photos_in_row-$counter)
		$result_final .= "\t<td colspan='".($number_of_photos_in_row-$counter)."'> </td>\n";

			$result_final .= "</tr>";
		}
	}
}

// Full Size View of Photo
else if( $pid )
{
	$result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" );
	list($photo_caption, $photo_filename) = mysql_fetch_array( $result );
	$nr = mysql_num_rows( $result );
	mysql_free_result( $result );	

	if( empty( $nr ) )
	{
		$result_final = "\t<tr><td>No Photo found</td></tr>\n";
	}
	else
	{
		$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
		list($category_name) = mysql_fetch_array( $result );
		mysql_free_result( $result );	

		$result_final .= "<tr>\n\t<td>
					<a href='viewgallery.php'>Categories</a> > 
					<a href='viewgallery.php?cid=$cid'>$category_name</a></td>\n</tr>\n";

		$result_final .= "<tr>\n\t<td align='center'>
				<br />
				<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />
				<br />
				$photo_caption
				</td>
				</tr>";
	}
}

// Final Output
echo <<<__HTML_END

<html>
<head>
<title>Gallery View</title>
</head>
<body>
<table width='100%' border='0' align='center' style='width: 100%;'>
$result_final		
</table>
</body>
</html>

__HTML_END;
?>

 

Any help will be much appreciated

 

Cheers

Link to comment
Share on other sites

Sorry my mistake, I think you are getting the error because $_GET['cid'] and $_GET['pid'] aren't actually set. Try this, which will validate they are set before trying to use them:

 

if (isset($_GET['cid']))
{
    $cid = (int) $_GET['cid'];
}

if (isset($_GET['pid']))
{
    $pid = (int) $_GET['pid'];
}

Link to comment
Share on other sites

When opening the page is ?cid=1 (where the id you want is 1) part of the query string in the address bar?

 

If not then $_GET['cid'] hasn't been defined, which is why you're getting the undefined index message.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.