Search a mysql array using checkboxes

Hi,

I’m trying to script a search for a database. The script works fine except for the array search. I have a database entry form for car accessories which will implode the checkboxes into an array and place it into one field.

The trouble I’m having is searching this field using the same checkboxes. if someone checks the first checkbox and, say the 5th checkbox, the search will return the results for the first checkbox, ignoring the 5th checkbox where I want both checkbox values to be searched for.

Here is the search form:

<form name="search" action="searching.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="10%">Make:</td>
    <td width="39%"><input name="manufacturer" type="text" size="10" maxlength="10" /></td>
    <td width="10%">Model:</td>
    <td width="41%"><input name="model" type="text" size="10" maxlength="10" /></td>
    </tr>
  <tr>
    <td colspan="4">Price:</td>
    </tr>
  <tr>
    <td>From:      </td>
    <td><input name="price_from" type="text" size="10" maxlength="10" /></td>
    <td>To:      </td>
    <td><input name="price_to" type="text" size="10" maxlength="10" /></td>
  </tr>
  </table>
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <td colspan="4"><h3 align="center" class="h3">Included Features</h3></td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]0" type="checkbox" value="Airbag(s)" />
    </div></td>
    <td>Airbag(s)</td>
    <td><div align="center">
      <input name="accessories[]1" type="checkbox" value="Antilock Braking System" />
    </div></td>
    <td>Antilock Braking System</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]2" type="checkbox" value="Air Conditioning" />
    </div></td>
    <td>Air Conditioning</td>
    <td><div align="center">
      <input name="accessories[]3" type="checkbox" value="Automatic Gearbox" />
    </div></td>
    <td>Automatic Gearbox</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]4" type="checkbox" value="Alloy wheels" />
    </div></td>
    <td>Alloy wheels</td>
    <td><div align="center">
      <input name="accessories[]5" type="checkbox" value="Climate Control" />
    </div></td>
    <td>Climate Control</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]6" type="checkbox" value="Central Locking" />
    </div></td>
    <td>Central Locking</td>
    <td><div align="center">
      <input name="accessories[]7" type="checkbox" value="Cruise Control" />
    </div></td>
    <td>Cruise Control</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]8" type="checkbox" value="Diesel" />
    </div></td>
    <td>Diesel</td>
    <td><div align="center">
      <input name="accessories[]9" type="checkbox" value="DVD System" />
    </div></td>
    <td>DVD System</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]10" type="checkbox" value="Estate" />
    </div></td>
    <td>Estate</td>
    <td><div align="center">
      <input name="accessories[]11" type="checkbox" value="Electric Windows" />
    </div></td>
    <td>Electric Windows</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]12" type="checkbox" value="Power Assisted Steering" />
    </div></td>
    <td>Power Assisted Steering</td>
    <td><div align="center">
      <input name="accessories[]13" type="checkbox" value="R/C Central Locking" />
    </div></td>
    <td>R/C Central Locking</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]14" type="checkbox" value="Satellite Navigation" />
    </div></td>
    <td>Satellite Navigation</td>
    <td><div align="center">
      <input name="accessories[]15" type="checkbox" value="Traction Control" />
    </div></td>
    <td>Traction Control</td>
    </tr>
  <tr>
    <td><div align="center">
      <input name="accessories[]16" type="checkbox" value="Sunroof" />
    </div></td>
    <td>Sunroof</td>
    <td><div align="center">
      <input name="accessories[]17" type="checkbox" value="Electric Sunroof" />
    </div></td>
    <td>Electric Sunroof</td>
    </tr>
    </table><br />
      <br /><div align="center">
  <input name="Submit" type="submit" value="Search" />
</div>
</form>

And the Searching Script:

<?
$manufacturer = $_GET["manufacturer"];
$model = $_GET["model"];
$price_from = $_GET["price_from"];
$price_to = $_GET["price_to"];
$accessories_1 = $_GET['accessories'][0] ;
$accessories_2 = $_GET['accessories'][1] ;
$accessories_3 = $_GET['accessories'][2] ;
$accessories_4 = $_GET['accessories'][3] ;
$accessories_5 = $_GET['accessories'][4] ;
$accessories_6 = $_GET['accessories'][5] ;
$accessories_7 = $_GET['accessories'][6] ;
$accessories_8 = $_GET['accessories'][7] ;
$accessories_9 = $_GET['accessories'][8] ;
$accessories_10 = $_GET['accessories'][9] ;
$accessories_11 = $_GET['accessories'][10] ;
$accessories_12 = $_GET['accessories'][11] ;
$accessories_13 = $_GET['accessories'][12] ;
$accessories_14 = $_GET['accessories'][13] ;
$accessories_15 = $_GET['accessories'][14] ;
$accessories_16 = $_GET['accessories'][15] ;
$accessories_17 = $_GET['accessories'][16] ;
$accessories_18 = $_GET['accessories'][17] ;
$string = $_SERVER['QUERY_STRING'] ;
if (!$price_from){
$price_from = '0';
}
if (!$price_to){
$price_to = '10000000';
}

$page = (!isset($_GET['page']))? 1 : $_GET['page']; 
$prev = ($page - 1); 
$next = ($page + 1); 
/* Max results per page */ 
$max_results = 5; 
/* Calculate the offset */ 
$from = (($page * $max_results) - $max_results); 
/* Query the db for total results. You need to edit the sql to fit your needs */ 
$result = mysql_query("SELECT * FROM cars WHERE price BETWEEN '$price_from' AND '$price_to' AND manufacturer LIKE '%$manufacturer%' AND model LIKE '%$model%' OR accessories_1 LIKE '%$accessories_1%' OR '%$accessories_2%' OR '%$accessories_3%' OR '%$accessories_4%' OR '%$accessories_5%' OR '%$accessories_6%' OR '%$accessories_7%' OR '%$accessories_8%' OR '%$accessories_9%' OR '%$accessories_10%' OR '%$accessories_11%' OR '%$accessories_12%' OR '%$accessories_13%' OR '%$accessories_14%' OR '%$accessories_15%' OR '%$accessories_16%' OR '%$accessories_17%' OR '%$accessories_18%'"); 
$total_results = mysql_num_rows($result); 
$total_pages = ceil($total_results / $max_results); 
$pagination = ''; 
/* Create a PREV link if there is one */ 
if($page > 1) 
{ 
$pagination .= '<a href="searching.php?'.$string.'&page='.$prev.'">Previous</a> '; 
} 
/* Loop through the total pages */ 
for($i = 1; $i <= $total_pages; $i++) 
{ 
if($i == $page) 
{ 
$pagination .= $i; 
} 
else 
{ 
$pagination .= '&nbsp;<a href="searching.php?'.$string.'&page='.$i.'">'.$i.'</a>&nbsp;'; 
} 
} 
/* Print NEXT link if there is one */ 
if($page < $total_pages) 
{ 
$pagination .= '<a href="searching.php?'.$string.'&page='.$next.'">Next</a>'; 
} 
$result2 = mysql_query("SELECT * FROM cars WHERE price BETWEEN '$price_from' AND '$price_to' AND manufacturer LIKE '%$manufacturer%' AND model LIKE '%$model%' AND accessories_1 LIKE '%$accessories_1%' OR '%$accessories_2%' OR '%$accessories_3%' OR '%$accessories_4%' OR '%$accessories_5%' OR '%$accessories_6%' OR '%$accessories_7%' OR '%$accessories_8%' OR '%$accessories_9%' OR '%$accessories_10%' OR '%$accessories_11%' OR '%$accessories_12%' OR '%$accessories_13%' OR '%$accessories_14%' OR '%$accessories_15%' OR '%$accessories_16%' OR '%$accessories_17%' OR '%$accessories_18%' ORDER BY price DESC LIMIT $from, $max_results");
if (mysql_num_rows($result2) == 0){
echo "<div align='center'>No Vehicles Found in your Search. <form action='search.php'><input type=submit value='Search Again'></form></div>";
}
elseif (mysql_num_rows($result2) == 1){
echo "<p>Your search returned <b>". $total_results ."</b> Vehicle:</p>";
}
else {
echo "<p>Your search returned <b>". $total_results ."</b> Vehicles:</p>";
}
//grab all the content
while($row=mysql_fetch_array($result2))
{ 
$special=$row["special"];
if ($special=='no'){
echo "<table width='550' border='0' cellspacing='0' cellpadding='0' class='list_bg'>";
echo "<tr width='550'>";
 echo "<td class='list_head_tl'>"."&nbsp;</td>";
 echo "<td align='center' class='list_head'><b>" . $row['manufacturer'] . "&nbsp;" . $row['model'] . "&nbsp;" . $row['engine'] . "</b></td>";
 echo "<td class='list_head_tr'>"."&nbsp;</td>";
 echo "<td class='list_head_trr'>"."&nbsp;</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4' class='list_top'>&nbsp;</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td>"."&nbsp;</td>";
 echo "<td>"."<div class='outerpair1'><div class='outerpair2'><div class='shadowbox'><div class='innerbox'><a href='car_view.php?id=". $row['id'] ."'><img src='imagethumb.php?s=". $row['image_1'] ."&w=166' title='". $row['manufacturer'] ."&nbsp;". $row['model'] ."&nbsp;". $row['engine'] ."' width='166' border='0'></a></div></div></div></div></td>";
 echo "<td>"."&nbsp;</td>";
 echo "<td align='justify'>".$row['description']."</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4'>"."<table width='500' border='0' cellspacing='0' cellpadding='0' align='center'><tr><td>".$row['accessories_1']."</td></tr></table></td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4'>"."<table width='500' border='0' cellspacing='0' cellpadding='0' align='center'><tr><td align='right'><a href='car_view.php?id=". $row['id'] ."'>More Information</a></td></tr></table></td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4' class='list_bot'>"."&nbsp;</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4'><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
 echo "<tr>";
 echo "<td class='list_bot_bll'>"."&nbsp;</td>";
 echo "<td width='23' class='list_price_bl'>"."&nbsp;</td>";
 echo "<td width='174' align='center' class='list_price'><b>". "Price: &euro;". number_format ($row['price'], 0, "", ","),"</b></td>";
 echo "<td width='23' class='list_price_br'>"."&nbsp;</td>";
 echo "</tr>";
 echo "</table></td>";
 echo "</tr>";
 } else {
 echo "<table width='550' border='0' cellspacing='0' cellpadding='0' class='list_bg'>";
 echo "<tr width='550'>";
 echo "<td class='list_head_tl'>"."&nbsp;</td>";
 echo "<td align='center' class='list_head'><b>" . $row['manufacturer'] . "&nbsp;" . $row['model'] . "&nbsp;" . $row['engine'] . "</b></td>";
 echo "<td class='list_head_tr'>"."&nbsp;</td>";
 echo "<td class='list_head_trr'>"."&nbsp;</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4' class='list_top'>&nbsp;</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td>"."&nbsp;</td>";
 echo "<td>"."<div class='outerpair1'><div class='outerpair2'><div class='shadowbox'><div class='innerbox'><a href='car_view.php?id=". $row['id'] ."'><img src='imagethumb.php?s=". $row['image_1'] ."&w=166' title='". $row['manufacturer'] ."&nbsp;". $row['model'] ."&nbsp;". $row['engine'] ."' width='166' border='0'></a></div></div></div></div></td>";
 echo "<td>"."&nbsp;</td>";
 echo "<td align='justify'>".$row['description']."</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4'>"."<table width='500' border='0' cellspacing='0' cellpadding='0' align='center'><tr><td>".$row['accessories_1']."</td></tr></table></td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4'>"."<table width='500' border='0' cellspacing='0' cellpadding='0' align='center'><tr><td align='right'><a href='car_view.php?id=". $row['id'] ."'>More Information</a></td></tr></table></td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4' class='list_bot'>"."<div class='special_price' align='right'>Old Price: &euro;". number_format ($row['price'], 0, "", ","),"</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td colspan='4'><table width='100%' border='0' cellspacing='0' cellpadding='0'>";
 echo "<tr>";
 echo "<td class='list_bot_bll'>"."&nbsp;</td>";
 echo "<td width='23' class='list_price_bl'>"."&nbsp;</td>";
 echo "<td width='174' align='center' class='list_price'><b>". "Special Price: &euro;". number_format ($row['special_price'], 0, "", ","),"</b></td>";
 echo "<td width='23' class='list_price_br'>"."&nbsp;</td>";
 echo "</tr>";
 echo "</table></td>";
 echo "</tr>";
 }
   }
 echo "</table>";
 echo "<div align='right'>". $pagination;
?>

Can anyone see what I am doing wrong, or where I might have to include an in_array?