need help with a search engine script

Discussion in 'Web Design and Development' started by balam, Dec 16, 2009.

  1. balam macrumors newbie

    Joined:
    Dec 14, 2009
    #1
    Hi,

    i need help to make my search script more flexible. As it is now it will only give accurate result if i search the column called location but i want it to search a column called rent to. i.e notting hill £900, I have tried to change the query to

    $construct = "SELECT * FROM flats WHERE location, rent =$construct";

    but its not valid. heres the entire code.


    PHP:
    <?php

    include ('connect.php');

    error_reporting(E_ALL);
    ini_set('display_errors''1');

    $submit $_GET['submit'];
    $search $_GET['search'];
    $x=0;
    $construct='';
    $foundnum=0

    if (!
    $submit)
      
      echo 
    "you didnt submit a keyword."

    else

    {

    if (
    strlen($search)<=2)

       echo 
    "search term to short.";
     else 
    {
      echo 
    " You searched for <b>$search</b><hr size='1'>";

      
    //connect to our database

     
    $search_exploded explode(" ",$search);


     foreach(
    $search_exploded as $search_each)

    {

    // construct query

    $x++;
    if (
    $x==1)
        
    $construct .= " location LIKE '%$search_each%'"
        else
        
    $construct .= " OR rent LIKE '%$search_each%'";
        
          }

       
    // echo out construct
      
     
    $construct "SELECT * FROM flats WHERE location, rent =$construct";
     
    $run mysql_query($construct);
     
    $foundnum mysql_num_rows($run);

    if (
    $foundnum==0)
      echo 
    "No results found.";
    else
    {
       echo 
    "$foundnum result found!<p>";

     while (
    $runrows mysql_fetch_assoc($run))

    {

    // get data

       
    $select $runrows['type'];
       
    $title $runrows['title'];
       
    $location $runrows['location'];
       
    $rent $runrows['rent'];
       
    $description $runrows['description'];
       
    $contactEmail $runrows['contactEmail'];
       
    $number $runrows['number'];

    echo 
    "

        
    $title
        <br>
        
    $select
        <br>
        
    $rent
        <br>
        
    $location
        <br>
        
    $description
        <br>
        
    $contactEmail
        <br>
        
    $number
       <hr>"
    ;

    }     


          }
        }
      }


    ?>

     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    I believe you want,
    PHP:
    $construct "SELECT * FROM flats WHERE $construct";
    To see how the query ends up you can echo $construct out to make sure it's in proper syntax.
     

Share This Page