SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2000
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Image File Looping Help

    Hello Everyone,

    I am having a bit of a problem trying to come up with a solution on how to accomplish this PHP task. First, let me explain what I am trying to do.

    I have over 2,300 images in a directory called graphics. These images are divided into three categories by their filenames.

    For example this is what I have:

    Files 100000.jpg thru 100574.jpg are small images.
    Files 200000.jpg thru 201287.jpg are medium images.
    Files 300000.jpg thru 300523.jpg are large images.

    What I would like to do is to be able to list only files 100000.jpg thru 100019.jpg on a page within a HTML table that has 5 columns and 4 rows per page. In other words, I would only want files 100000.jpg thru 100019.jpg on the first page.

    On the second page I would have files 100020.jpg thru 100039.jpg. But you don't have to worry about figuring out how to create the multiple pages, I pretty much know how I am going to accomplish that.

    Thanks in advance, and I look forward to your replies.

  2. #2
    SitePoint Zealot patrikG's Avatar
    Join Date
    Aug 2003
    Location
    Sussex, UK
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like you already sussed it - so what's the problem?

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2000
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is what I mentioned above. How do you do the looping procedure as mentioned? As soon as I can get the looping procedure, I can apply it for the other pages.

  4. #4
    SitePoint Zealot patrikG's Avatar
    Join Date
    Aug 2003
    Location
    Sussex, UK
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you already have an array with the filenames?

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2000
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If this is what you mean, then yes, I have an array set up to grab all the files in a directory. But I don't know how to get it to do what I want it to do.

    PHP Code:
    <?

    $path 
    'graphics'
    $dir opendir($path);
    while ((
    $file readdir($dir)) !== false) { 
        if (
    is_file($path.'/'.$file) && $file != '.' && $file != '..'
            echo 
    "<img src=$path/$file>";
    }

    closedir($dir);

    ?>
    What I would like to do is to be able to list only files 100000.jpg thru 100019.jpg on a page within a HTML table that has 5 columns and 4 rows per page. So, where do I go from here?

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2000
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am still looking for a solution if anyone has one. I know this can be done, but I can't pinpoint what I need to do in order to accomplish it.

  7. #7
    SitePoint Zealot patrikG's Avatar
    Join Date
    Aug 2003
    Location
    Sussex, UK
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    read the filename and path into an array

    PHP Code:
     <?
      $path 
    'graphics';
      
    $dir opendir($path);
      while ((
    $file readdir($dir)) !== false)
          {
          if (
    is_file($path.'/'.$file) && $file != '.' && $file != '..')
              
    $images[]=$file;
          }
      
    closedir($dir);
      
    array_chunk($images,19,true);
      
    ?>
    Then loop through the array using foreach and output them accordingly.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2000
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Patrik.

    But how would I go about getting images 20-39 with this approach?

    Also, I am still unsure how to set up the HTML table to break down the 5 columns per 4 rows with using the array. I need the script to create a table with 5 columns and an arrayed image in each column. There would be 4 rows of 5 in the table.

    Example:

    R1 1 2 3 4 5
    R2 1 2 3 4 5
    R3 1 2 3 4 5
    R4 1 2 3 4 5

    I would need the script to do something like this on the fly:

    Code:
    <table>
    <tr>
    <td><img src="graphics/100000.jpg"></td>
    <td><img src="graphics/100001.jpg"></td>
    <td><img src="graphics/100002.jpg"></td>
    <td><img src="graphics/100003.jpg"></td>
    <td><img src="graphics/100004.jpg"></td>
    </tr>
    <tr>
    <td><img src="graphics/100005.jpg"></td>
    <td><img src="graphics/100006.jpg"></td>
    <td><img src="graphics/100007.jpg"></td>
    <td><img src="graphics/100008.jpg"></td>
    <td><img src="graphics/100009.jpg"></td>
    </tr>
    <tr>
    <td><img src="graphics/100010.jpg"></td>
    <td><img src="graphics/100011.jpg"></td>
    <td><img src="graphics/100012.jpg"></td>
    <td><img src="graphics/100013.jpg"></td>
    <td><img src="graphics/100014.jpg"></td>
    </tr>
    <tr>
    <td><img src="graphics/100015.jpg"></td>
    <td><img src="graphics/100016.jpg"></td>
    <td><img src="graphics/100017.jpg"></td>
    <td><img src="graphics/100018.jpg"></td>
    <td><img src="graphics/100019.jpg"></td>
    </tr>
    </table>
    My next page I would want to be able to tell the script where to go next with the images, such as this:

    Code:
    <table>
    <tr>
    <td><img src="graphics/100020.jpg"></td>
    <td><img src="graphics/100021.jpg"></td>
    <td><img src="graphics/100022.jpg"></td>
    <td><img src="graphics/100023.jpg"></td>
    <td><img src="graphics/100024.jpg"></td>
    </tr>
    
    ...
    
    <tr>
    <td><img src="graphics/100035.jpg"></td>
    <td><img src="graphics/100036.jpg"></td>
    <td><img src="graphics/100037.jpg"></td>
    <td><img src="graphics/100038.jpg"></td>
    <td><img src="graphics/100039.jpg"></td>
    </tr>
    </table>
    Thanks for the help you have provided so far, I have been scoring the internet since I posted this topic for an answer.

  9. #9
    SitePoint Zealot patrikG's Avatar
    Join Date
    Aug 2003
    Location
    Sussex, UK
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmoore
    But how would I go about getting images 20-39 with this approach?
    array_chunk() as shown above turns your one-dimensional array $images into a two-dimensional array, each array item a chunk of 19 items of the original array.

    PHP Code:
     <?
       $path 
    'graphics';
       
    $dir opendir($path);
       while ((
    $file readdir($dir)) !== false)
           {
           if (
    is_file($path.'/'.$file) && $file != '.' && $file != '..')
               
    $images[]=$file;
           }
       
    closedir($dir);
     
       
    $images_split=array_chunk($images,19,true);
     
       echo 
    "<h4>Images</h4><pre>";
       
    print_r($images_split);
       echo 
    "</pre>";
      
    ?>
    The bottom bit will show you what I mean.

  10. #10
    SitePoint Enthusiast
    Join Date
    May 2000
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what you mean with that, but I still haven't figured out how to make it loop within a table like I mentioned above.

  11. #11
    SitePoint Zealot patrikG's Avatar
    Join Date
    Aug 2003
    Location
    Sussex, UK
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmoore
    I see what you mean with that, but I still haven't figured out how to make it loop within a table like I mentioned above.
    Use foreach for that. E.g.

    PHP Code:
     foreach ($images_split as $image_file)
         {
         echo 
    '<tr>';
         foreach (
    $image_file as $image)
             {
             echo 
    '<td><img src="graphics/'.$image.'"></td';
             }
         echo 
    '</tr>';
         } 


Bookmarks

Posting Permissions

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