Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to search ALL categories 1

Status
Not open for further replies.

deecee

Technical User
Joined
Aug 25, 2001
Messages
1,678
Location
US
I have a drop down that has product categories. I want to add an ALL to the dropdown but I cannot make that work

Here is my processing code

Code:
<?php require_once('Connections/connCorp.php'); ?>
<?php
$prod_products = &quot;0&quot;;
if (isset($HTTP_POST_VARS['ProductType'])) {
  $prod_products = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['ProductType'] : addslashes($HTTP_POST_VARS['ProductType']);
}
$lowprice_products = &quot;1&quot;;
if (isset($HTTP_POST_VARS['LowPrice'])) {
  $lowprice_products = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['LowPrice'] : addslashes($HTTP_POST_VARS['LowPrice']);
}
$highprice_products = &quot;1600&quot;;
if (isset($HTTP_POST_VARS['HighPrice'])) {
  $highprice_products = (get_magic_quotes_gpc()) ? $HTTP_POST_VARS['HighPrice'] : addslashes($HTTP_POST_VARS['HighPrice']);
}
mysql_select_db($database_connCorp, $connCorp);
$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s') AND (product = '%s')&quot;, $lowprice_products,$highprice_products,$prod_products);
$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);
?>

And here is my search page code

Code:
<html>
<head>
<title></title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
<style type=&quot;text/css&quot;>
<!--
td.font {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	color: #0099FF;
}
-->
</style>
<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+&quot;.location='&quot;+selObj.options[selObj.selectedIndex].value+&quot;'&quot;);
  if (restore) selObj.selectedIndex=0;
}
//-->
</script>
</head>

<body leftmargin=&quot;0&quot; topmargin=&quot;0&quot; marginwidth=&quot;0&quot; marginheight=&quot;0&quot;>
<table width=&quot;640&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
  <tr>
    <td><img src=&quot;images/index_r1_c1.gif&quot; width=&quot;521&quot; height=&quot;43&quot;><a href=&quot;product.php&quot;><img src=&quot;images/index_r1_c12.gif&quot; width=&quot;119&quot; height=&quot;43&quot; border=&quot;0&quot;></a></td>
  </tr>
  <tr>
    <td height=&quot;26&quot; align=&quot;center&quot; bgcolor=&quot;#003366&quot;><?php include(&quot;menu.php&quot;); ?>
    </td>
  </tr>
  <tr>
    <td><img src=&quot;images/producthd.gif&quot; width=&quot;640&quot; height=&quot;130&quot;></td>
  </tr>
  <tr>
    <td><table width=&quot;640&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
      <tr>
        <td width=&quot;20&quot; rowspan=&quot;2&quot;>&nbsp;</td>
        <td><p align=&quot;left&quot;><font size=&quot;+2&quot; face=&quot;Arial, Helvetica, sans-serif&quot; color=&quot;#006699&quot;><a href=&quot;[URL unfurl="true"]http://www.companycasuals.com/catalog-servlets/CustomBrowser?customer=cascade&quot;><img[/URL] src=&quot;images/apparel.gif&quot; width=&quot;90&quot; height=&quot;56&quot; align=&quot;right&quot; border=&quot;0&quot;></a>Providing
              products that meet your needs.</font></p>
          <p align=&quot;left&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot;> Tap the
              full power of brand named merchandise incentives. Cascade Promotion
              Corporation utilizes highly desirable merchandise you can for your
              program and budget. To get started, you can use our product finder
              to locate items suited to your needs.</font></p>
          <p align=&quot;left&quot;><font face=&quot;Arial, Helvetica, sans-serif&quot;><b> Enter
                any combination pricing and Product Types to get a catalog of
                products meeting your specifications:<br>
          <br>
          </b></font></p></td>
      </tr>
      <tr>
        <td><form action=&quot;productsearch.php&quot; method=&quot;post&quot; name=&quot;productSearch&quot; id=&quot;productSearch&quot;>
        <table width=&quot;400&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot;>
          <tr>
            <td height=&quot;30&quot;><div align=&quot;right&quot; style=&quot;font: verdana; font-size: 10px; color: #0099ff;&quot;><strong>LOW PRICE&nbsp;</strong></div></td>
            <td height=&quot;30&quot;> | 
              <input name=&quot;LowPrice&quot; type=&quot;text&quot; id=&quot;LowPrice&quot; value=&quot;1&quot;></td>
          </tr>
          <tr>
            <td height=&quot;30&quot;><div align=&quot;right&quot; style=&quot;font: verdana; font-size: 10px; color: #0099ff;&quot;><strong>HIGH PRICE&nbsp;</strong></div></td>
            <td height=&quot;30&quot;> | 
              <input name=&quot;HighPrice&quot; type=&quot;text&quot; id=&quot;HighPrice&quot; value=&quot;1600&quot;></td>
          </tr>
          <tr>
            <td height=&quot;30&quot;><div align=&quot;right&quot; style=&quot;font: verdana; font-size: 10px; color: #0099ff;&quot;><strong>TYPE
                OF PRODUCT</strong></div></td>
            <td height=&quot;30&quot;>| 
              <select name=&quot;ProductType&quot; size=&quot;1&quot; id=&quot;ProductType&quot;>
              <option value=&quot;0&quot; selected>ALL
              <option value=&quot;1&quot;>CAMERAS
              <option value=&quot;2&quot;>CRYSTAL
              <option value=&quot;3&quot;>ELECTRONICS
              <option value=&quot;4&quot;>HOME OFFICE
              <option value=&quot;5&quot;>HOUSEWARES
              <option value=&quot;6&quot;>OPTICS
              <option value=&quot;7&quot;>OUTDOOR SPORTS
              <option value=&quot;8&quot;>WATCHES
              </select></td>
          </tr>
          <tr>
            <td height=&quot;40&quot; colspan=&quot;2&quot; align=&quot;center&quot; valign=&quot;middle&quot;><input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;SUBMIT&quot;>
&nbsp;&nbsp;
<input type=&quot;reset&quot; name=&quot;Submit2&quot; value=&quot;RESET&quot;></td>
          </tr>
        </table>
        <br><?php include(&quot;bottom.php&quot;); ?>
        </form></td>
      </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

So pretty much I can search each category within the price specified but I cannot for the life of me figure out how to get ALL to work.

[Hammer]
Nike Failed Slogans -- &quot;Just Don't Do It!&quot;
 
I would seem to me that to search a particular category, you would have to have a WHERE clause that limits the search.

If the &quot;ALL&quot; option is selected, don't add that WHERE clause limit to your query.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Ya but I still need to have the search check a price range &quot;low to high&quot; and then if they want to narrow by category as well. So this would require an &quot;AND&quot; statement so wouldn't a WHERE clause be necessary

[Hammer]
Nike Failed Slogans -- &quot;Just Don't Do It!&quot;
 
Yes, you still need a WHERE clause.

You just don't need the part of it that limits the category.

I notice that you are building your queries through the use of a single sprintf statement. I recommend in this case that you build it using concatenation of substrings.

Want the best answers? Ask the best questions: TANSTAAFL!
 
ok i appreciate your help but I am as beginner as it comes to this. About the best i can do is a simple mail function and some simple form processing.

You just don't need the part of it that limits the category.

But then how do i limit the search to check for price AND category. also i dont know how to conctenate. I am currently reading O'reilly php manual but i am still at least a week away from understanding some basic functions. This problem however needs to be resolved before I can learn this :/

I do appreciate your helpfulness as you have helped me in the past.

[Hammer]
Nike Failed Slogans -- &quot;Just Don't Do It!&quot;
 
Okay, as I see it, when you want to search for a price range and in a certain category, you issue a query like:

SELECT.....WHERE price >= 5.00 AND price <= 15.00 AND category = 3

But if you want to search just for price, the query would look something like:

SELECT.....WHERE price >= 5.00 AND price <= 15.00

The differenct between the two queries is the presence or absence of the part of the where clause which limits the search to a particular category. Specifically the part that reads, &quot; AND category = 3&quot;

It seems a fairly simple matter to append that string to the end of a query, depending on whether a form input has a certain value.



Want the best answers? Ask the best questions: TANSTAAFL!
 
could i use an if statement for this

like...

Code:
 if (product = &quot;0&quot;) 
{$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s'), $lowprice_products,$highprice_products,$prod_products);
$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);}

else 
{$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s') AND (product = '%s')&quot;, $lowprice_products,$highprice_products,$prod_products);
$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);}

[Hammer]
Nike Failed Slogans -- &quot;Just Don't Do It!&quot;
 
oops i messed up

could i use an if statement for this

like...

Code:
 if ($prod_products = &quot;0&quot;) 
{$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s'), $lowprice_products,$highprice_products,$prod_products);
$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);}

else 
{$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s') AND (product = '%s')&quot;, $lowprice_products,$highprice_products,$prod_products);
$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);}

[Hammer]
Nike Failed Slogans -- &quot;Just Don't Do It!&quot;
 
An if statement is exactly what I've been pointing you to since my first response in this thread. I don't know if the logic of your code will accomplish what you want, but I do have a few comments.

If you are testing a value you use &quot;==&quot;. If you are assigning a value, you use &quot;=&quot;. Check the operator you are using in your if-statement.

Your code is not as elegant as it might be. There is no point having duplicated code in both the &quot;then&quot; and &quot;else&quot; blocks of a conditional -- if you're going to do it both times, why not just have the common code follow the conditional?

Also, why not just append the part of the WHERE clause you need?

Like this version of the code you've posted, which uses the conditional only to append the necessary part of the WHERE clause:

Code:
$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s'), lowprice_products,$highprice_products);

if ($prod_products == &quot;1&quot;) 
{
	$query_products .= sprintf(&quot; AND (product = '%s')&quot;,$prod_products);
}

$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);

Want the best answers? Ask the best questions: TANSTAAFL!
 
WoW man thanks so very much...this is what i got to work with your help. the thing is that I see what your code means and that it pretty much says the same as my drawn out not so elegant code but when it comes time for me to come up with that type of efficiency then...Well thanks anyways I am tearing as I type :/


Code:
mysql_select_db($database_connCorp, $connCorp);
$query_products = sprintf(&quot;SELECT * FROM products WHERE (msrp BETWEEN '%s' AND '%s')&quot;,$lowprice_products,$highprice_products);
if ($prod_products > &quot;0&quot;) 
{
    $query_products .= sprintf(&quot; AND (product = '%s')&quot;,$prod_products);
}

$products = mysql_query($query_products, $connCorp) or die(mysql_error());
$row_products = mysql_fetch_assoc($products);
$totalRows_products = mysql_num_rows($products);

sometime you just gotta say &quot;WHAT THE @#*% !!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top