Help finalizing this code.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
remasters
Forum Newbie
Posts: 6
Joined: Wed Dec 22, 2004 11:07 am

Help finalizing this code.

Post by remasters »

Can someone help me with this I am new to php but trying to get a simple email system set up. I don't have many addresses so I am using a text file to store the information.

My text file is in the following format.

Firstname|Lastname|Email|Account|Accountid
Randy|Jackson|rjackson@yahoo.com|active|12345
Jim|Howard|jimhow@msn.com|active|12346

I need to extract the fields individually starting with line 1 not 0.

Here is what I have so far but I cannot seem to get the information by looping properly.

<?php
$SEP="|";
$dbfile = "mytextfile.txt";


$contents = file($dbfile);
//Take out the line feeds at the ends of the array
for ($i=0;$i<count($contents);$i++) {$contents[$i] = trim($contents[$i]);}

for($i=1;$i<count($contents);$i++){
if(eregi("^".$SEP,$contents[$i])){$loc = $i;
$tmp = explode($SEP,$contents[$loc]);
echo $tmp[$i]."<br>";
echo $i."<br>";
}
}

?>

I am just outputing the information to the screen right now for testing. But what it is doing is giving me one piece of information on each line in the file.

The output should be as such

Randy
Jackson
rjackson@yahoo.com
active
12345

Jim
Howard
jimhow@msn.com
active
12346

Can someone help me finish this up. I have spent a couple of days on this and seem to just be running around in circles.

Any and all help is greatly appreciated.
Thanks,
remasters
kenrbnsn
Forum Newbie
Posts: 13
Joined: Tue Jul 01, 2003 3:34 pm
Location: Hillsborough, NJ USA

Post by kenrbnsn »

You are not looping through your exploded array. Try the following modification:

Code: Select all

<?php
$SEP='|'; 
$dbfile = 'mytextfile.txt'; 
$contents = file($dbfile); 
for ($i=1;$i<count($contents);$i++)
      echo implode("<br>\n",explode($SEP,trim($contents[$i]))).'<br>'.$i."<br><br>\n"; 
?>
I tightened up the code, removed your second loop, started the first loop from 1 instead of 0.

Ken
remasters
Forum Newbie
Posts: 6
Joined: Wed Dec 22, 2004 11:07 am

Fantastic Ken

Post by remasters »

That worked great. Thank you so much. You the man.

Wish I would have asked sooner, but hey, you can't learn anything, if you just ask the question without first trying.

Again, thank you so much,

Remasters
kenrbnsn
Forum Newbie
Posts: 13
Joined: Tue Jul 01, 2003 3:34 pm
Location: Hillsborough, NJ USA

Post by kenrbnsn »

No problem. All of use were new coders once, so most of us are happy to help. I've been reading many of the PHP forums for many years and have just started to reply to people asking for help.

I suggest you read as many examples as possible, and ask when you don't understand or can't figure something out.

I'm still learning ways of improving my code by reading the forums.

Good luck with your projects.

Ken
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

another option would have been [php_man]fgetcsv[/php_man]
Post Reply