jbrill Posted July 30, 2007 Share Posted July 30, 2007 hey guys, im trying to learn how this isset thing works and im having problems. what im trying to do: i have to pages that are going to be using this page im creating. This new page basically organizes the products and only displays what the url's values tell it to. (obviously the cat and subcat values change) one url is: http://mydomain.com/quick_products.php?cat=1 and the other is: http://mydomain.com/quick_products.php?cat=1&subcat=80 depending on what page the user navigates to this next page from will determine what url style form above is used. On this nect page, if only the cat is present in the URL i would liek it to display all the products under that category (cat) however if there is a subcat (subcat) in the url i would liek it to sort the results where cat = cat and subcat = subcat. right now it will not show anything when just the cat is present, but when the cat and subcat are present it is showing the corect results... just need some guidance here as i am new to php <? include 'includes/header.php'; $cat = (isset($_GET["cat"])) ? $_GET["cat"] : NULL; $subcat = (isset($_GET["subcat"])) ? $_GET["subcat"] : NULL; ?> <td width="722" valign="top"> <!---Display Subcategories---> </td> </tr> <tr> <td width="722" height="800" valign="top" align="center"> <? $featuredresults = mysql_query("SELECT * FROM products WHERE cat='".$cat."' AND subcat='".$subcat."' AND publish=1 AND featured=1"); while($row = mysql_fetch_array($featuredresults)) { echo "<table width=\"90%\" border=\"0\" style=\"border: 1px solid #f2f1e9; margin-bottom: 5px; background-color:#f2f1e9;\">"; echo "<tr><td><span class=\"listingtext\"><strong>". $row['name'] ."</strong></span></td>"; echo "<td align=\"right\"><span class=\"listingtext\">". $row['prod_number'] ."</span></td></tr>"; echo "<tr><td><span class=\"price\">Price: ". $row['price'] ."</span></td>"; echo "<td align=\"right\"><a class=\"details\" href=\"details.php?id=".$row['id']."&cat=".$row['cat']."&table=products\">Details</a></td>"; echo "</table>"; } ?> <? $allresult = mysql_query("SELECT * FROM products WHERE cat='".$cat."' AND subcat='".$subcat."' AND publish=1 AND featured=0 ORDER by name desc"); while($row = mysql_fetch_array($allresult)) { echo "<table width=\"90%\" border=\"0\" style=\"border: 1px solid #f2f1e9; margin-bottom: 5px; background-color:#f2f1e9;\">"; echo "<tr><td><span class=\"listingtext\"><strong>". $row['name'] ."</strong></span></td>"; echo "<td align=\"right\"><span class=\"listingtext\">". $row['prod_number'] ."</span></td></tr>"; echo "<tr><td><span class=\"price\">Price: ". $row['price'] ."</span></td>"; echo "<td align=\"right\"><a class=\"details\" href=\"details.php?id=".$row['id']."&cat=".$row['cat']."&table=products\">Details</a></td>"; echo "</table>"; } ?> <? include 'includes/footer.php' ?> Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/ Share on other sites More sharing options...
Barand Posted July 30, 2007 Share Posted July 30, 2007 If subcat is not set, leave the subcat condition out of the query Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311364 Share on other sites More sharing options...
jbrill Posted July 30, 2007 Author Share Posted July 30, 2007 but subcat will be set if it come from the second page Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311367 Share on other sites More sharing options...
hitman6003 Posted July 30, 2007 Share Posted July 30, 2007 If you leave it in there the database will pull on an empty string, which would be incorrect... $query = "SELECT * FROM products WHERE cat = '" . $cat . "'" . ($subcat != "" ? " AND subcat = '" . $subcat . "' " : "") . "AND publish = 1 AND featured = 1"; Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311371 Share on other sites More sharing options...
jbrill Posted July 30, 2007 Author Share Posted July 30, 2007 the above code did nto to do anything, it does not show any results Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311381 Share on other sites More sharing options...
Barand Posted July 30, 2007 Share Posted July 30, 2007 <?php $where = array(); $whereclause = ''; $where[] = "(publish = 1)"; $where[] = "(featured = 1)"; if (isset($_GET['cat'])) { $where[] = "(cat = {$_GET['cat']})"; } if (isset($_GET['subcat'])) { $where[] = "(subcat = {$_GET['subcat']})"; } if (count($where)) $whereclause = "WHERE " . join (' AND ', $where); $query = "SELECT * FROM products $whereclause"; ?> Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311384 Share on other sites More sharing options...
jbrill Posted July 30, 2007 Author Share Posted July 30, 2007 awesoem guys! thanks so much. i have one more question though. on a similiar page, i have applied almost the same code. however it will not display my products now. Here is the code <? $cat = (isset($_GET["cat"])) ? $_GET["cat"] : NULL; $subcat = (isset($_GET["subcat"])) ? $_GET["subcat"] : NULL; $allresult = "SELECT * FROM products WHERE cat = '" . $cat . "'" . ($subcat != "" ? " AND subcat = '" . $subcat . "' " : "") . " AND publish = 1"; $allresultquery = mysql_query($allresult); while($row2 = mysql_fetch_array($allresultquery)) { echo"<table height=\"275\" border=\"0\" class=\"productdisplay\">"; echo"<tr>"; echo"<td align=\"center\">"; if($row2['photo']==null || !file_exists($row2['photo'])) { echo "<img src=\"images/nophoto.gif\" width=66 height=39 border=0>"; } else { // if image exists, resize and display $size = getimagesize($row2['photo']); $width = $size[0]; $height = $size[1]; $max = 200; if($width > $max || $height > $max) { $ratio = $width/$height; if($width < $height) { // width is smaller than height, hence the height should be adjuste to max first. $resize = $max/$height; $nheight = 200; $nwidth = ceil($width*$resize); } if($width > $height) { // height is smaller than width, hence the width should be adjuste to max first. $resize = $max/$width; $nwidth = 200; $nheight = ceil($height*$resize); } } else { $nwidth=$width; $nheight=$height; } echo "<img src=\"".$row2['photo']."\" border=0 width=\"".$nwidth."\" height=\"".$nheight."\">"; }"</td>"; echo"</tr>"; echo"<tr>"; echo"<td width=\"220\" align=\"center\"><span class=\"listingtext\"><strong>". $row2['name'] ."</strong></span><br><span class=\"listingtext\">Product Number: ". $row2['prod_number'] . "</span><br><span class=\"price\">Price: ". $row2['price'] . "</span><br><a class=\"details\" href=\"details.php?id=".$row2['id']."&cat=".$row2['cat']."&table=products\">Details</a></td>"; echo"</tr>"; echo"</table>"; } ?> When i take out this part: " . ($subcat != "" ? " AND subcat = '" . $subcat . "' " : "") . " It then shows the products, however i need it to be in there, where abouts is the problem? Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311439 Share on other sites More sharing options...
jbrill Posted July 30, 2007 Author Share Posted July 30, 2007 anyone? Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-311476 Share on other sites More sharing options...
jbrill Posted July 31, 2007 Author Share Posted July 31, 2007 anyoneeeeeeeeee? Link to comment https://forums.phpfreaks.com/topic/62549-using-isset-why-will-this-not-work/#findComment-312082 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.