Jump to content

Recommended Posts

Ok, I'm not a developer by any means, but I thought I'd take a stab at this solution a buddy of mine needs.

I've managed to make script (pieced together from a few forums.) that dynamically fills in a table 3 columns wide with every image in a directory (folder). What I need now is to be able to stop filling the table once there are 6 cells full (3 columns by 2 rows) then create a numbered breadcrumb list under the table that takes you to the next set. Make sense at all?

 

 

Thanks,

Pindawg

 

Code Used below:

 

<?php

    $path = "images/tees";
    $path_large = "images/tees/large";
    $title = 'Need formula here';
    $extensions = Array('.gif','.jpg','.png');
    $num_cols = 3;
    $img_width = '215';

    $images = Array();
    if(@$handle = opendir($path))
    {
    while(false!== ($file = readdir($handle)))
    {
    if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions))
    {
    $images[] = $file;
    }
    }
    }

    echo "<table>\n";
    echo "<colgroup>\n";
    for($i=0;$i<$num_cols;$i++)
    {
    echo "<col width='".$img_width."' />\n";
    }
    echo "</colgroup>\n<tr>\n";

    if(count($images)>0)
    {
    $i=1;
    foreach($images as $image)
    {
    if($i==$num_cols+1)
    {
    echo "</tr>\n<tr>\n";
    $i=1;
    }
    echo "<td><a href='$path_large/$image' rel='lightbox' title='$title'><img src='$path/$image' width='$img_width' /></a></td>\n";
    $i++;
    }
    if($i <= $num_cols)
    {
    while($i<=$num_cols)
    {
    echo "<td> </td>\n";
    $i++;
    }
    }
    }
    else
    {
    echo "<td>No images in specified folder</td>\n";
    }
    echo "</tr>\n</table>\n";
    ?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/133367-solved-php-table-help/
Share on other sites

To archive what you're tying to do only requires a few small changes to your code.

 

First add

// split the $images array into separate arrays containing 6 images
// see http://php.net/array-chunk for documentation
$pages = array_chunk($images, 6);

// get the request page
$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page']-1 : 0;

 

After the following

    while(false!== ($file = readdir($handle)))
    {
    if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions))
    {
    $images[] = $file;
    }
    }
    }

 

Next change

    if(count($images)>0)
    {
    $i=1;
    foreach($images as $image)
    {

 

to

    if(count($pages[$page]) > 0)
    {
    $i=1;
    foreach($pages[$page] as $image)

 

Now to add your page links change

    echo "</tr>\n</table>\n";

 

to

echo "</tr>\n<tr>\n<td colspan=\"3\" align=\"center\">Page: ";

for($i = 0, $p = count($pages); $i < $p; $i++)
    echo ' <a href="?page='.($i+1).'">'.($i+1) .'</a>';

echo "</td>\n</tr>\n</table>";

Ok so I now get better results using the above code. It adds pages links to the bottom of the table, but it only loads every 6th image, insted of loading 6 images into the table. Did I miss something?

 

 

 

<?php

 

    $path = "images";

    $path_large = "images/large";

    $title = 'Need formula here';

    $extensions = Array('.gif','.jpg','.png');

    $num_cols = 3;

    $img_width = '215';

 

    $images = Array();

    if(@$handle = opendir($path))

    {

    while(false!== ($file = readdir($handle)))

    {

    if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions))

    {

    $images[] = $file;

    }

    }

 

// split the $images array into separate arrays containing 6 images

// see http://php.net/array-chunk for documentation

 

$pages = array_chunk($images, 6);

 

// get the request page

$page = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page']-1 : 0;

 

    echo "<table>\n";

    echo "<colgroup>\n";

    for($i=0;$i<$num_cols;$i++)

    {

    echo "<col width='".$img_width."' />\n";

    }

    echo "</colgroup>\n<tr>\n";

 

    if(count($pages[$page]) > 0)

    {

    $i=1;

    foreach($pages[$page] as $image)

    if($i==$num_cols+1)

    {

    echo "</tr>\n<tr>\n";

    $i=1;

    }

    echo "<td><a href='$path_large/$image' rel='lightbox' title='$title'><img src='$path/$image' width='$img_width' /></a></td>\n";

    $i++;

    }

    if($i <= $num_cols)

    {

    while($i<=$num_cols)

    {

    echo "<td> </td>\n";

    $i++;

    }

    }

    }

    else

    {

    echo "<td>No images in specified folder</td>\n";

    }

echo "</tr>\n<tr>\n<td colspan=\"3\" align=\"right\">Page: ";

 

for($i = 0, $p = count($pages); $i < $p; $i++)

    echo ' <a href="?page='.($i+1).'">'.($i+1) .'</a>';

 

echo "</td>\n</tr>\n</table>";

    ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.