Page 1 of 1

Help finalizing this code.

Posted: Wed Dec 22, 2004 11:21 am
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

Posted: Wed Dec 22, 2004 11:46 am
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

Fantastic Ken

Posted: Wed Dec 22, 2004 11:51 am
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

Posted: Wed Dec 22, 2004 12:00 pm
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

Posted: Wed Dec 22, 2004 5:47 pm
by timvw
another option would have been [php_man]fgetcsv[/php_man]