Hey I got problem with my(found) search code. If I search for something that is split into two columns like Name and Last name, it only works if the first name is only One name(word). For example If I got a Name column with two names such as Nancy Dandry, and a LastName column with 5 names, Tomson Craig John McGee Name, I can search for it only if I use one name(word) from the Name Column.
Example
:Search Field: Nancy John McGee Name = Results
:Search Field: Dandry Tomson John Tomson = Results
:Search Field: Nancy Dandry = No Results
:Search Field: Nancy Dandry Tomson = No Results.
Basically if the Name column has two or more words and I search for two or more words, I get no results at all. How do I got about fixing this? I really can't find anyway, I've searched all over... I believe the Culprit is the little snippet of code below, but I'm not sure.
I've been trying to tackle this issue several times for a few months and given up on it every time :& I've tried all the other options, but this is the closest I've been to solving it... Sounds like I'm building a rocket, but yeah... :)
$output = ' ' ;
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="Name LIKE '%$search_each%'";
else
$construct .="AND LastName LIKE '%$search_each%'";
}
The Form I'm using
<form action='sear.php' method='GET'>
<center>
<h1> Search</h1>
<input type='text' size='50' name='search'></br></br>
<input type='submit' name='submit' value='Search' ></br></br></br>
</center>
</form>
Full Php Code
<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
if(strlen($search)<=1)
echo "Search term is too short!";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","Name_Name","Password");
mysql_select_db("Database_Test");
$output = ' ' ;
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="Name LIKE '%$search_each%'";
else
$construct .="AND LastName LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM Information WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Excuse for not finding the result... None";
else
{
echo "$foundnum results found !<p>";
$per_page = 10;
$start = isset($_GET['start']) ? $_GET['start']: '';
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT id, Name, LastName FROM Information WHERE $construct ORDER BY id DESC LIMIT $start, $per_page");
while($runrows = mysql_fetch_assoc($getquery))
{
$id = $runrows ['id'];
$Name = $runrows ['Name'];
$LastName = $runrows ['LastName'];
$output .= ' <div class="Name">' . $Name . ' </div> <div class="LastName">' . $LastName . ' </div> ';
}
//Pagination Starts
echo "<center>";
$prev = $start - $per_page;
$next = $start + $per_page;
$adjacents = 3;
$last = $max_pages - 1;
if($max_pages > 1)
{
//previous button
if (!($start<=0))
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$prev'>Prev</a> ";
//pages
if ($max_pages < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
$i = 0;
for ($counter = 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
elseif($max_pages > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if(($start/$per_page) < 1 + ($adjacents * 2))
{
$i = 0;
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($i == $start){
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//in middle; hide some front and some back
elseif($max_pages - ($adjacents * 2) > ($start / $per_page) && ($start / $per_page) > ($adjacents * 2))
{
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=0'>1</a> ";
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start/$per_page)+1; $counter < ($start / $per_page) + $adjacents + 2; $counter++)
{
if ($i == $start){
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
//close to end; only hide early pages
else
{
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=0'>1</a> ";
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$per_page'>2</a> .... ";
$i = $start;
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$i'>$counter</a> ";
}
$i = $i + $per_page;
}
}
}
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='sear.php?search=$search&submit=Search+names+keyword&start=$next'>Next</a> ";
}
echo "</center>";
}
}
?>