homepage Welcome to WebmasterWorld Guest from 54.235.16.159
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Search for value of a search criteria
ahmed24




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

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




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

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




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

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




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

thanks works perfectly

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved