Professional Web Applications Themes

Help searching through an array - PHP Development

hello all, this is continued from a previous post. i am still stuck, and really could use some help. basically, via a form, search words are submitted, i.e. "apple". it then searches through the contents of the index_file and sees if a match is found. if you searched for "apple orange", then a match for index1.html (listed below) would be returned. however, if you search for "apple pie", NO match is returned. even though, "apple" and "pie" are in the file, they are just not on the same line. can someone please look at the function and see if it ...

  1. #1

    Default Help searching through an array

    hello all,
    this is continued from a previous post. i am still stuck, and really could
    use some help.

    basically, via a form, search words are submitted, i.e. "apple". it then
    searches through
    the contents of the index_file and sees if a match is found.
    if you searched for "apple orange", then a match for index1.html (listed
    below) would be
    returned.

    however, if you search for "apple pie", NO match is returned. even though,
    "apple" and "pie"
    are in the file, they are just not on the same line.

    can someone please look at the function and see if it is possible to make
    searches return matches
    if two or more words are entered, and they exist on different lines ?

    the contents of $GLOBALS[index_file]:
    [url]http://127.0.0.1/index1.html|apple|orange|banana|[/url]
    [url]http://127.0.0.1/index2.html|pie|[/url]

    we'll hardcode the variable:
    $query = "apple pie";

    the function:
    function s_search($query) {
    $query = trim(strtolower(c_strip_chars($query)));
    $search_data = file($GLOBALS[index_file]) or die("<h4
    align=\"center\">$GLOBALS[err_no_search_db]</h4>");
    $pages_found = " ";
    foreach ($search_data as $search_page) {
    $page_arr = explode("|", $search_page);
    $found_count = 0;
    $qry_array = split('[, ]+',trim(strtolower($query)));
    foreach ($qry_array as $qry) {
    if (in_array($qry, $page_arr)) {
    ++$found_count;
    $pages_found .= $page_arr[0] . " ";
    }
    }
    if ($found_count == count($qry_array)) $result_arr[] = $page_arr[0];
    }
    return $result_arr;
    }

    many thanks all.


    fartsniff Guest

  2. #2

    Default Re: Help searching through an array


    "sniff" <sniff.com> wrote in message
    news:vh6509rd1gtr6dcorp.supernews.com...
    > hello all,
    > this is continued from a previous post. i am still stuck, and really could
    > use some help.
    >
    > basically, via a form, search words are submitted, i.e. "apple". it then
    > searches through
    > the contents of the index_file and sees if a match is found.
    > if you searched for "apple orange", then a match for index1.html (listed
    > below) would be
    > returned.
    >
    > however, if you search for "apple pie", NO match is returned. even though,
    > "apple" and "pie"
    > are in the file, they are just not on the same line.
    >
    > can someone please look at the function and see if it is possible to make
    > searches return matches
    > if two or more words are entered, and they exist on different lines ?
    >
    > the contents of $GLOBALS[index_file]:
    > [url]http://127.0.0.1/index1.html|apple|orange|banana|[/url]
    > [url]http://127.0.0.1/index2.html|pie|[/url]
    >
    > we'll hardcode the variable:
    > $query = "apple pie";
    >
    > the function:
    > function s_search($query) {
    > $query = trim(strtolower(c_strip_chars($query)));
    > $search_data = file($GLOBALS[index_file]) or die("<h4
    > align=\"center\">$GLOBALS[err_no_search_db]</h4>");
    > $pages_found = " ";
    > foreach ($search_data as $search_page) {
    > $page_arr = explode("|", $search_page);
    > $found_count = 0;
    > $qry_array = split('[, ]+',trim(strtolower($query)));
    > foreach ($qry_array as $qry) {
    > if (in_array($qry, $page_arr)) {
    > ++$found_count;
    > $pages_found .= $page_arr[0] . " ";
    > }
    > }
    > if ($found_count == count($qry_array)) $result_arr[] = $page_arr[0];
    > }
    > return $result_arr;
    > }
    >
    > many thanks all.
    >
    >
    Could you clarify that what you want to do is perform a 'AND' search - thus,
    if the user enters APPLE and ORANGE, and both these are in the one line,
    then your query should return true with something... however if APPLE and
    PIE is entered, and only APPLE but not PIE is on a single line, then it
    should return FALSE...

    Am I correct with that belief?

    If so I think you might need something like preg_match with a regular
    expression... Can you paste two or three lines of index_file so I can get a
    look at how you have it pd? ie are its values comma or space seperated?


    Randell D. Guest

  3. #3

    Default Re: Help searching through an array

    > Could you clarify that what you want to do is perform a 'AND' search -
    thus,
    > if the user enters APPLE and ORANGE, and both these are in the one line,
    > then your query should return true with something... however if APPLE and
    > PIE is entered, and only APPLE but not PIE is on a single line, then it
    > should return FALSE...
    >
    > Am I correct with that belief?
    >
    > If so I think you might need something like preg_match with a regular
    > expression... Can you paste two or three lines of index_file so I can get
    a
    > look at how you have it pd? ie are its values comma or space
    seperated?
    >
    actually, if someone enters in APPLE AND ORANGE then it will return a
    "match"
    which displays index1.html, but if someone entered in APPLE AND PIE then it
    would return 2 "matches", displaying both index1.html and index2.html

    i understand what James says below about "exploding" the query into words,
    but
    am not sure how to completely implement it.

    trust me folks, i am trying to get this to work.


    fartsniff Guest

Similar Threads

  1. Replies: 5
    Last Post: September 17th, 07:54 PM
  2. array data matches but array created in loop doesn't work
    By Reed Law in forum PHP Development
    Replies: 1
    Last Post: August 13th, 04:25 AM
  3. #24897 [Com]: array_multisort() will reindex the array but not if array length is 1
    By franklin_se at hotmail dot com in forum PHP Development
    Replies: 0
    Last Post: August 2nd, 03:29 PM
  4. Replies: 0
    Last Post: August 1st, 12:02 PM
  5. Accessing elements in array ref of array references
    By fatted in forum PERL Miscellaneous
    Replies: 3
    Last Post: July 24th, 08:58 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139