I have a text file like the one below:
312:Lee:Stan:234525235
415: Doe:John:346347347
719:Smith:John:2362362362
110:Wong:Toby:125125125125
It's basically in this format: "customer id:last name:first name: order id" I'm trying to write a script where a user enters in a customer id and last name from a form and it searches through the text file and if it finds a match for both, it will return that line. Here's what I have so far:
<?php
function getRecord($oneRecordLine)
{
global $cust_id;
global $last_name;
$eachline=explode(":","$oneRecordLine");
if($eachline[0]=="$cust_id" && $eachline[1]=="$last_name")
{
print_r($eachline);
}
else
{
print "not found";
}
}
$cust_id=$_POST['cust_id'];
$last_name=$_POST['last_name'];
$handle = fopen("customers.dat", "r") or die ("Can't open file");
$buffer = fgets($handle);
while (!feof($handle))
{
getRecord($buffer);
$buffer = fgets($handle);
}
?>
It works, but the problem is if I do a search on cust_id = 719 and last_name = Smith, it returns
not found
not found
Array (.....
not found
instead of just
Array (.......
I've been messing around with the loop, but I don't know what's wrong. It should only display "not found" only once if nothing is found in the array.