Forum Moderators: coopster

Message Too Old, No Replies

search array()

         

clown

1:58 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



Think i'm getting it to work now...

I'll update this post later

clown

3:13 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



well... I just cant get anything to work.. the closest i've been able to come is by using current() and next()... but It only gives respond on the last entry in the array... is there any easier way than this?

<?

$uname = $_REQUEST['username'];
$pass = $_REQUEST['password'];
$string = $uname . "@" . $pass;

$array = file("filename");
$asize = count($array);
$i = 1;
echo "Search string: " . $string . "<br><br>";
while ($i <= $asize) {
$result = current($array);
if ($result == $string) { echo $result . " <=== MATCH!"; }
else { echo $result."<br>"; }
$result = next($array);
$i++;
}

?>

whoisgregg

3:28 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

$uname = $_REQUEST['username']; 
$pass = $_REQUEST['password'];
$string = $uname . "@" . $pass;
$array = file("filename");
$match_found = false;
foreach ($array as $line) {
if ($line == $string) { $match_found = true; break; }
}
if($match_found){
echo 'You are signed in!';
} else {
echo 'Incorrect username or password.';
}

clown

4:11 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



it only prints out "Incorrect username or password." no mather if I all the login info is right. :(

whoisgregg

4:16 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, forgot that the array elements returned by
file() [php.net]
still have the line ending on them. Change this one line:

if ( rtrim($line) == $string) { $match_found = true; break; } 

clown

4:38 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



thank you m8, it works now...

sorry for all the questions guys.. but i'm a real rookie... i've been using php for long time, but only include() just to get links to work... but yesterday i decided to start digging deeper into the world of php... but i'm trying to use google before i post here. but it's hard when you're stuck and have no clue what to look for =)

again guys.. you have been really helpfull sofar =) really happy i found this site.

whoisgregg

4:58 pm on Apr 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad to help clown. :)