Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: find_value() issue

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default find_value() issue

    I was just introduced to find_value. I decided I'd play around with it and see if it worked.
    Well...
    It didn't.
    I have two pages, search1.html and search2.php. The html page just submits a search for a name. All the names someone could search for are found in an array called $names.
    find_value() sees if that name is there.
    I'm getting this error:
    Code:
    Fatal error: Call to undefined function: find_value() in d:\webspace\ADMIN\flamehtmlstudios.com\WWW\projects\search\search2.php on line 36
    Here's my code, tell me what's wrong:

    PHP Code:
    $names = array( => "alex" "amanda" "john" "smith" "jewell" );

    $name $_REQUEST['name'];

    $name ereg_replace("A""a"$name);
    $name ereg_replace("B""b"$name);
    $name ereg_replace("C""c"$name);
    $name ereg_replace("D""d"$name);
    $name ereg_replace("E""e"$name);
    $name ereg_replace("F""f"$name);
    $name ereg_replace("G""g"$name);
    $name ereg_replace("H""h"$name);
    $name ereg_replace("I""i"$name);
    $name ereg_replace("J""j"$name);
    $name ereg_replace("K""k"$name);
    $name ereg_replace("L""l"$name);
    $name ereg_replace("M""m"$name);
    $name ereg_replace("N""n"$name);
    $name ereg_replace("O""o"$name);
    $name ereg_replace("P""p"$name);
    $name ereg_replace("Q""q"$name);
    $name ereg_replace("R""r"$name);
    $name ereg_replace("S""s"$name);
    $name ereg_replace("T""t"$name);
    $name ereg_replace("U""u"$name);
    $name ereg_replace("V""v"$name);
    $name ereg_replace("W""w"$name);
    $name ereg_replace("X""x"$name);
    $name ereg_replace("Y""y"$name);
    $name ereg_replace("Z""z"$name);

    $search_name find_value($names$name);

    if ( !
    $search_name) { $outcome "Sorry, no matches"; }
    else { 
    $outcome $search_name; }

    echo 
    "You searched for: <i>".$name."</i>.<br />".$search_name
    Obviously, the ereg_replace just changes the search into lowercase. Is there an easier way to do that, too?

    Help?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Argh!
    $names = array( 1 => "alex" , "amanda" , "john" , "smith" , "jewell" );

    $name = strtolower($_REQUEST['name']);
    $search_name = find_value($names, $name);
    $outcome = $search_name or 'Sorry, no matches';
    echo 'You searched for: <i>'.$name.'</i>.<br />'.$search_name;
    I can't help you with find_value(), though, because I'm not sure what you want it to do. You seem to expect it to return a string?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Alright, well...I was thinking I could use it as a search tool. Say, someone searches for alex.
    Then, it outputs:

    1. alex

    or something of that nature.

    Eventually, I'm thinking about using it on my blog for people to search on the site and find matches of the blogs with those keywords in them.

    I mean, find_value() takes what's searched for and looks for it in the array, right? So if I put in inside a variable, wont that work?

    ps:

    Code:
    $outcome = $search_name or 'Sorry, no matches';
    How does THAT work? Is that really valid, with the "or", and PHP knows 'Sorry, no matches' is the message to use when no results is the case?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I mean, find_value() takes what's searched for and looks for it in the array, right?
    find_value() doesn't exist I'm still not entirely sure what you expect it to do.
    How does THAT work? Is that really valid, with the "or", and PHP knows 'Sorry, no matches' is the message to use when no results is the case?
    You can chain expressions together:
    Code:
    doOneThing() or doAnotherThing() or doAThirdThing() or die("Couldn't do anything!");
    ... and PHP will execute one after the other until it finds one that returns true, and use that return value as the final result of the whole chain.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Ok, well, here's what I want it to do:

    I want it to take what was searched for, see if it's in the array $names, and if it is, output it.

    for example, if someone searches for "alex"...

    then, it finds "alex" in the array, so it outputs:

    1. alex

    eventually, say someone searches for "php" on my blog...

    well, then in the array I'll have all my posts...and the posts with php will come up:

    1. Feedback Forms with PHP
    2. Comments with PHP
    3. PHP vs. ASP

    etc...

  6. #6
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Thanks a ton! It's working now. However, say I search for "alex", only "alex" comes up, not "alex jewell" because it must see it as the full thing and not seperate words, yes?

    I mean:

    PHP Code:
    $names = array( "1. Alex" => "alex" "2. Amanda" => "amanda" "3. John" => "john" 
                    
    "4. Smith" => "smith" "5. Jewell" => "alex jewell" ); 
    So, then when "alex" is searched for, only 1. Alex comes up, and not: 1. Alex then 5. Jewell. Know what I'm saying?

    Anyway around this?

    Here's my code:

    PHP Code:
    $names = array( "1. Alex" => "alex" "2. Amanda" => "amanda" "3. John" => "john" 
                    
    "4. Smith" => "smith" "5. Jewell" => "alex jewell" );

    $name strtolower($_REQUEST['name']);

    $search_name array_search($name$names);

    $outcome $search_name or 'Sorry, no matches';
    echo 
    'You searched for: <i>'.$name.'</i>.<br />'.$search_name
    ps - could this be done with "array_keys()"?
    Last edited by alexjewell; 08-30-2006 at 09:29 PM.

  8. #8
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Array_keys only helps if you need( or anticipate) more that one result. For this situation you probably need to write a function to loop through each key with astrpos(or similar) test. I don't have time tonight,but I'll look in to this tomorrow if you would like.

  9. #9
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Hey, thanks blm, I'd appreciate it

  10. #10
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Ok, got some time. Here's a function to do what you want(I think)
    PHP Code:
    <?php
    //array_strpos    Array    Search Value
    function array_find_value($array,$svalue){
        
    $return = array();
        foreach(
    $array as $key=>$value){
            if(
    stristr($value,$svalue) !== FALSE){
                
    $return[$key] = $value;
            }
        }
        return 
    $return;
    }
    ?>
    Be careful the function is case-insensitive

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
  •