Re: [PHP] Re: Search a flat file.

2002-06-08 Thread Miguel Cruz

If the file is of a reasonable size (not hundreds of kilobytes), then you 
can just slurp it all into an array and foreach through it:

  $matched = 0;
  $myfile = file('/usr/home/crud/myfile');
  foreach ($myfile as $line)
  {
$split_line = explode(':', $line);
if ($split_line[1] == 'bob')
{
  echo 'pGot a match!/p';
  $matched = 1;
  break;
}
  }
  if (!$matched)
echo 'pUsername not found./p';

If it's larger, you're doing it an awful lot, or your server is 
resource-cramped, you'd probably want to fopen() it and loop over fgets() 
so you don't have to read in any more than necessary.

miguel

On Fri, 7 Jun 2002, Tom Ray wrote:
 ok I believe I follow you on that, I will give that a try. But the 
 question I have now is how do I loop that request?
 
 Gaylen Fraley wrote:
 
 Use the explode() function based on the colon.  Then parse the 2nd array
 element for the username that you want.  If matched, display all elements in
 that array row.  Conversely you could use the implode() to reassemble the
 array row.
 
 (Assume $file_row is the row name that you have assigned)
 
 /* your code to loop through the file goes here */
 $your_array = explode(:,$file_row);
 /* remember that arrays start with element 0 */
 if ($your_array[1]) == some name {
 /* routine to display entire row */
 }
 /* continue processing loop */
 
 --
 Gaylen
 PHP KISGB v4.0.5 Guest Book http://www.gaylenandmargie.com/phpwebsite/
 
 Tom Ray [EMAIL PROTECTED] wrote in message
 [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
   
 
 I want to be able to search a flat file line by line looking for data
 such as a username then display all the information in that line. Is
 there some way to search the following format:
 
 record1:username:info:info:info
 record2:username:info:info:info
 record3:username:info:info:info
 
 
 So if someone submits the username in record two for the search I want
 it to stop at record two and then display that information...
 
 any suggestions?
 
 
 
 
 
 
   
 
 
 
 
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: Search a flat file.

2002-06-08 Thread Daniel Grace

Be warned that explode won't work as expected if any of your fields contain
colons, though it doesn't look like it'll be an issue in this sort of case.

Gaylen Fraley [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 Use the explode() function based on the colon.  Then parse the 2nd array
 element for the username that you want.  If matched, display all elements
in
 that array row.  Conversely you could use the implode() to reassemble the
 array row.

[...]

 Tom Ray [EMAIL PROTECTED] wrote in message
 [EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
  I want to be able to search a flat file line by line looking for data
  such as a username then display all the information in that line. Is
  there some way to search the following format:
 
  record1:username:info:info:info
  record2:username:info:info:info
  record3:username:info:info:info
 





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[PHP] Re: Search a flat file.

2002-06-07 Thread Gaylen Fraley

Use the explode() function based on the colon.  Then parse the 2nd array
element for the username that you want.  If matched, display all elements in
that array row.  Conversely you could use the implode() to reassemble the
array row.

(Assume $file_row is the row name that you have assigned)

/* your code to loop through the file goes here */
$your_array = explode(:,$file_row);
/* remember that arrays start with element 0 */
if ($your_array[1]) == some name {
/* routine to display entire row */
}
/* continue processing loop */

--
Gaylen
PHP KISGB v4.0.5 Guest Book http://www.gaylenandmargie.com/phpwebsite/

Tom Ray [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 I want to be able to search a flat file line by line looking for data
 such as a username then display all the information in that line. Is
 there some way to search the following format:

 record1:username:info:info:info
 record2:username:info:info:info
 record3:username:info:info:info


 So if someone submits the username in record two for the search I want
 it to stop at record two and then display that information...

 any suggestions?




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Re: Search a flat file.

2002-06-07 Thread Tom Ray

ok I believe I follow you on that, I will give that a try. But the 
question I have now is how do I loop that request?

Gaylen Fraley wrote:

Use the explode() function based on the colon.  Then parse the 2nd array
element for the username that you want.  If matched, display all elements in
that array row.  Conversely you could use the implode() to reassemble the
array row.

(Assume $file_row is the row name that you have assigned)

/* your code to loop through the file goes here */
$your_array = explode(:,$file_row);
/* remember that arrays start with element 0 */
if ($your_array[1]) == some name {
/* routine to display entire row */
}
/* continue processing loop */

--
Gaylen
PHP KISGB v4.0.5 Guest Book http://www.gaylenandmargie.com/phpwebsite/

Tom Ray [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
  

I want to be able to search a flat file line by line looking for data
such as a username then display all the information in that line. Is
there some way to search the following format:

record1:username:info:info:info
record2:username:info:info:info
record3:username:info:info:info


So if someone submits the username in record two for the search I want
it to stop at record two and then display that information...

any suggestions?






  




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php