Google Answers Logo
View Question
 
Q: PHP ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: PHP
Category: Computers > Programming
Asked by: swykpisz-ga
List Price: $7.50
Posted: 17 Apr 2003 13:53 PDT
Expires: 17 May 2003 13:53 PDT
Question ID: 191936
Is there a way to query a text file using php? If so how? the format
of the file is as follows:
number|	Name|		Time
990999	Jones,John	122.85
123456	Jim Doe		1.25   

I want to be able to enter 990999 and return Jones,John	122.85. Also
this file may contain up to 1000 lines.

Is this possible without using SQL or MySQL?
Answer  
Subject: Re: PHP
Answered By: sycophant-ga on 17 Apr 2003 14:52 PDT
Rated:5 out of 5 stars
 
This is quite possible in PHP, although not as efficent as it would be
with an SQL database.

You can do so like this:

$file = "data.txt";
$lines = file($file);
$out = array();
foreach ($lines as $i) {
  list($number,$name,$time) = split("\t",$i);
  $out[$number] = array("name"=>$name,"time"=>$time);
}

I have assumed your delimiter in the textfile is a tab as it wasn't
too clear and that's what it seemed to be.

The code takes the file (defined in $file) puts it in an array, one
line per element, then goes though that array and breaks the line up,
and stores the content in a new array, indexed by the number.

What you will end up with is an array, indexed by the number, with
contains an array of name and time. You could use it like so:

$person = $out[$number];
echo "Name: ".$person["name"]." - Time: ".$person["time"];

The key functions are:
<a href="http://www.php.net/manual/en/function.file.php">file()</a>
<a href="http://www.php.net/manual/en/function.split.php">split()</a>
<a href="http://www.php.net/manual/en/control-structures.foreach.php">foreach()</a>

I haven't tried running this code specifically but have written code
like it many times. Play with it, it should do exactly what you want.

If you have trouble, let me know, with a little details about what
does or does not work and I will see what I can do.

Regards,
sycophant-ga

Request for Answer Clarification by swykpisz-ga on 17 Apr 2003 15:35 PDT
I'm getting this error:
Warning: Undefined offset: 2 in c:\program files\apache
group\apache\htdocs\grep.php on line 6

Warning: Undefined offset: 1 in c:\program files\apache
group\apache\htdocs\grep.php on line 6

Warning: Undefined offset: 2 in c:\program files\apache
group\apache\htdocs\grep.php on line 6

Warning: Undefined offset: 1 in c:\program files\apache
group\apache\htdocs\grep.php on line 6

Warning: Undefined offset: 2 in c:\program files\apache
group\apache\htdocs\grep.php on line 6

Warning: Undefined offset: 1 in c:\program files\apache
group\apache\htdocs\grep.php on line 6
Name: - Time: 

What would the variable name be that I would recieve individual
results not the whole file?

Request for Answer Clarification by swykpisz-ga on 17 Apr 2003 15:45 PDT
So I'm using this as so:
<?
$file = "data.txt";
$lines = file($file);
$out = array();
$number = "990999";
foreach ($lines as $i) {
  list($number,$name,$time) = split("\t",$i);
  $out[$number] = array("name"=>$name,"time"=>$time);
}

$person = $out[$number];
echo "Name: ".$person["name"]." - Time: ".$person["time"];



?>

But it seems to list the last name in the file not the number's
information I'm looking for,is there a way to make this work to pull
the information from the file that pertains to the number?

Clarification of Answer by sycophant-ga on 18 Apr 2003 04:27 PDT
Is the first problem you described resolved? If so, I will work with
what you have described in the second one:

<? 
function getData() {
  $file = "data.txt"; 
  $lines = file($file); 
  $out = array(); 

  foreach ($lines as $i) { 
    list($number,$name,$time) = split("\t",$i); 
    if (is_numeric($number)) {
      $out[$number] = array("name"=>$name,"time"=>$time); 
    }
  } 
  return $out;
}

if (!empty($number)) {
  $data = getData();
  $person = $data["number"];
  if (!is_array($person)) {
    echo "No entry found";
  } else {
    echo "Name: ".$person["name"]." - Time: ".$person["time"];
  }
} else {
  ?>
<form action="<? echo $PHP_SELF; ?>" method="get">
Number: <input type="text" name="number"><br>
<input type="submit" value="Go">
</form>
  <?
}

?> 
 
 
What this is now is two parts - the first part is a function. In
addition to the code I provided earlier, it checks to make sure the
number is actually a number (a basic check to ensure invalid data
lines are not included). In then populates an array with the details
and returns that array as the result.

The second part is basically within the if statement. It will check to
see if a number to search on is defined (ie. has been passed to the
script by the submission of a form). If a number is there, it will
check the data and display the details, or an error if no data is
found. If no number has been defined to search on, then it will
display a very basic search form.

If this is still not working for you then I may have misinterpreted
the datafile layout in your original post. If that is the case, could
you let me know where I can see the raw data, or a sample of a few
lines of it? It may be that the script need to be changed or the data
needs some slight alteration to work effectively.

Regards,
sycophant-ga

Request for Answer Clarification by swykpisz-ga on 18 Apr 2003 15:05 PDT
I really appreciate your patience and knowledge in this request,I
receive this error from the code I receive last:
Warning: Undefined index: number in c:\program files\apache
group\apache\htdocs\newgrep.php on line 18
No entry found 

Am I copying something wrong? I put a copy of the file I'm trying to
query at: http://home.earthlink.net/~wykpisz/data.txt

It will eventually be at 1000 lines. Again I appreciate your patience
with this

Clarification of Answer by sycophant-ga on 18 Apr 2003 17:06 PDT
Doh, the main problem was a simple typo. There were a few other things
fouling up the works too.

Here is my revised code:

<?

function getmicrotime(){
  list($usec, $sec) = explode(" ",microtime());
  return ((float)$usec + (float)$sec);
}

function getData() {
  $start = getmicrotime();
  $file = "data.txt";
  $lines = file($file);
  $out = array();
  foreach ($lines as $i) {
    list($number,$name,$time) = split("\t",$i);
    $number = rtrim($number);
    $name = rtrim($name);
    $time = rtrim($time);
    if (is_numeric($number)) {
      $out[$number] = array("name"=>$name,"time"=>$time);
    }
  }
  $end = getmicrotime();
  $count = count($out);
  $out["exectime"] = $end - $start;
  return $out;
}

if (!empty($number)) {
  $data = getData();
  if (isset($data[$number])) {
    $person = $data[$number];
    echo "Name: ".$person["name"]." - Time: ".$person["time"];
  } else {
    echo "No entry found";
  }
  echo "<br>Indexed ".(count($data) -1)." elements in
".$data["exectime"]." secs";
} else {
  ?>
<form action="<? echo $PHP_SELF; ?>" method="get">
Number: <input type="text" name="number"><br>
<input type="submit" value="Go">
</form>
  <?
}

?>


Right, here are the changes:
1) Fixed typo in the display section - was $data["number"] - is now
$data[$number]
2) Added element trimming - the three rtrim() commands - this was
causing the matches to fail sometimes.
3) Changed the if statement that test for a record, to avoid a PHP
warning.
4) Added timing - for reference, my server running Debian Linux on a
P3-633 indexed 1003 lines in 0.06 of a second.

You will probably want to change the display section and the form.

Also, you should put proper HTML opening tags at the beginning of the
file, before the <? and HTML closing tags at the end of the file after
the last ?>.

I am not sure of your current experience with PHP, but you may want to
take the code I have given you and make it a seperate function that
you can call from another page - helps keep your code apart from your
page design and makes things a little easier down the line.

There are a lot of good PHP resources to be found at the following
places:
<a href="http://www.hotscripts.com/PHP/Tips_and_Tutorials/">HotScripts.com</a>
<a href="http://php.resourceindex.com/Documentation/">ResourceIndex.com</a>

Hope this works for you.

Regards,
sycophant-ga

Request for Answer Clarification by swykpisz-ga on 22 Apr 2003 11:57 PDT
Sorry I have one more question,how would I output data if I had
several instances of the same number? Meaning if I had 5 entries of
the same number,how would I be able to list all 5 entries,thanks in
advance.

Clarification of Answer by sycophant-ga on 23 Apr 2003 04:01 PDT
Hi,

The way I have written the script, that wouldn't work. The reason
being that I assume the number is unique, and use it as the array key
- so that any lines with the same number in the file, would overwrite
any earlier entries with the same numbers.

Here is a revised script:

<?  

function searchData($search) {
	$file = "data2.txt";
	$lines = file($file);
	$out = array();
	foreach ($lines as $i) {
		list($number,$name,$time) = split("\t",$i);
		$number = rtrim($number);
		$name = rtrim($name);
		$time = rtrim($time);
		if ($number == $search) {
			array_push($out,array("number"=>$number,"name"=>$name,"time"=>$time));
		}
	}
	return $out;
}

if (!empty($number)) {
	$data = searchData($number);
	if (count($data) > 0) {
		foreach ($data as $entry) {
			echo "Number: ".$entry["number"]." - Name: ".$entry["name"]." -
Time: ".$entry["time"]."<br>\n";
		}
	} else {
		echo "No entries found";
	}
} else {
  ?> 
<form action="<? echo $PHP_SELF; ?>" method="get"> 
Number: <input type="text" name="number"><br> 
<input type="submit" value="Go"> 
</form> 
  <? 
}
 
?>  
  


This works a little different, possibly a little better - basically, I
check every line I pull out of the file against the number we are
searching for. If I find that number, I add the data from that line to
the array. Then, I go through every element in the array and display
the details.

It will only match the exact number, so "1002" will only match 1002,
not 10021 and 21002.

If you would like to be able to a partial match, that could be done
with a relatively simple change too.

Hope that does it for you.

Regards,
sycophant-ga
swykpisz-ga rated this answer:5 out of 5 stars
Thank you for your patience and your knowledge in this matter. To
quote someone your helped before,you should be have the title of
genius

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy