Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: find_value() issue

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

    Default

    Alright, it looks like we're on the right track. However, should we make $search_name global? I'm getting an undefined variable error:

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

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

    //array_strpos    Array    Search Value
    function array_find_value($array,$svalue){
        
    $search_name = array();
        foreach(
    $array as $key=>$value){
            if(
    stristr($value,$svalue) !== FALSE){
                
    $search_name[$key] = $value;
            }
        }
        return 
    $search_name;
    }

    $outcome $search_name or 'Sorry, no matches';
    echo 
    'You searched for: <i>'.$name.'</i>.<br />'.$outcome
    ps - don't we somehow have to incorporate the $name and $names, as well?

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

    Default

    You don't understand what the function I wrote does.I intended for it to just point you in the right direction. I should have given a better explanation than one comment. Here is a more complete example.
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Test Search</title>
    </head>
    <body>
    <?php
    //array_find_value    Array    Search Value
    function array_find_value($array,$svalue){
        
    $return = array();
        if(empty(
    $svalue)){
            return 
    $array;
        }
        foreach(
    $array as $key=>$value){
            if(
    stristr($value,$svalue) !== FALSE){
                
    $return[$key] = $value;
            }
        }
        return 
    $return;
    }
    function 
    search($array,$search){
        
    $find array_find_value($array,$search);
        echo 
    '<p>You searched for "'.$search.'". There were '.count($find).' results.</p>';
        echo 
    ' <form method="get" action="'.$_SERVER['PHP_SELF'].'"><input type="text" name="name"><button type="submit">Search Again</button></form>';
        echo 
    '<ol>';
        foreach(
    $find as $key=>$value){
            echo 
    "<li>".$key."</li>";
        }
        echo 
    '</ol>';
    }

    if(!empty(
    $_REQUEST['name'])){
        
    $name $_REQUEST['name'];
    }
    else{
        
    $name '';
    }
    $names = array( "Alex" => "alex" "Amanda" => "amanda" "John" => "john" "Smith" => "smith" "Jewell" => "alex jewell" );
    search($names,$name);
    ?>
    </body>
    </html>

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

    Default

    Thanks A LOT!

    Test it out, search for "alex":

    http://www.flamehtmlstudios.com/proj...h/search1.html

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
  •