Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Reading rows of array from text file

    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
    Code:
    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 Code:
    <?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>"
    ;
            }
        }
    ?>

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,080
    Thanks
    10
    Thanked 1,158 Times in 1,149 Posts
    Here's my shot at it, untested:

    PHP Code:
    <?php 
        $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>";
              
    $tiles=explode(",",$line);
              foreach(
    $tiles as $piece){
              
    $image="test/33_".$piece.".gif";
              echo 
    "<td><img src='".$image."' alt='' /></td>";
              }
           echo 
    "</tr>";
           }
        echo 
    "</tbody></table>";
    ?>


    .

  3. #3
    Regular Coder
    Join Date
    May 2011
    Posts
    246
    Thanks
    1
    Thanked 59 Times in 58 Posts
    Start with the following code and modify it appropriately
    PHP Code:
    $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";
                            }
                    }
            }


  4. #4
    New to the CF scene
    Join Date
    Nov 2005
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help.

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

  5. #5
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,080
    Thanks
    10
    Thanked 1,158 Times in 1,149 Posts
    .... and GVRE is correct ... test to make sure the array is a valid array before you loop.

    I was being sloppy.


    .


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •