/    Sign up×
Community /Pin to ProfileBookmark

Help with readfile (PHP)

hi i have the following saved in a fle called “name.txt”.
Mark, 20, Y
James, 21, X
Al, 30, W

I want to make php print the file in the browser in a format like this:
Mark
20
Y

James
21
x

Al
30
W

how do i do this????

this is my php code below please tell me what am i doing wrong:

<?
$namefile = (“name.txt”);
$filepointer = fopen($namefile,”r”);

$myarray = file($namefile);

for ($i=0; $i< count($myarray); $i++)
{

$aline = $myarray[$i];
print $aline . “n”;

}

fclose ($filepointer);

function splitsubroutine($aline, $col)
{
$intoarray = explode(“,”,$aline);
return $intoarray[$i];
}
?>

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelOct 28.2005 — You don't need to use fopen() and fclose() with file().

This is how i would do it:
[code=php]<?php

$lines = file('name.txt');

header('Content-Type: text/plain');

foreach($lines as $line)
{
$parts = explode(',',$line);
foreach($parts as $part)
{
echo trim($part) . "n";
}
echo "n";
}

?>[/code]
Copy linkTweet thisAlerts:
@chickenydauthorOct 28.2005 — hi thanx for the help. i just want to know what this line does.

header('Content-Type: text/plain');
Copy linkTweet thisAlerts:
@ScleppelOct 28.2005 — It makes the output plain text (like a normal .txt file) instead of PHP's default (ususally) of html (.html,.htm).
×

Success!

Help @chickenyd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...