Page 1 of 1

Reading and listing a Comma Delimiting file

Posted: Mon Feb 20, 2012 4:15 pm
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];  
}
?>

Re: Reading and listing a Comma Delimiting file

Posted: Mon Feb 20, 2012 7:34 pm
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);

Re: Reading and listing a Comma Delimiting file

Posted: Tue Feb 21, 2012 6:10 am
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,

Re: Reading and listing a Comma Delimiting file

Posted: Tue Feb 21, 2012 7:47 am
by Eric!
Are you talking about fgetcsv and fputcsv? See the examples in the links.

Re: Reading and listing a Comma Delimiting file

Posted: Tue Feb 21, 2012 8:02 am
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);

Re: Reading and listing a Comma Delimiting file

Posted: Tue Feb 21, 2012 8:24 am
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 />";

Re: Reading and listing a Comma Delimiting file

Posted: Tue Feb 21, 2012 7:09 pm
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>";
}
?>