Next - Prev (Image Set)

images php coding issues or problems here.

Moderators: macek, egami, gesf

DWillett
New php-forum User
New php-forum User
Posts: 22
Joined: Fri May 25, 2012 12:19 am

Next - Prev (Image Set)

Postby DWillett » Tue May 29, 2012 8:09 am

My code pulls images and displays 12 of them, 12 seems to fit my browser nicely.
I could increase the number in the code but on testing 12 is the magic number.
My image sets contain more than 12 so can I use a "Next - Prev - First - Last" etc to the next 12 in the set?
Very new to doding guy's, spent 10 years with VB6 so any walk through is a benefit for my learning.

Kind Regards

Code: Select all

    <?php
     error_reporting(0);
     ?>
    <?PHP
      function getFileList($dir)
      {
        // array to hold return value
        $retval = array();

        // add trailing slash if missing
        if(substr($dir, -1) != "/") $dir .= "/";

        // open pointer to directory and read list of files
        $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
        while(false !== ($entry = $d->read())) {
          // skip hidden files
          if($entry[0] == ".") continue;
          if(is_dir("$dir$entry")) {
            $retval[] = array(
              "name" => "$dir$entry/",
              "type" => filetype("$dir$entry"),
              "size" => 0,
              "lastmod" => filemtime("$dir$entry")
            );
          } elseif(is_readable("$dir$entry")) {
            $retval[] = array(
              "name" => "$dir$entry",
              "size" => filesize("$dir$entry"),
              "lastmod" => filemtime("$dir$entry")
            );
          }
        }
        $d->close();

        return $retval;
      }
    ?>
    <?PHP
      $dirlist = getFileList(".");


      // output file list as HTML table
      echo "<table border=\"1\">\n";
      echo "<tr><th>Name</th></tr>\n";
       

      foreach($dirlist as $file) {
    if(!preg_match("/\.pdf$/", $file['name'])) continue;
        echo "<tr>\n";
        echo "<td><a href=\"{$file['name']}\" target=\"inner\">{$file['name']}</a></td>\n";
        echo "</tr>\n";
       
      }
      echo "</table>\n\n";
     

    ?>

    <?php
    //path to directory to scan. i have included a wildcard for a subdirectory

    $directory = "";
     

    //get all image files with a .jpg extension.

    $images = glob("" . $directory . "*.jpg");


    $imgs = '';

    // create array

    foreach($images as $image){ $imgs[] = "$image"; }


    //shuffle array

    shuffle($imgs);


    //select first 12 images in randomized array

    $imgs = array_slice($imgs, 0, 12);


    //display images

    foreach ($imgs as $img) {
        echo "<a href=\"$img\" target=\"inner\"><img src='$img' width=\"80\" border=\"1\"/></a> ";

    }
    ?>

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Next - Prev (Image Set)

Postby Nullsig » Tue May 29, 2012 10:29 am

What you want to do is "paginate" the data.

I'll walk you through the general idea of pagination and let you do the grunt work with your own project.

You first need to adopt your script to handle a get variable so that you can directly access the page on click of a link:

Code: Select all

$page = (isset($_GET['page'])) ? $_GET['page'] : 1;


You will also need to figure out if you have more than one page of 12 when you query the database.

Code: Select all

$query = "SELECT * FROM blah WHERE blah2=blah3
if($rs = mysql_query($query)){
      $numberOfPages = ceil(mysql_num_rows($rs)/12);//ceil will round up.
}



While you output your data you want to limit to only the first 12

Code: Select all

$i = 1;
while($row = mysql_fetch_assoc($rs) && $i <= 12){
      //output your data.
}

DWillett
New php-forum User
New php-forum User
Posts: 22
Joined: Fri May 25, 2012 12:19 am

Re: Next - Prev (Image Set)

Postby DWillett » Tue May 29, 2012 2:11 pm

hmmmm

Looks very similar to vb script, well the concept is !! Similar syntax which I can sort of see through just about.
I'm in the deep end once more.....

Appreciate the help though.
May take me some time but I'll give it my best shot, forgive me if I have to ask the silliest questions and extend this thread.

Here goes

Kind regards

DWillett
New php-forum User
New php-forum User
Posts: 22
Joined: Fri May 25, 2012 12:19 am

Re: Next - Prev (Image Set)

Postby DWillett » Wed May 30, 2012 12:33 am

Hi NullSig

Googles great and returns some interesting pages on "Pagination" !
Being the complete novice in this field may prove quite difficult.
It seems pagination requires a MySQL database in the bckground, my project doesn't use one.
My images are uploaded via my VB project and from what I understand about my script (as above) is that it doesn't create a database entry from the outset.

I don't know where this leaves me??

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Next - Prev (Image Set)

Postby Nullsig » Wed May 30, 2012 7:09 am

My pagination example did require a SQL script.

You do have to have some sort of data source to access. You can use a flat file. Or you can use the "scandir" function to scan the images from a directory.

One of the scripts I use for a photo gallery generates a Pagination using a list of images from a scanned directory. The idea is the same. The functions you will use differ on the data source you are utilizing.

DWillett
New php-forum User
New php-forum User
Posts: 22
Joined: Fri May 25, 2012 12:19 am

Re: Next - Prev (Image Set)

Postby DWillett » Fri Jun 01, 2012 1:03 am

Hi Nullsig
Thanks for that.
I'll check out the ScanDir script and see how it fits with my project.
I am interested in the gallery script you mentioned, is it available?

Regards

User avatar
Nullsig
php-forum Fan User
php-forum Fan User
Posts: 981
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Re: Next - Prev (Image Set)

Postby Nullsig » Fri Jun 01, 2012 5:35 am

:( Unfortunately it is owned by the company I work for and they have not made it publicly available.

That's the issue with working for a company. They take credit for and make profit from your work.

DWillett
New php-forum User
New php-forum User
Posts: 22
Joined: Fri May 25, 2012 12:19 am

Re: Next - Prev (Image Set)

Postby DWillett » Fri Jun 01, 2012 5:46 am

No worries.. I'll get there eventually.

Kind Regards

DWillett
New php-forum User
New php-forum User
Posts: 22
Joined: Fri May 25, 2012 12:19 am

Re: Next - Prev (Image Set)

Postby DWillett » Fri Jun 15, 2012 5:48 am

I did some searching and came up with this code. The code should pull jpg files from the current Directory(Folder) but returns nothing but the following text although there are 50 or so images:

Files in Directory
Page 1 of 0

Can anyone help with the code:

Code: Select all

<?php 
$directory = "";

$files = array();
if( is_dir( $dir ) ) {
  if( $dh = opendir( $dir ) ) {
    while( ( $file = readdir( $dh ) ) !== false ) {
      if( $file != "." && $file != ".." ) {
        array_push( $files, $file );
      }
    }
    closedir( $dh );
  }
}

$page_size = 3;
$total_pages = ceil( count( $files ) / $page_size );

if( isset( $_GET['p'] ) ) {
  $current_page = $_GET['p'];
  if( $current_page > $total_pages ) {
    $current_page = $total_pages;
  }
} else {
  $current_page = 1;
}

$start = ( $current_page * $page_size ) - $page_size;

print( "Files in Directory " . $dir . "<br>\n" );
print( "Page " . $current_page . " of " . $total_pages . "<br><br>\n\n" );

for( $j=0; $j<$total_pages; $j++ ) {
  $p = $j + 1;
  print( "<a href='?p=" . $p . "'>" . $p . "</a> " );
}

print( "<br><br>" );
print( "<hr>" );

for( $i=$start; $i<$start + $page_size; $i++ ) {

/*the line below, verifies if the file exists in the directory and
only shows the files that exist and does not show more the error.*/

if( is_file( $dir . $files[$i] ) ) { 

print( "<img src='" . $dir . $files[$i] . "' border='0' vspace='2'
hspace='2'><br>" );
print( "<hr>" );   
 
  }
}
?>


Return to “PHP coding => Images”

Who is online

Users browsing this forum: No registered users and 1 guest