This is the sort of thing I would do
$tmpquery = "SELECT item_pic, cat, sub, item_name, username FROM item_name
WHERE 1=1 AND";
if($category)
{
$filter1 = " cat =\"";
$filter1 .= implode("\",\"",$category);
$filter1 .= " \" ";
$tmpquery .= $filter1;
$tmpquery .= " AND ";
}
if($subcat)
{
$filter2 = " sub =\"";
$filter2 .= implode("\",\"", $subcat);
$filter2 .= "\"";
$tmpquery .= $filter2;
$tmpquery .= " AND ";
}
$query=substr($tmpquery,0,strlen($tmpquery) - 4); // remove extra AND
I haven't tried this with checkboxes, but you should get the idea. This
works with other
queries that I have done.
John Lodge
-----Original Message-----
From: Trelfa, Jonathon [mailto:[EMAIL PROTECTED]]
Sent: Friday, March 01, 2002 3:45 PM
To: SQL Mailing List (E-mail)
Subject: Complex query with PHP/MySQL
I am attempting to generate a query statement (using PHP) that is generated
based on user input from 2 sets of checkboxes. You can see a
barely-functioning version at this URL:
http://www.nobleaccord.com/view/view.php
The top part of the form just shows what the query statement looks like.
The bottom form actually shows something (it's a much much simpler query).
Anyway, my goal is to be able to make a complex filter for the query so that
the items I want to see are shown. Here's the code for the form process:
<h3>The results of your query are as follows:</h3>
<table border=0 cellspacing=0 cellpadding=0 align=center width=90%>
<tr>
<td>
<?PHP
$connect = mysql_connect ($host, $user, $pass) or die("Unable to connect to
database");
@mysql_select_db("nobleacc_items",$connect) or die ("Unable to select data
table");
if ($submit1)
{
$filter1 = " cat =\"";
$filter1 .= implode("\",\"",$category);
$filter1 .= " \" ";
$filter2 = " sub =\"";
$filter2 .= implode("\",\"", $subcat);
$filter2 .= "\"";
$query = "SELECT item_pic, cat, sub, item_name, username FROM item_name
WHERE $filter1 AND ($filter2)";
$result = mysql_query ($query, $connect);
print "<b>The first filter is:</b> $filter1<br>";
print "<b>The second filter is:</b> $filter2<br>";
print "<b>The query you just built is:</b> $query<br>";
?>
My question is this...how do I make this work better? I am getting errors
as it stands right now so I am pretty sure that the query that is built
isn't correct.
Thanks,
Jon Trelfa
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail
<[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php
---------------------------------------------------------------------
Before posting, please check:
http://www.mysql.com/manual.php (the manual)
http://lists.mysql.com/ (the list archive)
To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php