Welcome to WebmasterWorld Guest from 54.144.79.200

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Search for value of a search criteria

     

ahmed24

7:04 pm on Sep 10, 2009 (gmt 0)

5+ Year Member



I have a text file that's content is loaded into a variable and it looks like this:

212=RESULT1,213=RESULT2,214=RESULT4,

The numbers are what I want to be able to search and get result. so basically if i want to search for 212 i need to find everything after 212= and before the comma.

can anyone tell me how i can do this ?

thanks

andrewsmd

9:25 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Try this
<html>
<body>
<form name="form" method="post">
Search: &nbsp; <input type="text" name="input" /><br>
<input type="submit" name="submit" value="submit" />
</form>

<?php

//if they click the submit button
if(isset($_POST['submit'])){

//a boolean for no results returned
$noResult = true;

//the input from the user
$input = $_POST['input'];

//the data in the format you gave
$data = "1=RESULT1,13=RESULT2,214=RESULT4,215=RESULT4,216=RESULT4,216=RESULT4,217=RESULT4";

//split the array based on a comma
$tempArr = split(",", $data);

//now go through the array and search for the number
foreach($tempArr as $i){

//the position of the =
$tempPos = stripos($i, "=");
$tempNum = substr($i, 0, $tempPos);
$tempData = substr($i, $tempPos + 1);

if($input == $tempNum){
echo("Your result is $tempData");
$noResult = false;
break;
}//if

}//foreach

if($noResult){

echo("Sorry, your search did not return any results.");

}//if noresult

}//if isset

?>
</body>
</html>

andrewsmd

9:33 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Note: the above code will return true if a user enters 0214 for the value 214 because it is checking their numerical values. If you want to check every digit exactly then convert them to strings i.e.
if($input === $tempNum){ //added and extra =

ahmed24

10:08 pm on Sep 10, 2009 (gmt 0)

5+ Year Member



thanks works perfectly
 

Featured Threads

Hot Threads This Week

Hot Threads This Month