ibnmuhammad Posted October 16, 2007 Share Posted October 16, 2007 Hey every one, I got this problem with my code and would love it if any one could help with this probelm. Am creating a program that reconises commas in text file (comma delimited code) and i want to implement the same function and loop. This program should get the file, store it ina array then enter a loop that jumps in to the function. here is the txt file: Take That, Greatest Hits, 2007, 10.99, 35 50cent, Am Back, 2005, 11.88, 70 Mike Jone, Never Sing Alone, 2007, 12.99, 30 Terry Gamble, Alone, 1987, 13.99, 40 Michael Jackson, Am Bad, 1990, 15.99, 60 The problem is the code below displays this: Take That Am Back 2007 13.99 60 BUT IT SHOULD PRINT ALL THE INFORMATION IN THE TXT FILE ONE AFTER THE FILE UNDER EACH OTHER. LIKE THIS: Take That Greatest Hits 2007 10.99 35 50cent Am Back 2005 11.88 70 Mike Jone Never Sing Alone 2007 12.99 30 Terry Gamble Alone 1987 13.99 40 Michael Jackson Am Bad 1990 15.99 60 PLEAAASEEE HELPPPPPPP ME WITH MY PROBLEM AM VERY NEW TO PROGRAMMING php: <? $filename = "filename.txt"; $filepointer = fopen($filename, "r"); $myarray = file ($filename); for ($mycount = 0; $mycount < count ($myarray);$mycount++) { $aline = $myarray[$mycount]; $cd = getvalue($myarray[0], $mycount); print "$cd" . "<br> \n"; } function getvalue($file, $mycount) { $intoarray = explode (",", $file); return $intoarray[$mycount]; } fclose($filepointer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73507-please-help-am-new-to-php/ Share on other sites More sharing options...
ibnmuhammad Posted October 16, 2007 Author Share Posted October 16, 2007 The array has this content from a txt file: Array ( [0] => Take That, Greatest Hits, 2007, 10.99, 35 [1] => 50cent, Am Back, 2005, 11.88, 70 [2] => Mike Jone, Never Sing Alone, 2007, 12.99, 30 [3] => Terry Gamble, Alone, 1987, 13.99, 40 [4] => Michael Jackson, Am Bad, 1990, 15.99, 60 ) Quote Link to comment https://forums.phpfreaks.com/topic/73507-please-help-am-new-to-php/#findComment-370817 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2007 Share Posted October 16, 2007 You are only looping through the lines in the file, you are not getting each string out of each line. Look at the function fgetcsv(). Ken Quote Link to comment https://forums.phpfreaks.com/topic/73507-please-help-am-new-to-php/#findComment-370822 Share on other sites More sharing options...
sasa Posted October 16, 2007 Share Posted October 16, 2007 try <? $filename = "filename.txt"; $filepointer = fopen($filename, "r"); $myarray = file ($filename); for ($mycount = 0; $mycount < count ($myarray);$mycount++) { $aline = $myarray[$mycount]; $cd = str_replace(',',"<br />\n", $aline); //$cd = getvalue($myarray[0], $mycount); print "$cd" . "<br> \n"; } function getvalue($file, $mycount) { $intoarray = explode (",", $file); return $intoarray[$mycount]; } fclose($filepointer); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73507-please-help-am-new-to-php/#findComment-370956 Share on other sites More sharing options...
kenrbnsn Posted October 16, 2007 Share Posted October 16, 2007 Using the fgetcsv() function, this is reduced to a 4 line script: <?php $fp = fopen('filename.txt','r'); while ($data = fgetcsv($fp,80)) echo implode("<br>\n",$data)."<br>\n"; fclose($fp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/73507-please-help-am-new-to-php/#findComment-370998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.