Jump to content

Recommended Posts

Hello,

 

I have a page that I would like to display all records by default. On this page I have two ul's 1 for brands and the other for sizes.

I would like to refine my query based on the value picked from the ul

 

Currently I am having a problem using $_GET['brand']  - because php tells me "UNDEFINED INDEX: 'brand' in..."

 

Any Ideas how I can pass a value from an ul to my sql query??

Link to comment
https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/
Share on other sites

undefined index is a notice, not a strict error.  it points out poor coding practice, but it won't halt the processing of your script.  you can get rid of these by turning down your error_reporting level either directly in the php.ini (rarely accessible if you're in a hosted environment) or using the error_reporting() function.

 

it will give you this notice if you try to use $_GET['brand'] or run a comparison on it without it being passed via URL, because technically the index doesn't exist in $_GET if it hasn't been passed.  we'll need to see code to tell you if your script will actually work, but notices don't mean the script isn't working.

Well I don't want to code poorly if I can help it.

 

This is the brand ul:

<ul>
					<li><a href="catalog.php?brand=ALL" id="brand=ALL">--View All--</a></li><?php echo "\n";
					$sql 	= "SELECT DISTINCT b.Name, b.BrandId, b.Code
								FROM brands b, product pd, item i, itemfeatures itf, features f
								WHERE i.BrandId = b.BrandId
								AND f.Name = '$pageTitle'
								AND itf.FeatureId = f.FeatureId
								AND itf.ItemId = i.ItemId
								AND pd.ItemId = i.ItemId
								ORDER BY b.Name";	  
					$result = dbQuery($sql);

					while($row = dbFetchAssoc($result)){?>
					<li style="text-align:left; padding-left:2px;"><a href="catalog.php?brand=<?php echo $row['Name']; ?>"><?php echo $row['Name']; ?></a></li><?php
						echo "\n";
					} ?>
				</ul>

 

and this is the add brand to query bit that is throwing me the notice:

<?php # Add Brand to query.
if (isset($_GET['brand']) || $_SESSION['brand']) {
if ($_GET['brand']=="ALL")
	unset($_SESSION[brand]);
elseif ($_GET[brand])
	$_SESSION[brand] = $_GET[brand];

if ($_SESSION[brand]) {
	$QWhere .= $QWhere == '' ? "WHERE i.Code = '$_SESSION[brand]'" : " AND b.Name = '$_SESSION[brand]'";

	# Get name of brand being displayed.
	$qryBrand = "SELECT Name FROM brands WHERE Code = '$_SESSION[brand]'";
	$rsBrand = mysql_query($qryBrand, $connection1) or die(mysql_error());
	$row_rsBrand = mysql_fetch_assoc($rsBrand);
	$Brand = $row_rsBrand[Name];
	mysql_free_result($rsBrand);
}
}

you start off well in that second portion, using isset($_GET['brand']) - this will avoid undefined index notices.  however, since $_SESSION['brand'] can also match that conditional, it could enter the statement without $_GET['brand'] being defined.  therefore when you use $_GET['brand'] in the if() comparison immediately following the initial if(), it will launch an undefined index notice if it isn't defined in the case that $_SESSION['brand'] has hurled your script into that statement.

 

long story short, you can rearrange that if() set to be slightly better:

 

	if (isset($_GET['brand']) && $_GET['brand']=="ALL")
	unset($_SESSION['brand']);
elseif (isset($_GET['brand']))
	$_SESSION['brand'] = $_GET['brand'];

One problem though...

 

With the condition set up this way I never unset the session and display all brands when the url is empty (e.g. catalog.php instead of catalog.php?brand="whatever")

 

How can I set the display to ALL if the url query is blank ??

 

Thanks

you'd want to add a condition to that first if() condition, such that if the $_GET['brand'] is NOT set, it will unset the session variable as well:

 

if (!isset($_GET['brand']) || (isset($_GET['brand']) && $_GET['brand']=="ALL"))

 

this if() condition says that if $_GET['brand'] is NOT set, or if it is set AND equal to 'ALL', it will unset the $_SESSION['brand'] variable and presumably show all brands.  is that what you're after?

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.