View Full Version : find_value() issue
alexjewell
08-30-2006, 07:26 PM
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:
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:
$names = array( 1 => "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?
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?
alexjewell
08-30-2006, 08:27 PM
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:
$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?
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:
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.
alexjewell
08-30-2006, 08:51 PM
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...
blm126
08-30-2006, 08:56 PM
http://us2.php.net/array_search
alexjewell
08-30-2006, 09:21 PM
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:
$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:
$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()"?
blm126
08-31-2006, 01:47 AM
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.
alexjewell
08-31-2006, 07:17 PM
Hey, thanks blm, I'd appreciate it
:)
blm126
08-31-2006, 10:21 PM
Ok, got some time. Here's a function to do what you want(I think)
<?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
alexjewell
08-31-2006, 10:54 PM
Alright, it looks like we're on the right track. However, should we make $search_name global? I'm getting an undefined variable error:
$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?
blm126
09-01-2006, 12:59 AM
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.
<!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>
alexjewell
09-01-2006, 09:33 PM
Thanks A LOT!
Test it out, search for "alex":
http://www.flamehtmlstudios.com/projects/search/search1.html
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.