You are here: Re: Code for searching a mysql database on multiple terms inputed from an html form « All PHP « IT news, forums, messages
Re: Code for searching a mysql database on multiple terms inputed from an html form

Posted by Dano on 09/03/06 14:34

I've done this both on HTML/PHPO forms, and in Visual Basic.

What you have to do is "parse" the pages when the form is submitted.
Test each form field for the presence or absence of data, and if it's
there, then handle it as you would.

Here's some code for searching for barcode labels:

-----BEGIN HTML FORM CODE-----
<h1>Find the Barcode Label You Need</h1>
<form name="form2" method="post"
action="find_labels.php">
<p>I need a
<input name="unit_width" type="text"
value="width" size="10">
x
<input name="unit_length" type="text"
value="length" size="10">
<select name="unit_color" id="unit_color">
<option value="Color">Color</option>
<option value="White" selected>White</option>
<option value="Both">Both</option>
</select>
<select name="product_subcat2"
id="product_subcat2">
<option value="TT" selected>Thermal
Transfer</option>
<option value="DT">Direct Thermal</option>
<option value="Not Sure">Not Sure</option>
</select>
label in
<select name="format">
<option value="Roll" selected>Roll</option>
<option value="Stack">Fanfold</option>
<option value="Not Sure">Not Sure</option>
</select>
format. <br>
Show me
<select name="product_name" id="product_name">
<option value="Stock" selected>BarcodeFactory
Stock</option>
<option value="OEM">Original Equipment
Manufacturer</option>
<option value="Both">Both BarcodeFactory and
OEM</option>
</select>
<select name="product_material"
id="product_material">
<option value="Synthetic">Synthetic</option>
<option value="Paper" selected>Paper</option>
<option value="Not Sure">Not Sure</option>
</select>
labels in a
<select name="inner_diameter">
<option value="three" selected>3-inch
core</option>
<option value="one">1-inch core</option>
<option value="both">Both 1- and 3-inch
core</option>
</select>
format.<br>
<input type="submit" name="Submit" value="Find
the Label!">
</p>
</form>
-----END HTML FORM CODE-----

-----BEGIN PHP PROCESSING CODE-----
include("connection.php");
$query = "select * from master_products where product_category =
'Labels' and unit_width = '".$HTTP_POST_VARS["unit_width"]."' and
unit_length = '".$HTTP_POST_VARS["unit_length"]."'";
if ($HTTP_POST_VARS["unit_color"] == 'Color') {
$query = $query." and unit_color <> 'White'";
}
if ($HTTP_POST_VARS["unit_color"] == 'White') {
$query = $query." and unit_color like '%White%'";
}
if ($HTTP_POST_VARS["product_subcat2"] == 'TT') {
$query = $query." and product_subcat2 = 'TT'";
}
if ($HTTP_POST_VARS["product_subcat2"] == 'DT') {
$query = $query." and product_subcat2 = 'DT'";
}
if ($HTTP_POST_VARS["product_material"] == 'Synthetic') {
$query = $query." and product_subcat1 like '%Synthetic%'";
}
if ($HTTP_POST_VARS["product_material"] == 'Paper') {
$query = $query." and product_subcat1 like '%Paper%'";
}
if ($HTTP_POST_VARS["format"] == 'Roll') {
$query = $query." and product_subcat4 = 'Roll'";
}
if ($HTTP_POST_VARS["format"] == 'Stack') {
$query = $query." and product_subcat4 = 'Stack'";
}
if ($HTTP_POST_VARS["product_name"] == 'Stock') {
$query = $query." and manufacturer = 'BarcodeFactory'";
}
if ($HTTP_POST_VARS["product_name"] == 'OEM') {
$query = $query." and manufacturer <> 'BarcodeFactory'";
}
if ($HTTP_POST_VARS["format"] == 'Roll') {
if ($HTTP_POST_VARS["inner_diameter"] == 'three') {
$query = $query." and inner_diameter >= '3.00'";
}
if ($HTTP_POST_VARS["inner_diameter"] == 'one') {
$query = $query." and inner_diameter = '1.00'";
}
}
$query = $query." order by aslowas";
//echo "<p>".$query."</p>";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
echo "<h2>Label Search Results</h2>";
if ($num_results == 0) {
echo "<p>No Labels were found that matched your search criteria.</p>";
}
else {
//Do whatever you want here with the results, like this:
echo
"<p>".$row["manufacturer"]."<br>".$row["product_name"]."<br>".$row["product_category"]."<br>".$row["product_number"]."
</p>";
}
-----END PHP PROCESSING CODE-----

Hopefully, you can see the "logic" I used, and tailor it to meet your
own needs. Hope it helps!

Dan Ford

kenoli wrote:
> I am designing a database with several "key word" columns. Users
> wanting to search the database will enter terms into html form fields
> from drop down menus populated from existing lookup tables or by adding
> their own terms which may, in some cases, be inserted into the lookup
> tables for subsequent users. Some of the form fields may be left
> empty.
>
> I want to create a query that will include all terms entered into a
> field in the form, search the "key word" columns for those terms and
> return the IDs of records that have those key words in their "key word"
> columns.
>
> I haven't started doing this, though I can see it will raise issues
> like vlidating data, what to do with empty form fields, and turning the
> form data into a proper SQL query. I can see using the SQL function
> IN() and a bunch of ORs.
>
> Does anyone know where I can find some procedural code (preferable) or
> an object class to help me do this?
>
> Thanks.
>
> --Kenoli
>
> Kenoli Oleari
> Neighborhood Assemblies Network
> kenoli@sfnan.org
> 510-601-8217
> http://www.sfnan.org
> http://www.horizonsofchange.com

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация