View Full Version : Reading rows of array from text file

08-23-2011, 11:50 AM
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:



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: http://student18.gamfe.com.hk/test/33_8.gif http://student18.gamfe.com.hk/test/33_7.gif

Here is my code at the moment but there is something wrong in it as I can't seems to correctly print everything out...

$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\">

08-23-2011, 02:04 PM
Here's my shot at it, untested:

$file = file("33.txt"); // can be any numbered text files
echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"65\" height=\"65\"><tbody>";
foreach($file as $line){
echo "<tr>";
foreach($tiles as $piece){
echo "<td><img src='".$image."' alt='' /></td>";
echo "</tr>";
echo "</tbody></table>";


08-23-2011, 02:14 PM
Start with the following code and modify it appropriately

$contents = @file("33.txt");
if (is_array($contents))
foreach($contents as $row)
$ids = explode(",", $row);
if (is_array($ids))
foreach($ids as $id)
echo "<img src=\"test/33_" . trim($id) . ".gif\" alt=\"\" />\n";

08-23-2011, 06:34 PM
Thanks for the help.

I managed to get it solve with the code samples above. :D

08-23-2011, 08:14 PM
.... and GVRE is correct ... test to make sure the array is a valid array before you loop.

I was being sloppy.