Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search with multiply words

    I'm about to update my search script. The search engine searches in my database where I ask it to search. What is new is I want do some search words, that the users can check in a checkbox, if he wants to use the word in the search.

    As for now my search engine works, the only problem is that it only searches the last word and not all of the checked words. My formula looks like this:

    PHP Code:
    <form method="POST" action="<?=$_SERVER["PHP_SELF"]?>"> 
    <p>Search for: 
    </p> 
    Books: <input type="checkbox" name='search' value="books"> 
    Movies: <input type="checkbox" name='search' value="movies"> 
    Outdoor: <input type="checkbox" name='search' value="outdoor"> 
    Indore: <input type="checkbox" name='search' value="indore"> 
    </p> 
    <p><input type='submit'  value='Search'></p> 
    </form>
    The php code looks like:
    PHP Code:
    <?php 
    if(isset($_POST['search'])) 

      
    $connx mysql_connect('localhost''*******'',*********') or die("connx"); 
      
    $db mysql_select_db('*********') or die(mysql_error()); 
      
      
    # convert to upper case, trim it, and replace spaces with "|": 
      
    $search mysql_real_escape_string($search); 
      
    $search strtoupper(preg_replace('/\s+/''|', ($_POST['search']))); 
      
      
    # create a MySQL REGEXP for the search: 
      
    $regexp "REGEXP '[[:<:]]($search)[[:>:]]'"
      
    $query "SELECT * FROM `keywords` WHERE UPPER(`keywords01`) $regexp OR "
               
    "`keywords02` $regexp OR "
               
    "`keywords03` $regexp OR "
               
    "`keywords04` $regexp"
      
      
    $result mysql_query($query) or die($query " - " mysql_error()); 
      
    echo 
    "<table>\n"
    while(
    $row mysql_fetch_assoc($result)) 


        echo 
    "<tr>"
        echo 
    "<td><img src=../{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>";
        echo 
    "<td>{$row['name']}</td>"
        echo 
    "<td>{$row['date']}</td>"
        echo 
    "<td><a href=../view.php?id={$row['id']} target=blank>VIEW</a></td>"
        echo 
    "</tr>\n"
      }  

     else {
      echo 
    "<p>Sorry, no results matched your search.</p>";
    }
    ?>
    Are there someone, who can figure out, why it is only the last marked checkboxs word that are searched and not all marked words and how do I get it to search for all marked words?

    Hope someone can help.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    The first problem is with your form itself ...

    name='search'

    You gave them all the same name, but didn't use an array.
    Do this instead for all of them ...

    name='search[]'

    Now when it comes to the PHP script, it will be an array that only contains the words that were checked.

    To test it out ....

    $words=$_POST['search'];
    foreach($words as $word){
    echo $word."<br>";
    }

  • #3
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see. I have now changed the form to:
    PHP Code:
    <form method="POST" action="<?=$_SERVER["PHP_SELF"]?>"> 
    <p>Search for: 
    </p> 
    Books: <input type="checkbox" name='search[]' value="books"> 
    Movies: <input type="checkbox" name='search[]' value="movies"> 
    Outdoor: <input type="checkbox" name='search[]' value="outdoor"> 
    Indore: <input type="checkbox" name='search[]' value="indore"> 
    </p> 
    <p><input type='submit'  value='Search'></p> 
    </form>

    Regarding your code:
    PHP Code:
    $words=$_POST['search'];
    foreach(
    $words as $word){
    echo 
    $word."<br>";

    Im unsure where to add it in my script:
    PHP Code:
    <?php  
    if(isset($_POST['search']))  
    {  
      
    $connx mysql_connect('localhost''*******'',*********') or die("connx");  
      
    $db mysql_select_db('*********') or die(mysql_error());  
       
      
    # convert to upper case, trim it, and replace spaces with "|":  
      
    $search mysql_real_escape_string($search);  
      
    $search strtoupper(preg_replace('/\s+/''|', ($_POST['search'])));  
       
      
    # create a MySQL REGEXP for the search:  
      
    $regexp "REGEXP '[[:<:]]($search)[[:>:]]'";  
      
    $query "SELECT * FROM `keywords` WHERE UPPER(`keywords01`) $regexp OR ".  
               
    "`keywords02` $regexp OR ".  
               
    "`keywords03` $regexp OR ".  
               
    "`keywords04` $regexp";  
       
      
    $result mysql_query($query) or die($query " - " mysql_error());  
       
    echo 
    "<table>\n";  
    while(
    $row mysql_fetch_assoc($result))  

    {  
        echo 
    "<tr>";  
        echo 
    "<td><img src=../{$row['type']}/{$row['folder']}/{$row['date']}-{$row['num']}/{$row['thumbimage']} border=1></td>"
        echo 
    "<td>{$row['name']}</td>";  
        echo 
    "<td>{$row['date']}</td>";  
        echo 
    "<td><a href=../view.php?id={$row['id']} target=blank>VIEW</a></td>";  
        echo 
    "</tr>\n";  
      }   
    }  
     else { 
      echo 
    "<p>Sorry, no results matched your search.</p>"

    ?>
    Ca you please help me adding your codes. I very new to PHP and how no idea where or how to add it. Really hope for your help in this matter. THANKS

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    My little piece of code was just an example to test out the checkbox array.

    You have to tell us more about 'keywords01', 'keywords02', 'keywords03', 'keywords04'

    Do those columns contain different things, or is each one assigned to one of the checkbox values (books, movies, outdoor, indoor)?

    Describe what the columns can contain, what kind of information.

    and check spelling on this: indore ... should be indoor.

  • #5
    New to the CF scene
    Join Date
    Nov 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for taking the time to answer me. I found out that if I replaced the lines:
    PHP Code:
      $search mysql_real_escape_string($search); 
      
    $search strtoupper(preg_replace('/\s+/''|', ($_POST['search']))); 
    With:
    PHP Code:
      $search implode'|'$_POST['search'] ); 
    Then my script is working

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Whoa. What?
    Something seems a little off here; is the $search you have here now representing a single property in a record?
    Are you searching a single table for this, or are you searching multiple? Since you have an established many to many relation, you should have up to three tables involved for this.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I'm questioning the 4 columns ... 'keywords01', 'keywords02', 'keywords03', 'keywords04'
    That is also going to affect the outcome? What do those columns contain?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    FYI mlseim, there's a thread open on the sql forum as well. My suspicion is the use of delimited lists (which is a no-no).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •