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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stuck on checkbox query

Status
Not open for further replies.

tripleti

Technical User
Jun 3, 2007
3
US
I need to use two columns of checkboxes to query a mysql db. One column is location and one is size. I can do a query with a drop down menu selection but am stuck on this using checkboxes.

Can you help or tell me where can I obtain a script for this?
 
a hint: only those checkboxes that are checked are submitted by the browser.

store the values of the checkboxes in the name, not the value

e.g.
Code:
<input type="checkbox" name="cbox[value1]" /> Value 1

then in your receiving script

Code:
foreach ($_POST['cbox'] as $val=>$i){
  $values[] = $val;
}
$query = "Select * from table where id IN ("'. implode("','",$values) . "')";
 
Am I allowed to post my current code here?
 
Here is my current code using a one drop down menu. I need to replace it with the two checkbox selections, Location (riverside, L A, Orange) and Size(under 1400, 1400 - 2000, over 2000). One must be selected from each column. Location and Size represent two columns of the db.

<script language="JavaScript">

function next(msg) {
document.location = "directory.php?select=" + msg + "&Submit=Select";
}

</script>



<?
// Connect database
mysql_connect("host","property","lop1");
mysql_select_db("property");

$select = "--select--";

// If submitted, check the value of "select". If its not blank value, get the value and put it into $select.
if(isset($select)&&$select!=""){
if(!empty($_GET['select'])){ $select = $_GET['select']; }
}
?>
<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
<!--
}
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 18px;
}

.style2 {
color: #0000FF;
font-size: 12;
}

.style3 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 11px;
font-weight: bold;
}
.style1a { font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #c07e04;
font-weight: bold;
}
.style1b { font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
}
.style1c { font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #000000;
font-weight: bold;
}
table.sample { border-width: thin thin thin thin;
border-spacing: 0px;
border-style: solid solid solid solid;
border-color: black black black black;
border-collapse: separate;
}
.style4 {color: #000000}
.style6 {color: #0000FF; font-family: Arial, Helvetica, sans-serif;}
.style8 {color: #000000; font-weight: bold; }
.style9 {
color: #FFFFFF;
font-weight: bold;
}
-->
</style>
</head>
<body MARGINWIDTH="0" MARGINHEIGHT="0" LEFTMARGIN="0" TOPMARGIN="0"
background="web_pics/background.jpg">
<div></div>
<table width="848" height="254" border="0">

<tr>
<td width="214" rowspan="4">&nbsp;</td>
<td width="624" height="23" valign="top"><span class="style1">Space Available</span></td>
</tr>
<tr>
<td height="4" valign="top"><img src="pics/line.jpg" alt="line" width="550" height="3" /></td>
</tr>
<tr>
<td height="21" valign="top">&nbsp;</td>
</tr>
<tr>
<td align="center" valign="top"><p>
<?php
$list=mysql_query("select distinct Name from properties order by id asc");

// Show records by while loop.
//while($row_list=mysql_fetch_assoc($list)){
// echo $row_list['Footage']."<br>";
//}

?>
</p>
<form method="get" name" class="style2" id="form1""form1 >
<span class="style8">Search by square footage: </span>
<select name="select" class="style6" onChange="next(form1.select.value);">
<option value="">--- Select ---</option>
<?
// Get records from database (table "nelson_property").
$list=mysql_query("select distinct Footage from properties order by id asc");

// Show records by while loop.
while($row_list=mysql_fetch_assoc($list)){
?>
<option value="<? echo $row_list['Footage']; ?>"
<? if($row_list['ID']==$select){ echo "selected"; } ?>> <? echo $row_list['Footage']; ?></option>
<?
// End while loop.
}
?>
</select>
</form>

<?
// If you have selected from list box.
if(isset($select)&&$select!="" && $select != "--select--" ){

// Get records from database (table "nelson_property").
$result=mysql_query("select * from properties where Footage='$select'");
//echo "<h1>".$select."</h1><br>";
while($row=mysql_fetch_assoc($result)) {




?> </td>
</tr>
<tr>
<td>&nbsp;</td>
<td height="97" align="center" valign="top"><table width="600" height="250" align="center" class="sample">
<tr>
<td width="651" height="362" valign="top"><table width="598" height="250" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td height="22" colspan="7" align="left" valign="top"><span class="style1c"><? echo $row['Name']; ?><br>
</span><span class="style1b"><? echo $row['Street_Number']; ?> <? echo $row['Street_Name']; ?><br>
<? echo $row['City']; ?>, <? echo $row['State']; ?></span></td>
</tr>
<tr>
<td height="22" colspan="7" align="left" valign="top">&nbsp;</td>
</tr>
<tr>
<td height="22" align="center" valign="top"><strong>Address</strong></td>
<td align="center" valign="top"><strong>Suite</strong></td>
<td align="center" valign="top"><strong>Size</strong></td>
<td align="center" valign="top"><strong>% Office </strong></td>
<td height="22" align="center" valign="top"><strong>Floor Plan </strong></td>
<td height="22" align="center" valign="top"><strong>Description</strong></td>
<td height="22" align="center" valign="top"><strong>Price</strong></td>
</tr>
<tr>
<td width="88" align="center" valign="top" class="style1b"><? if( $row['Address_1'] != "") {
echo $row['Address_1'];}?> </td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_1'] != "") {
echo $row['Suite_1'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_1'] != "") {
echo $row['Size_1'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_1'] != "") {
echo $row['%Office_1'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_1'] != "") {
echo $row['FloorPlan_1'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_1'] != "") {
echo $row['Suite_Desc_1'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_1'] != "") {
echo $row['Price_1'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_2'] != "") {
echo $row['Address_2'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_2'] != "") {
echo $row['Suite_2'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_2'] != "") {
echo $row['Size_2'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_2'] != "") {
echo $row['%Office_2'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_2'] != "") {
echo $row['FloorPlan_2'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_2'] != "") {
echo $row['Suite_Desc_2'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_2'] != "") {
echo $row['Price_2'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_3'] != "") {
echo $row['Address_3'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_3'] != "") {
echo $row['Suite_3'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_3'] != "") {
echo $row['Size_3'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_3'] != "") {
echo $row['%Office_3'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_3'] != "") {
echo $row['FloorPlan_3'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_3'] != "") {
echo $row['Suite_Desc_3'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_3'] != "") {
echo $row['Price_3'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_4'] != "") {
echo $row['Address_4'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_4'] != "") {
echo $row['Suite_4'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_4'] != "") {
echo $row['Size_4'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_4'] != "") {
echo $row['%Office_4'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_4'] != "") {
echo $row['FloorPlan_4'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_4'] != "") {
echo $row['Suite_Desc_4'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_4'] != "") {
echo $row['Price_4'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_5'] != "") {
echo $row['Address_5'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_5'] != "") {
echo $row['Suite_5'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_5'] != "") {
echo $row['Size_5'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_5'] != "") {
echo $row['%Office_5'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_5'] != "") {
echo $row['FloorPlan_5'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_5'] != "") {
echo $row['Suite_Desc_5'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_5'] != "") {
echo $row['Price_5'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_6'] != "") {
echo $row['Address_6'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_6'] != "") {
echo $row['Suite_6'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_6'] != "") {
echo $row['Size_6'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_6'] != "") {
echo $row['%Office_6'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_6'] != "") {
echo $row['FloorPlan_6'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_6'] != "") {
echo $row['Suite_Desc_6'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_6'] != "") {
echo $row['Price_6'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_7'] != "") {
echo $row['Address_7'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_7'] != "") {
echo $row['Suite_7'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_7'] != "") {
echo $row['Size_7'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_7'] != "") {
echo $row['%Office_7'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_7'] != "") {
echo $row['FloorPlan_7'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_7'] != "") {
echo $row['Suite_Desc_7'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_7'] != "") {
echo $row['Price_7'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_8'] != "") {
echo $row['Address_8'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_8'] != "") {
echo $row['Suite_8'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_8'] != "") {
echo $row['Size_8'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_8'] != "") {
echo $row['%Office_8'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_8'] != "") {
echo $row['FloorPlan_8'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_8'] != "") {
echo $row['Suite_Desc_8'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_8'] != "") {
echo $row['Price_8'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_9'] != "") {
echo $row['Address_9'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_9'] != "") {
echo $row['Suite_9'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_9'] != "") {
echo $row['Size_9'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_9'] != "") {
echo $row['%Office_9'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_9'] != "") {
echo $row['FloorPlan_9'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_9'] != "") {
echo $row['Suite_Desc_9'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_9'] != "") {
echo $row['Price_9'];}?>
</span></td>
</tr>
<tr>
<td width="88" align="center" valign="top"><span class="style1b">
<? if( $row['Address_10'] != "") {
echo $row['Address_10'];}?>
</span></td>
<td width="44" align="center" valign="top"><span class="style1b">
<? if( $row['Suite_10'] != "") {
echo $row['Suite_10'];}?>
</span></td>
<td width="57" align="center" valign="top"><span class="style1b">
<? if( $row['Size_10'] != "") {
echo $row['Size_10'];}?>
</span></td>
<td width="65" align="center" valign="top"><span class="style1b">
<? if( $row['%Office_10'] != "") {
echo $row['%Office_10'];}?>
</span></td>
<td width="72" align="center" valign="top"><span class="style1b">
<? if( $row['FloorPlan_10'] != "") {
echo $row['FloorPlan_10'];}?>
</span></td>
<td width="171" align="left" valign="top"><span class="style1b">
<? if( $row['Suite_Desc_10'] != "") {
echo $row['Suite_Desc_10'];}?>
</span></td>
<td width="98" align="center" valign="top"><span class="style1b">
<? if( $row['Price_10'] != "") {
echo $row['Price_10'];}?>
</span></td>
</tr>
<tr>
<td height="10" colspan="7" align="center" valign="bottom"><span class="style3">For additional information on above property, please
<?
if( $row['Web'] != "") {
echo '<a href="'.$row['Web'].'" target="_self">click here</a>';
}
?>
&nbsp;
to visit the <br />
associated location on this website.</span></td>
</tr>
</table>
</table>
<br>
<?
// End While statement
}
// End if statement.
}


// Close database connection.
mysql_close();
?></td>
</tr>
<tr>
<td>&nbsp;</td>
<td height="97" align="center" valign="top"><table border="1" align="center" cellpadding="2" cellspacing="0" bgcolor="#FFFFFF">
<tbody>
<tr>
<td width="134" align="center" bgcolor="#000000" class="regular"><b><font color="white">Location</font></b></td>
<td width="145" align="center" bgcolor="#000000" class="regular style9">Contact</td>
<td width="145" align="center" bgcolor="#000000" class="regular"><b><font color="white">Phone number</font></b></td>
<td width="165" align="center" bgcolor="#000000" class="regular"><b><font color="white">Email</font></b></td>
</tr>
<tr>
<td class="regular"><span class="style4">Riverside County, CA</span></td>
<td class="regular"><div align="center">Rhonda Grove</div></td>
<td class="regular"><div align="center">951-487-0080 </div></td>
<td class="regular">&nbsp;</td>
</tr>
<tr>
<td class="regular"><span class="style4">Pima County, AZ </span></td>
<td class="regular"><div align="center"></div></td>
<td class="regular"><div align="center"><span class="style4">XXX-XXX-XXXX</span></div></td>
<td class="regular">&nbsp;</td>
</tr>
<tr>
<td class="regular"><span class="style4">Mojave County, AZ </span></td>
<td class="regular"><div align="center">Cheri Mahan </div></td>
<td class="regular"><div align="center">928-758-0063</div></td>
<td class="regular">&nbsp;</td>
</tr>
</tbody>
</table></td>
</tr>
</table>

<p><br>
</p>
<p><br>
<br>
</p>
</body>
</html>
 
you can post code. but you need to post only those elements which evidence or support your problem/issue.

I have told you how to handle this kind of issue. It is now incumbent upon you to show us what you did with my suggestion, how you have moved your thinking forward etc. We're here to help you, not to spoon feed you.

you might want to check out this link
FAQ222-2244: How to get the best answers
 
I would revise my DB Normal form and then afterwards, I would make a simple function that can accept the $row[''] values. That function could then return the correct data, if any.

If you use relations in your database, you can simply do a join and it will be way way better for displaying data.

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top