Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

all images in a directory

images php coding issues or problems here.

Moderators: macek, egami, gesf

all images in a directory

Postby loriana » Fri Apr 05, 2013 4:37 pm

How can I show 40 images at a time when showing all images in a directory? I have a simple php code that shows all images in a directory with 4 images per row. Now what I want to do is only show the first 40 images, then have a next page link at the bottom that will take you to the next 40. How can I do this? 10 points for the best working answer. Thanks!
_____________________
2nd chance checking account ~ checking account for people with bad credit ~ checking account for bad credit
Last edited by loriana on Mon Apr 08, 2013 10:24 pm, edited 1 time in total.
loriana
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Mar 19, 2013 10:16 am

Re: all images in a directory

Postby filipbekic01 » Mon Apr 08, 2013 2:27 am

Well in case you want to show only first 40 images, i can help you with that. But in order to create page syste, you can find complete scripts on google. Here is code to show only first 40:

for($i=1;$i<=40;$i++) {
echo '<img src="PATH"/>';
}
filipbekic01
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Apr 08, 2013 2:19 am

Re: all images in a directory

Postby swarups » Tue Apr 09, 2013 3:32 am

Hi loriana,

I consider there is no db operation for this. I hope following code will help you.

Code: Select all
$directoryPath = 'Directory name'; //directory name where images are present
$per_page = 40;

//get the page number from url
if(isset($_GET['page_no'])){
   $page = $_GET['page_no'];
}
if($page == '' || $page < 1){
   $page = 1;
}


if (is_dir($directoryPath)) {
  if ($handle = opendir($directoryPath)) {
     while (false !== ($file = readdir($handle))) {
   if ($file != '.' && $file != '..'){
      $arrayDir[$arrayCnt] = $file;
      $arrayCnt = $arrayCnt + 1;
   }
     }
     array_multisort($arrayDir);
     for ($i = 0; $i < ($page*$per_page); $i++) {
      //code to discplay images         
     }
  }
}

swarups
New php-forum User
New php-forum User
 
Posts: 20
Joined: Tue Mar 06, 2012 11:05 pm

Re: all images in a directory

Postby seandisanti » Tue Apr 09, 2013 11:11 pm

i believe scandir is a better way to go. it will return an array of files and directories in a directory. Then you can use an iterator, or paginate it however you want. here's a video showing some SPL usage including iterators. http://jream.com/learning/videos/php-oo ... troduction
seandisanti
php-forum Fan User
php-forum Fan User
 
Posts: 838
Joined: Mon Oct 01, 2012 12:32 pm


Return to PHP coding => Images

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.