Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 18th, 2004, 10:32 AM
Registered User
 
Join Date: Mar 2004
Location: Ikeja, Lagos, Nigeria.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to keph
Default search for multiple words

I have this script that searches and returns the desired result if only one search term (one word) is entered. It will return nothing with more than one word, just a blank page. I've tried using split/explode and then itterating the array in a search loop, and I'm still not getting anything meaningful.

Some pls help

http://www.kephassolutions.com
  #2 (permalink)  
Old March 18th, 2004, 02:00 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Can you please post your code for my better understanding?

----------
---Snib---
----------

<><
  #3 (permalink)  
Old March 22nd, 2004, 12:33 PM
Registered User
 
Join Date: Mar 2004
Location: Ikeja, Lagos, Nigeria.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to keph
Default

Here is the code that searches the database;
//=================================================

            if (!($conn = db_connect()))
            return false;
        $result = mysql_query( "select id, name
                                 from list
                WHERE name LIKE '%$searchterm%'
    OR addr LIKE '%$searchterm%'
    OR state LIKE '%$searchterm%'
    order by name
    $limit_str
    ");

         if (!$result)
             return false;

  // make an array of the churchids

    $all_churchid = array();

    for ($count = 1; $row = mysql_fetch_row($result); ++$count)
      {
         $all_churchid[$count] = $row[0];
      }
        return $all_churchid;
//==================================================

This works fine when I search for just one word. But if I enter 2 or more words at a time, it says "no match for my search" and if I try spliting/exploding the '$searchterm' before running the query, I get no result


http://www.kephassolutions.com
  #4 (permalink)  
Old March 23rd, 2004, 09:33 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hmm. How are you handling the split cases? You didn't post code that deals with multiple search terms. split() and explode() return an array, so if you used that return value as a string, you'd just search for the string "Array" in your database.


$terms = preg_split("/\s+/", $searchterm); // split on whitespace

$where_clauses = array();

foreach ($terms as $term)
{
    $where_clauses[] = "name LIKE '%{$term}%'";
    $where_clauses[] = "addr LIKE '%{$term}%'";
    $where_clauses[] = "state LIKE '%{$term}%'";
}

$where_clause = " WHERE " . join ("\n OR ", $where_clauses);



Suppose your $searchterm was "hello world".

This would be split into an array containing "hello" and "world".

For each of these terms, you'd create an array ($where_clauses) containing the <col> LIKE <term> for your three columns.

Finally, you join them all together, separated by "OR" (with some formatting whitespace).

The result:

WHERE name LIKE '%hello%'
   OR addr LIKE '%hello%'
   OR state LIKE '%hello%'
   OR name LIKE '%world%'
   OR addr LIKE '%world%'
   OR state LIKE '%world%'



You might want to search online for PHP search engines and see how they do it. As you increase the number of search terms, you'll need some way of tracking the "best" matches. This is usually done by sorting by the number of pages that have ALL terms, then by all pages that have N-1 matches, then N-2, ..., and finishing with 1.
   (where N is the number of terms)



Take care,

Nik
http://www.bigaction.org/
  #5 (permalink)  
Old April 5th, 2004, 07:25 AM
Registered User
 
Join Date: Mar 2004
Location: Ikeja, Lagos, Nigeria.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to keph
Default

Hi Nik,

I've been trying like a week to get this thing to work. I've tried applying the code in different formats to no avail. The best result I get so far is a blank page. Below is the method I used in applying the code you posted earlier.

===================>>
<?
require_once("fns_lib.inc"); // my function library

//============ function to search multiple words ==================

function searchxp()
  {
  global $searchterm; // submitted from a form input field

  // search for the search term

  $searchterm = trim($searchterm);


  //test for empty submissions

    if (!$searchterm)
    {
    echo "No search term entered";
    exit;
    }

  $terms = preg_split("/\s+/", $searchterm);

  $where_clauses = array();

    foreach ($terms as $term)
         {
       $where_clauses[] = "name like '%{$term}%'";
       $where_clauses[] = "addr like '%{$term}%'";
       $where_clauses[] = "state like '%{$term}%'";
     }

  $where_clause = "where" . join ("\n or ", $where_clauses);


    if (!($conn = db_connect()))
        return false;
        $result = mysql_query( "select churchid, name
                                 from list
                '$where_clause' ");

         if (!$result)
             return false;


      // Displays the list

    $num_results = mysql_num_rows($result);

  if ($num_results == 0)
   {
     echo "No record Found";
     exit;
   }

    echo "<h4>[u]$num_results results found for $searchterm</u></h4>";

  for ($i = 0; $i < $num_results; $i++)
    {
    $row = mysql_fetch_array($result);

    echo "<span id=results>";
    echo "<strong>";
    echo htmlspecialchars(stripslashes($row["name"]));
    echo "</strong>";
    $name = $row["name"];
    $churchid = $row["churchid"];
    echo "</span>";
    echo "<br>";

    echo "</p>";

   }

  }
// ===== function ends here =========

  searchxp(); // call to function

?>

============>>

This is my complete code, please let me know where I'm getting it wrong.
Thanks a lot

http://www.kephassolutions.com
  #6 (permalink)  
Old April 6th, 2004, 01:23 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Looks to me like there's no whitespace between "where" and your first where clause.

That is, your query is likely failing because it looks like:

  wherename like '%term%'
or addr like '%term%'
or state like '%term%'


Some simple debugging measures would've allowed you to discover this a week ago. If you output the query before running it to make sure it looked okay, or perhaps output MySQL's error description when $result was false, you would've seen the answer yourself!

Don't be afraid to debug your own code! Use the tools available to you: namely, print the value of variables, and use the error messages at your disposal. Some extremely useful functions:
  http://www.php.net/echo
  http://www.php.net/print_r
  http://www.php.net/var_dump
  http://www.php.net/mysql_error



Take care,

Nik
http://www.bigaction.org/


Similar Threads
Thread Thread Starter Forum Replies Last Post
String Utils to search all words in filenames sbc789 JSP Basics 1 December 7th, 2008 10:39 PM
highlight search words lucian Dreamweaver (all versions) 6 January 21st, 2006 04:30 AM
highlight search words lucian Classic ASP Basics 10 August 27th, 2005 09:39 AM
macro for finding multiple words Lakshmi KS Access VBA 2 January 30th, 2004 02:42 AM
Selecting multiple words in Word 2000 jdsturm98 Beginning VB 6 0 September 3rd, 2003 03:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.