Reading a tab delimited text file and matching ID

udara22
udara22 used Ask the Experts™
on
Hi Experts,

I have a text file members.txt. In the text file I wrote, Member ID, First Name and Last Name seperated by tabs using PHP fwrite.

Now, I have another php file to search members by MemberID and display their data.

Ive been trying all sorts of ways to read the text file and search by MemberID, still cant figure out how to search the text file and if exists, show the whole row data on the website.

Any ideas?
members.txt
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
post your coding here for fast reply

Author

Commented:
Ok. Now I can look for MemberID in an array and print the values.

How do I display a message if MemberID cannot be found?

 $member_id =  $_GET["membership-no"];
 <?php
  
  $handle	=	fopen("../../data/sportsclub/members.txt","r");
	 
	while ($data = fgetcsv($handle,5000,"\t")) {
 
   		 if($data[0] == $member_id){ 
		 	$MemberFound = true;
		  	echo '<h3>Member Found</h3><br>';
			echo '<table cellpadding="6" cellspacing="0" border="1">';
			echo '<tr><td><strong>Membership Number</strong></td> <td>'. $data[0].'</td></tr>';
			echo '<tr><td><strong>First Name</strong></td> <td>'. $data[1].'</td></tr>';
			echo '<tr><td><strong>Family Name</strong></td> <td>'. $data[2].'</td></tr>';
			echo '<tr><td><strong>Gender</strong></td> <td>'. $data[3].'</td></tr>';
			echo '<tr><td><strong>Membership Type</strong></td> <td>'. $data[4].'</td></tr>';
			echo '<tr><td><strong>Email</strong></td> <td>'. $data[5].'</td></tr>';
			echo '<tr><td><strong>Phone</strong></td> <td>'. $data[6].'</td></tr>';
			echo '<tr><td><strong>Sports Interested</strong></td> <td>'. $data[7].'</td></tr>';
			echo '<tr><td><strong>Special Skills</strong></td> <td>'. $data[8].'</td></tr>';
			echo '</table>';
			
				 ?>
		  <? }
	
		
	}
	 
 
  ?>

Open in new window

you can use else clause to say
 echo "No matches found";
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Most Valuable Expert 2011
Top Expert 2016
Commented:
You should use a data base for this.  It's much easier than trying to write your own lookup programs.

See http://www.laprbass.com/RAY_temp_udara22.php
<?php // RAY_temp_udara22.php
error_reporting(E_ALL);
echo "<pre>";

// WHAT WE WANT TO LOOK FOR (MAYBE COMES FROM AN EXTERNAL FORM INPUT?)
$tgt = '88377';

$url = 'http://filedb.experts-exchange.com/incoming/2012/04_w15/568035/members.txt';
$str = file_get_contents($url);

// GET INDIVIDUAL LINES
$arr = explode(PHP_EOL, $str);

// TEST EACH LINE
$cnt = 0;
foreach ($arr as $txt)
{
    // IF THE MATCHING MEMBER ID IS PRESENT IT IS IN POSITION ZERO OF THE LINE
    if (strpos($txt, $tgt) === 0)
    {
        echo PHP_EOL . $txt;
        $cnt++;
    }
}

// REPORT IF ANY MATCHES?
if ($cnt)
{
    echo PHP_EOL . "FOUND $cnt MATCHING LINES";
}
else
{
    echo PHP_EOL . "FOUND NO MATCHING LINES";
}

Open in new window

Author

Commented:
Thanks guys,

@Ray_Paseur : This is actually a uni assignment I did for my mate that is due today morning. But anyway, I did what I can and gave him the files. So thanks guys for the help.

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for udara22's comment #37849564

for the following reason:

Havent solved yet
Most Valuable Expert 2011
Top Expert 2016

Commented:
A tested and working example was posted at ID: 37846559

Author

Commented:
@southmod : thanks. First of all, of course the question I asked is not the uni assignment. It's  lessthan 1/10th of the whole assignment. The assignment consists of UI,UX, writing to text file, reading, putting them in to an array, searching, updating existing data and many more. I asked how to put read the data and put it into an array because I just wanted to get some help. EE experts say they don't feed you with the fish but they teach you how to fish. That's exactly what I was after. Sorry if you misunderstood what I said.

So you recon I should close this question as i didn't receive a proper answer?
Most Valuable Expert 2011
Top Expert 2016

Commented:
If you had asked how to read data and put it into an array, we could have answered that part of the question.
http://php.net/manual/en/function.file.php

Author

Commented:
@ray_pasuer : thanks ray. Looks like I made the whole thing complicated. Well we learn everyday. Thanks for the link.
Most Valuable Expert 2011
Top Expert 2016

Commented:
@udata22: Why did you mark this down to a "B" -- was there something wrong?  I answered the question to the best of my ability given that there was not much direction about what you really needed to know.  Going forward, please consider following the EE Grading Guidelines.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26

Author

Commented:
@Ray_Paseur : Sorry. I didnt think too much about the grade. Is there anyway I can change it to Grade A now?

Author

Commented:
Thanks modus_operandi

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial