samoht Posted August 16, 2007 Share Posted August 16, 2007 hello again, I have several different query possiblities that I want to pass to my url Is ther a better way than if($cgId){echo "g=".$cgId."&brand=".$pkId;}elseif ($catId){echo "c=".$catId... Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 16, 2007 Share Posted August 16, 2007 well, you COULD toss them all into standardized arrays to pass through, but you'd still have to switch() through the cases to assign the standardized array: $this_query = array('g' => $cgId, 'brand' => $pkId); foreach ($these_query AS $name => $val) { echo $name.'='.$val.'&'; } Quote Link to comment Share on other sites More sharing options...
samoht Posted August 16, 2007 Author Share Posted August 16, 2007 I found that I can use $_SERVER['REQUEST_URI'] to just append something to the end of the current query. What I don't like about this is that it can created huge urls because someone might keep changing there brand or what ever. Is there a way to check if the current URI already has the same query (brand='' etc) and if it does to just replace it?? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 16, 2007 Share Posted August 16, 2007 if (isset($_GET['brand'])) { // you've got a brand already sitting in the query } is that what you're looking for? Quote Link to comment Share on other sites More sharing options...
samoht Posted August 16, 2007 Author Share Posted August 16, 2007 I'm not sure, can I just... <a href="<?php if(isset($_GET['brand'])){echo "&brand=".$row['Code']."\"";}else{ echo $_SERVER['REQUEST_URI']."&brand=".$row['Code']."\"";} ?>" doesn't seem right?? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 16, 2007 Share Posted August 16, 2007 you COULD simply parse through the entire $_GET string: foreach ($_GET AS $key => $val) { $query_string .= $key.'='.$val.'&'; } $final_query = '?'.substr($query, 0, -1); for each of the keys that you don't want to replicate, or that you want to replace, you can simply toss them into an if() within the foreach() which tells the loop to skip that variable. then manually append them to the end. Quote Link to comment Share on other sites More sharing options...
samoht Posted August 17, 2007 Author Share Posted August 17, 2007 I think this is what I want but I am a little confused about the condition and the variable to serve up to my href's <?php foreach ($_GET AS $key => $val) { if(isset($_GET['size'])) { $query_string //how do I change the size and set the query string here? } else { $query_string .= $key.'='.$val.'&'; } } $final_query = '?'.substr($query, 0, -1); ?> <a href="<?php echo $final_query; ?>"> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 17, 2007 Share Posted August 17, 2007 assuming the new value you want to use for 'size' is $newsize, you can do it one of two ways: <?php foreach ($_GET AS $key => $val) { if($key == 'size') { $query_string .= $key.'='.$newsize.'&'; } else { $query_string .= $key.'='.$val.'&'; } } $final_query = '?'.substr($query, 0, -1); ?> with this method, just add the keys to the if() conditional and perhaps use a variable variable name, or simply use an array (ie. $new[$key]). otherwise, you can simply append them manually: <?php foreach ($_GET AS $key => $val) { if($key != 'size') { $query_string .= $key.'='.$val.'&'; } } // add in the size manually $query_string .= 'size='.$newsize.'&'; $final_query = '?'.substr($query, 0, -1); ?> this way you just skip putting anything in the if() conditional into the query string, and add the others manually afterward. Quote Link to comment Share on other sites More sharing options...
samoht Posted August 17, 2007 Author Share Posted August 17, 2007 I must be doing something wacky. with: <?php $query_string = ''; $newsize = ''; foreach ($_GET AS $key => $val) { if($key=='size') { $query_string .= $key.'='.$newsize.'&'; } else { $query_string .= $key.'='.$val.'&'; } } $final_query = '?'.substr($query_string, 0, -1); ?> <a href="<?php echo $final_query."&size=".$row['SizeId']."\""; ?>"> my url looks fine when size is not set - but when set I get ...&size=&size=1 am I supposed to set $newsize = "size=" or something?? Also when I go to add other $key's will they be elseif($key == 'brand') etc Also, if I use the second method does my $final_query only work for size? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 17, 2007 Share Posted August 17, 2007 you're trying to do it both ways. you'll have to pick either one or the other - the appending method is probably easiest for you. note that i'm not adding a & to the final query echo, which is because we've already got one sitting at the end of the query string from the last loop: <?php $query_string = ''; foreach ($_GET AS $key => $val) { if($key!='size' && $key!='brand') { $query_string .= $key.'='.$val.'&'; } } $final_query = '?'.$query_string; ?> <a href="<?php echo $final_query.'size='.$row['SizeId'].'"'; ?>"> Quote Link to comment Share on other sites More sharing options...
samoht Posted August 17, 2007 Author Share Posted August 17, 2007 Sorry - I'm not quite there - though it seems I should be. The code from your last post resets the query - so I suppose I still need to append in the condition that one of my $key's is met Is that right? if the brand is set but not the size then I simply want to append the size to the query - but change the size from the query (and vice versa) Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 17, 2007 Share Posted August 17, 2007 so you mean to say that if either brand or size are missing from the incoming GET string, you want to append them with the database value? however, if they exist, you want to keep their current values? perhaps an example of what the script spits out now vs. what you'd like it to spit out would be helpful. Quote Link to comment Share on other sites More sharing options...
samoht Posted August 17, 2007 Author Share Posted August 17, 2007 by script do you mean url or sql? This is complicated because I have a main catalog.php page that serves up different product list based on the nav selection. (e.g. main level nav which checks the categorygroup's table or sub nav which checks the category table etc. all in all there are four levels here.) On top of that I have a leftnav.php that contains the two (currently) ul's "Brands" and "Size" which should refine the search for products. the first value in my url query tells me which of the 4 pages to serve up - currently designated with g, c, i, or p after that the brand and then the size - so a complete url looks like catalog.php?g=1&brand="HRSHY"&size=3 does this help?? ----------------------------------------------- if you want a look at my code for a page: <?php $productsPerRow = 3; $productsPerPage = 27; //$productList = getProductList($catId); $children = array_merge(array($catId), getChildCategories(NULL, $catId)); $children = ' (' . implode(', ', $children) . ')'; $QFrom = "FROM product pd, item i, productprice pp, itemfeatures itf, features f, packaging pack, size s, brands b"; $QWhere = "WHERE f.Name = '$pageTitle' AND pd.PackageId = pack.PackageId AND itf.FeatureId = f.FeatureId AND itf.ItemId = i.ItemId AND i.BrandId = b.BrandId AND pd.SizeId = s.SizeId AND pd.ItemId = i.ItemId AND pd.ProductId = pp.ProductId AND ClientPriceCode = 'R1'"; $QOrderBy = "ORDER BY pd.Name"; # Add Brand to query. if (!isset($_GET['brand']) || (isset($_GET['brand']) && $_GET['brand']=="ALL")) unset($_SESSION['brand']); elseif (isset($_GET['brand'])) $_SESSION['brand'] = $_GET['brand']; if (isset($_SESSION['brand'])) { $QWhere .= $QWhere == '' ? "WHERE i.Code = '$_SESSION[brand]'" : " AND b.Code = '$_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); } # Add Size to query. if (!isset($_GET['size']) || (isset($_GET['size']) && $_GET['size']=="ALL")) unset($_SESSION['size']); elseif (isset($_GET['size'])) $_SESSION['size'] = $_GET['size']; if (isset($_SESSION['size'])) { $QWhere .= $QWhere == '' ? "WHERE pd.SizeId = '$_SESSION[size]'" : " AND s.SizeId = '$_SESSION[size]'"; # Get name of brand being displayed. $qrySize = "SELECT Name FROM Size WHERE SizeId = '$_SESSION[size]'"; $rsSize = mysql_query($qrySize, $connection1) or die(mysql_error()); $row_rsSize = mysql_fetch_assoc($rsSize); $totalRows_rsSize = mysql_num_rows($rsSize); $Size = $row_rsSize['Name']; mysql_free_result($rsSize); } $sql = "SELECT i.Name AS ItemName, pd.ItemId as Id, pd.ProductId, pd.Name, pd.PriceRetail, pp.PriceSell, i.ImagetnURL, QtyInStock, pack.PackageType, s.Name as Size $QFrom $QWhere $QOrderBy"; $result = dbQuery(getPagingQuery($sql, $productsPerPage)); $pagingLink = getPagingLink($sql, $productsPerPage, "g=$cgId"); $numProduct = dbNumRows($result); And this is part of my leftnav.php <?php $query_string = ''; foreach ($_GET AS $key => $val) { if($key!='size' && $key!='brand') { $query_string .= $key.'='.$val.'&'; } } $final_query = '?'.$query_string; ?> <div id="ltnav" class="clearfix"> <a href="#" style="text-align:center; background:transparent;"> <img src="assets/bclogo_exclusive.png" title="Bucks County Exclusive" border="0"/> </a> <div id="shopby" class="clearfix"> <h3>Shop By</h3> <div id="shoppics"> <a href="catalog.php?g=27"><p class="bulk">Bulk</p></a> <a href="catalog.php?g=121"><p class="container">Container</p></a> </div> <div id="slider" style="clear:both"> <ul class="menu"> <li><a class="top"><!--[if IE 7]><!--></a><!--<![endif]--><td></td><!--[if lte IE 6]></a><![endif]--></li> <li class="sub"><a href="<?php echo $result;?>" class="slide">Brands<!--[if IE 7]><!--></a><!--<![endif]--><table><tr><td> <ul> <li><a href="<?php echo $_SERVER['REQUEST_URI'];?>&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)){ $pkId = $row['Code'];?> <li style="text-align:left; padding-left:2px;"><a href="<?php echo $final_query.'brand='.$row['Code'].'"'; ?>"><?php echo $row['Name']; ?></a></li><?php echo "\n"; } ?> </ul> Hope that's clear?? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 17, 2007 Share Posted August 17, 2007 i think it's clear. the only thing i don't see in your code is where you issue size links. but essentially i think i see where you're going - you want to issue links that change the brand, as well as a series of links that change the size, while maintaining every other GET variable in each case. is that correct? if so, you can construct a query that retains everything EXCEPT those two, as well as one string for each of the two ones that might change. in this way, you can construct the GET string piecewise by starting with the main query, followed by the piece that you DON'T want to change in that iteration (ie. size, in the case that your echoing brand links), followed by the unique value: <?php $optionals = array('brand', 'size'); $query_string = ''; foreach ($_GET AS $key => $val) { if(!in_array($key, $optionals)) { $query_string .= $key.'='.$val.'&'; } } $main_query = '?'.$query_string; // construct the optional pieces foreach ($optionals AS $name) { $$name = $name.'='.$_GET["$name"].'&'; } ... while($row = dbFetchAssoc($result)){ $pkId = $row['Code'];?> <li style="text-align:left; padding-left:2px;"><a href="<?php echo $final_query.$size.'brand='.$row['Code'].'"'; ?>"><?php echo $row['Name']; ?></a></li><?php echo "\n"; } ?> this will go through the GET string, re-constructing it with everything except what's in the $optionals array. after that, it will go ahead and make a local variable by the same name with a value equal to its piece of the GET string. does this make sense, and more importantly, is this what you're after? Quote Link to comment Share on other sites More sharing options...
samoht Posted August 17, 2007 Author Share Posted August 17, 2007 Thanks for the help, I am getting a notice on the unidentified index again foreach ($optionals AS $name) { $$name = $name.'='.$_GET["$name"].'&'; } on the line $$name... I assume a simple if(isset($_GET["$name"] solves that but where do I declare $size? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 18, 2007 Share Posted August 18, 2007 you're right, an isset() will help you eliminate the undefined index notice. the line $$name will declare all local variables by the same name as their value in the $optionals array - it's a variable variable name, cute huh? it will form a variable with the name of whatever's in $name. in this case, it will make $brand and $size variables. Quote Link to comment 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.