jch02140 Posted August 23, 2011 Share Posted August 23, 2011 I am trying to print out rows of images and pieces them to form a bigger map but having a little trouble printing out correctly. I am doing the following... A numbered text file with rows of numbers like these: 33.txt 48,48,49,49,48,47,48,49,48,50,50,48,50,49,48,49,49,49 49,11,4,8,50,11,4,4,8,48,11,4,4,8,48,11,8,50 ... The number represent different area and the number in the text file represent each individual pieces of that particular area. the number of rows and columns are arbitrary. Example: Here is my code at the moment but there is something wrong in it as I can't seems to correctly print everything out... <?php $file = file_get_contents("33.txt"); // can be any numbered text files $entries = explode("\n", $file); echo "Entries <br /><hr />"; for($i=0;$i < sizeof($entries);$i++){ for($j=0;$j < sizeof($entries);$j++){ $data = explode(",", $entries[$j]); echo "<td align=\"center\" background=\"test/33_"; echo $data[$j]; echo ".gif\" width=\"65\" height=\"65\"> <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"65\" height=\"65\"> <tbody><tr> <td> <center></center> </td> </tr></tbody> </table> </td>"; } } ?> Link to comment https://forums.phpfreaks.com/topic/245492-reading-rows-of-array-from-text-file/ Share on other sites More sharing options...
AyKay47 Posted August 23, 2011 Share Posted August 23, 2011 after retrieving the contents.. why don't you explode with a comma delimiter right away? then use a foreach loop to display the results.. what are your actual results with this code..? Link to comment https://forums.phpfreaks.com/topic/245492-reading-rows-of-array-from-text-file/#findComment-1260934 Share on other sites More sharing options...
Psycho Posted August 23, 2011 Share Posted August 23, 2011 First off, let me add some suggestions on coding style: You create an array using explode(); and then loop through the results using for($i=0;$i < sizeof($entries);$i++){ You should use a foreach() loop for arrays. In the loop above the function sizeof() is being executed on each iteration of the loop - very inefficient. If you do need to perform a loop on a static value - define that value beforehand. The only reason to have a loop like that is if the array $entries would change length during the loop. Second, instead of using file_get_contents() and exploding the contents using "\n" use file() instead which reads the file as an array to start with. Anyway, give this a try: //Read file into an array of lines $file = file("33.txt"); $tableHTML = =''; foreach($file as $line) { $tableHTML .= "<tr>\n"; $numbers = explode(',', $line); foreach($numbers as $num) { $tableHTML .= "<td align=\"center\" background=\"test/33_{$num}.gif\" width=\"65\" height=\"65\"> </td>\n"; } $tableHTML .= "</tr>\n"; } echo "Entries <br /><hr />"; echo "<table>\n"; echo $table; echo "</table>\n"; Link to comment https://forums.phpfreaks.com/topic/245492-reading-rows-of-array-from-text-file/#findComment-1260943 Share on other sites More sharing options...
jch02140 Posted August 24, 2011 Author Share Posted August 24, 2011 Thanks for the tips. Much appeciated. jch02140 Link to comment https://forums.phpfreaks.com/topic/245492-reading-rows-of-array-from-text-file/#findComment-1261303 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.