samoht Posted August 15, 2007 Share Posted August 15, 2007 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?? Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/ Share on other sites More sharing options...
akitchin Posted August 15, 2007 Share Posted August 15, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325015 Share on other sites More sharing options...
samoht Posted August 15, 2007 Author Share Posted August 15, 2007 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325025 Share on other sites More sharing options...
akitchin Posted August 15, 2007 Share Posted August 15, 2007 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']; Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325034 Share on other sites More sharing options...
samoht Posted August 15, 2007 Author Share Posted August 15, 2007 Thanks for the help!! Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325060 Share on other sites More sharing options...
samoht Posted August 16, 2007 Author Share Posted August 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325862 Share on other sites More sharing options...
akitchin Posted August 16, 2007 Share Posted August 16, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325868 Share on other sites More sharing options...
samoht Posted August 16, 2007 Author Share Posted August 16, 2007 Yes that is what I was after! thanks again for the help - I should of thought of !isset - but I wasn't thinking Quote Link to comment https://forums.phpfreaks.com/topic/65121-solved-using-_get-with-a-href/#findComment-325877 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.