General Community > Scripting Help

Help needed with a Image Gallery code


The Wizard:

I'm working on a simple script that reads a Dir of image files and shows them in a table. I want to have six images show up at a time and have then in two rows of three. This is simple enough, but I'm having problems intergrating the pagination into the deal. Below is a copy of the code I'm working on.

Note I also have it displayingwhats in the file in text as well just as a test so don't worry about that part it's displaying the images in two columns that I can't figure out. I'm still learning php so I'm not that good yet.



--- Quote ---<?php

//Read filenames!!!
$dir = opendir(".");

//Load the files from images directory to filename array
while (($file = readdir($dir)) !== false)
     if (eregi("gif", $file) || eregi("jpg", $file) || eregi("png", $file))
                  $fn[] = $file;//add the file into the files array


// puts files in alphabetical order
// Put our data into an array 
$dataArray = $fn; 
// Get the current page 
$currentPage = trim($_REQUEST[page]); 
// Pagination settings 
$perPage = 6; 
$numPages = ceil(count($dataArray) / $perPage); 
if(!$currentPage || $currentPage > $numPages) 
    $currentPage = 0; 
$start = $currentPage * $perPage; 
$end = ($currentPage * $perPage) + $perPage; 
// Extract ones we need 
foreach($dataArray AS $key => $val) 

    if($key >= $start && $key < $end) 
        $pagedData[] = $dataArray[$key]; 

// *** Displays Data in file *****
foreach($pagedData AS $item) 
    echo $item . "<br>"; 
//  Test Table Area

foreach($pagedData AS $item)
echo "<table width=\"100%\" cellspacing=\"3\"><tr>
      </tr><tr><td colspan=\"2\"><hr /></td></tr><tr>
      <td align=\"center\"><img src=\"$item\" /></td>";
// ***** Displays next and previous links ****   
if($currentPage > 0 && $currentPage < $numPages) 
    echo '<a href="?page=' . ($currentPage - 1) . '">« Previous page</a><br>'; 
if($numPages > $currentPage && ($currentPage + 1) < $numPages) 
    echo '<a class="right" href="?page=' . ($currentPage + 1) . '">Next page »</a><br>'; 

--- End quote ---

Hi The Wizard!

You're doing a great job already with PHP, even though you are still learning :)

I see you are using eregi() in your script. Take a look here:
That function is deprecated, that means it's not supported anymore.

I'm not that experienced with pagination, heck I suck at maths so that's just my 2 cents :)

The Wizard:
Thank you Yoshi2889 for pointing out for me. I'll have to come up with a fix for that. Is there anybody out there who has a idea how to fix my code. I know I'm doing something dumb, but I just cant see the answer.


The Wizard:
I have solved this problem.

Thanks everybody!

Glad you got it solved :)


[0] Message Index

Go to full version