Jump to content

Submitting Multiple variables in a search


JAMerlino

Recommended Posts

Hi all, I am new here and have a question.  I have created a search tool for a database table which I am currently searching through the Description column in the table but would like to add the Title, and the Category columns as well.  My script is below.  it searches, creates the table adn displays adn paginates perfectly, but as soon as I try to add more variables it crashes.  It seems to be in the $construct variable I am building to fetch adn display the results. 

 

Any assistance would be great.

I would like to be able to, in index.php search feild, put Burger and it would look through Title, Catagory, adn Description within the table and display the results.

 

 

This is the Index.php script

<html>
<head>
<title>RECIPE SEARCH</title>
</head>
<body>
<form action='search.php' method='GET'>
<center>
<h1>CHEF JAMIE RECIPE SEARCH TOOL</h1><br/>
<h4>Currently searching through recipe ingredients.</h4>
<input type='text' size='50' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>

And this is the search.php page

<?php
    
$button = $_GET ['submit'];
$search = $_GET ['search'];
  
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","xxxx","xxxxxx!");
mysql_select_db("chefjami_CJG2");
    
$search_exploded = explode (" ", $search);
    
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="Description LIKE '%$search_each%'";
else
$construct .="AND Description LIKE '%$search_each%'";
}
  
$constructs ="SELECT * FROM recipes WHERE $construct";
$run = mysql_query($constructs);
    
$foundnum = mysql_num_rows($run);
    
if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling. </br>4. <a href='index.php'>Go back to search</a>";
else
{
  
echo "$foundnum results found !  <a href='index.php'>Go back to search</a><p>";
  
$per_page = 1;
$start = $_GET['start'];
$max_pages = ceil($foundnum / $per_page);
if(!$start)
$start=0;
$getquery = mysql_query("SELECT * FROM recipes WHERE $construct LIMIT $start, $per_page");
  
while($runrows = mysql_fetch_assoc($getquery))
{
$Title = $runrows ['Title'];
$Description = $runrows['Description'];
$Catagory = $runrows ['Catagory'];
   
echo "
<b><h3>$Title</h3></b><br>
$Description<br>
<strong>Category: $Catagory</strong><p>
<hr>
";
   
}
  
//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='search.php?search=$search&submit=Search+source+code&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='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&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='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&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='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&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='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";
}  
$i = $i + $per_page;               
}
                                  
}
//close to end; only hide early pages
else
{
echo " <a href='search.php?search=$search&submit=Search+source+code&start=0'>1</a> ";
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$per_page'>2</a> .... ";
 
$i = $start;               
for ($counter = ($start / $per_page) + 1; $counter <= $max_pages; $counter++)
{
if ($i == $start){
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'><b>$counter</b></a> ";
}
else {
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$i'>$counter</a> ";  
}
$i = $i + $per_page;             
}
}
}
          
//next button
if (!($start >=$foundnum-$per_page))
echo " <a href='search.php?search=$search&submit=Search+source+code&start=$next'>Next</a> ";   
}  
echo "</center>";
}
}
?>
Link to comment
Share on other sites

Try something like this:

$search_exploded = array_map('mysql_real_escape_string', explode(' ', $search));

foreach($search_exploded as $search_each)
{
    $construct[] ="(Description LIKE '%$search_each%' OR
    Catagory LIKE '%$search_each%' OR
    Title LIKE '%$search_each%')";
}
$construct = implode(' OR ', $construct);
Edited by AbraCadaver
Link to comment
Share on other sites

@AbraCadaver,

 

That won't work as requested. That would find any results where the words exist in any of the fields. The request, as I understood it is where all the words exist in Description, or all the words exist in Title, or all the words exist in Category.

 

@JAMerlino,

 

You should really do this through FULL TEXT searching. This requires modifications to your tables and different ways of running the queries. You can Google how to do that if you want to do it that way. Else, with your current setup, this should work. Seems a little verbose, but it is easily configurable and should be easy to follow.

 

 

//Create array of all fields to run search against
$searchFields = array('Description', 'Title', 'Category');

//Create array of search words
$searchWords = array_filter(explode (' ', $search));

//Create template of search parts (with placeholder)
$searchParts = array();
foreach($searchWords as $word)
{
    $searchParts[] = "FIELD_NAME LIKE '%{$word}%'";
}

//Implode the parts into a complete search string for a field
$searchString = "(" . implode(' AND ', $searchParts)  . ")";

//Create search strings for all fields replacing placeholder
$constructParts = array;
foreach($searchFields as $fieldName)
{
    $constructParts[] = str_replace('FIELD_NAME', $fieldName, $searchString);
}

//Combine into a single string
$construct = implode(' OR ' $constructParts);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.