Jump to content

using isset, why will this not work?


jbrill

Recommended Posts

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
Share on other sites

<?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
Share on other sites

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&#58;  ". $row2['prod_number'] . "</span><br><span class=\"price\">Price&#58;  ". $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
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.