Reading and listing a Comma Delimiting file

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
Lizbeth
Forum Newbie
Posts: 3
Joined: Mon Feb 20, 2012 3:43 pm

Reading and listing a Comma Delimiting file

Post by Lizbeth »

Hi, i have a comma delimiting file house.txt (below)that i would like to display via the "commas" so that it lists like this:
WD68AA
400
file.jpg
2
The plist.php file at the moment reads in house.txt and displays the file as it is below.The getValue.php is a function that i would like to use to display the txt file as it is above....im stuck on as to how to put them together to do this??
So that plist.php reads the txt file and getValue.php sorts the file using the explode() function?

Thankyou in advance,

[text]
#house.txt
WD68AA,400,file1.jpg,2
SL29DD,350,house8.jpg,4
WM34SA,180,flat2.jpg,5
SK22MW,220,house5.jpg,3
KS23RR,260,flat6.jpg,2
[/text]

#Plist.php

Code: Select all

<?
 $filename = "house.txt";
 $filepointer = fopen($filename,"r");  // open for read
 $myarray = file ($filename);
 for ($mycount = 0; $mycount < count($myarray); $mycount++ )
    {		
     // one input line at a time
       $aline = $myarray[$mycount]; 
        print $aline ."\n <br>"; 
     } 
  fclose ($filepointer);

#if (!($filepointer = fopen($filename,"r"))){ exit; 
}
?>
#getValue.php

Code: Select all

<?
$details = "hello,aa,bb,cc,dd,ee,there"; 
 $partdetails  =  &getvalue($details,3);
 print " $details then $partdetails ";
 exit;    #finish here - so it does not run into function accidentally

  function getvalue ($text, $commaToLookFor)
  {   
       $intoarray = explode(",",$text);   
       return  $intoarray[ $commaToLookFor];  
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Reading and listing a Comma Delimiting file

Post by Celauran »

Is this the kind of output you're looking for?

Code: Select all

$handle = fopen('house.txt', 'r');
while (!feof($handle))
{
    $line = explode(',', fgets($handle));
    foreach ($line as $item)
    {
        print "{$item}<br />";
    }
    print "<br />";
}
fclose($handle);
Lizbeth
Forum Newbie
Posts: 3
Joined: Mon Feb 20, 2012 3:43 pm

Re: Reading and listing a Comma Delimiting file

Post by Lizbeth »

Hi thankyou for this,but it is a little more complicated than i am looking for as the criteria is to just use a for loop and a function, but thankyou anyway,
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Reading and listing a Comma Delimiting file

Post by Eric! »

Are you talking about fgetcsv and fputcsv? See the examples in the links.
Lizbeth
Forum Newbie
Posts: 3
Joined: Mon Feb 20, 2012 3:43 pm

Re: Reading and listing a Comma Delimiting file

Post by Lizbeth »

No just using the explode() function to seperate the strings at the commas and list them one by one,one after the other.

Something like this:
<?
$filename = "house.txt"; //Text file to read
$filepointer = fopen($filename,"r"); // open for read
$myarray = file ($filename); //store the file in an array

for ($mycount = 0; $mycount < count($myarray);$mycount++ ) //loop through the array
{ // one input line at a time
$aline = $myarray[$mycount]; // the entire file contents "should" be in $aline
$aline = explode(",",$filename); // The contents are passed to explode and seperated into individual strings
print $aline ."\n <br>"; //However when it gets to printing out the $aline it just prints out Array 5 times one after the other in a list.
} // So something is missing as its not picking up the file contents

fclose ($filepointer);
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Reading and listing a Comma Delimiting file

Post by Eric! »

What didn't you like about Celauran's post? Just make a function out of it.

Your main problem is how you're handling the printing of the exploded data because it is an array not a string see explode(). Try printing it as Celauran did...

Code: Select all

foreach ($aline as $item)
    {
        print "{$item}<br />";
    }
    print "<br />";
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: Reading and listing a Comma Delimiting file

Post by litebearer »

Perhaps...

Code: Select all

<?
$filename = "house.txt";
$lines = file ($filename);
for ($i=0; $i < count($lines); $i++ ) { 
	$data = explode(",", $lines[$i]);
	echo $data[0] . "<br />" . $data[1] . "<br />" .$data[2] . "<br />" .$data[3] . "<br /><hr>";
}
?>
Post Reply