I have a form with 4 fields. I want to be able to search with keywords in one or more field. The problem I have is my count is miscounting the keywords. As long as there is a keyworn in the last field it counts correctly, but if this last field is left blank it still counts it as a word. I've got a few other problems but that is the one I'm stuck on. Any thoughs?
Am I going about this multi=field search the wrong way?
<?php
$username="****";
$password="****";
$database="****";
mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
// retrieve form data
$raidTarget = $_POST['raidTarget'];
$raidDate = $_POST['raidDate'];
$raidLeader = $_POST['raidLeader'];
$raidZone = $_POST['raidZone'];
//Following code checks to see if fields are empty, if not it adds the keyword to the $keyword value
if(!empty($raidTarget)){
$keywords .= $raidTarget;
$keywords .=" ";
}
if(!empty($raidDate)){
$keywords .= $raidDate;
$keywords .=" ";
}
if(!empty($raidLeader)){
$keywords .= $raidLeader;
$keywords .=" ";
}
if(!empty($raidZone)){
$keywords .= $raidZone;
}
$pieces = explode(" ", $keywords);
$count=count($pieces);
$query="SELECT * FROM Raid WHERE ";
echo $count, " keywords";
if ($count==1)
{
$query.="target = '$keywords' OR date ='$keywords' OR leader ='$keywords' OR zone ='$keywords'";
}
elseif ($count>=1)
{
for($a=0;$a<$count;$a++)
{
echo $pieces[$a];
$query.="target = '$pieces[$a]' OR date ='$pieces[$a]' OR leader ='$pieces[$a]' OR zone ='$pieces[$a]'";
if ($a != $count-1)
{
//$query.=" AND ";
}
}
}
echo $query;
$result=mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo '<p>Target: ' . $row['target'] . '</p>';
echo '<p>Date: ' . $row['date'] . '</p>';
echo '<p>Raid Leader: ' . $row['leader'] . '</p>';
echo '<p>Zone: ' . $row['zone'] . '</p>';
echo '<p>Raid duration: ' . $row['start_time'];
echo ' to ' . $row['end_time'] . '</p>';
echo '<p>Attendance: ' . $row['attendance'] . '</p>';
echo '<p>Late: ' . $row['late_attendance'] . '</p>';
echo '<p>Left early: ' . $row['left_attendance'] . '</p>';
}
mysql_close();
?>