SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Needle in haystack - exact match

    I've spent hours on this and it's doing my nut in

    I've been trying out various needle in haystack flows but I can't get an exact match for the term.

    For instance, the user inputs a website name - I then bring out an array to check whether it already exists.

    Now if that website is called "The London Newspaper" I want to get an exact match for the entire term but at the moment I'm triggering a positive on any one of those words

    I'm sure it's nothing too difficult...
    Let everyday be Christmas

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    $a[]= 'The London Newspaper';
    $a[]= 'The Leeds Newspaper';

    $b 'The';

    if( 
    in_array($b $a ) )
    echo 
    'found'
    How does that not do what you mean?

    // gives nothing, no match

  3. #3
    SitePoint Enthusiast Wuiqed's Avatar
    Join Date
    Dec 2006
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you wanted to achieve?

    Code PHP:
    $haystack = array(
    	'The London Newspaper',
    	'The Leeds Newspaper',
    );
     
    $needle = 'The London Newspaper';
     
    foreach ($haystack as $item) {
    	if ($item == $needle) {
    		// proceed...
    	}
    }

  4. #4
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's something obvious I'm missing here.

    I'm calling the query from the database and putting it into a loop

    while($final_result = mysql_fetch_array($result) {

    // so this would list all websites

    echo $final_result[0];

    }

    No I'm getting confused about how this array would fit into a flow as above
    Let everyday be Christmas

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Divisive Cotton View Post
    There's something obvious I'm missing here.

    I'm calling the query from the database and putting it into a loop

    while($final_result = mysql_fetch_array($result) {

    // so this would list all websites

    echo $final_result[0];

    }

    No I'm getting confused about how this array would fit into a flow as above
    *untested* as I dont use mysql_* style connections
    PHP Code:

    $b 
    'The London Paper';

    $result mysql_query("SELECT id, paper FROM mytable");

    while (
    $row mysql_fetch_array($resultMYSQL_BOTH)) {

      if( 
    $b === $row['paper'] )
      {
       echo 
    $b ' was found in mytable' ;
       continue;
      }else{
       echo 
    $b ' was NOT found in mytable' ;
      }


    OR
    PHP Code:

    $b 
    'The London Paper';

    $result mysql_query("SELECT id, paper FROM mytable");

    $papers mysql_fetch_array($resultMYSQL_BOTH)) ;

    if ( 
    in_array$b$papers['paper'] ){
    echo 
    $b ' was found in mytable' ;


    But why not just search the table?

    $qry = "select * from my table where paper = '$b'";
    Last edited by Cups; Jun 20, 2009 at 01:26. Reason: renamed var to papers


Bookmarks

Posting Permissions

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