[SOLVED] Simple Counting Solution Needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slater
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 12:13 pm
Location: Nottingham, England

[SOLVED] Simple Counting Solution Needed

Post by slater »

Hey,

Here the problem:

I have a page that would like to list all the images in a folder (folder is determined by variable). I need to find the number of images in the folder adn print the thumbnails and links to the fulls sized pics untill all images are displayed. The Pic number will need to increment by 1 each time.

Cheers,
Slater
User avatar
binary_w0lf
Forum Newbie
Posts: 14
Joined: Mon Mar 22, 2004 11:48 am
Location: Greece : Salonika

Post by binary_w0lf »

?? Why dont you just use a gallery??
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

something like this maybe

Code: Select all

<?php

$dir = "/some/dir/";
	
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
			
			if ($file!="." && $file!=".." && $file!="Thumbs.db"  && $pic_type==$type) {
				echo "<img src='$file' />";
			}
		}
	}

	closedir($dh);
}

?>
Last edited by andre_c on Tue Mar 23, 2004 12:51 pm, edited 1 time in total.
slater
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 12:13 pm
Location: Nottingham, England

Post by slater »

yea thats the sort of thing im after, just one thing, do i need to define $type?

Gallery scripts get too complex,, i only need one little feature.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

sorry, pic_type is something that I was using on my code, but you don't need to use it
slater
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 12:13 pm
Location: Nottingham, England

Post by slater »

Right,

The code above works fine, but (allways a but), just need to make it a bit more complex, if i can,

i have around 100pics in each folder is it possible to split the resuilts into two rows of 5 pics, so it would display 10pics per page, 5 on top row 5 on the bottom and then list the pages at the bottom of the table (e.g. Page: 1 2 3 4 etc)

Cheers,
Slater
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

slater wrote:Right,

The code above works fine, but (allways a but), just need to make it a bit more complex, if i can,

i have around 100pics in each folder is it possible to split the resuilts into two rows of 5 pics, so it would display 10pics per page, 5 on top row 5 on the bottom and then list the pages at the bottom of the table (e.g. Page: 1 2 3 4 etc)

Cheers,
Slater

some judicial use of loops (probably a for loop), and some vars to keep track of the page to decide what image to display could work.

You could stick all the filenames into an array, use a nested loop to iterate 5 times twice, keep track of the last pic displayed, and then work out how many page links to be displayed depending on how many images u need to still display. Start looping from the image after last displayed until 10 after that.

Shouldn't be too difficult to implement. I have probs confused the hell out of you with my crap explanation! sorry if that's the case. I'm sure someone else can come up with a better explanation or expain this better.

I would do some code for you if it were not for that fact that I'm off to bed now lol.
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

Here you go:

Code: Select all

<table>
<tr>
<?php

$dir = "/some/dir/";
   
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
      $counter = 0;
      while (($file = readdir($dh)) !== false) {
         if ($counter % 5 == 0) echo "</tr><tr>"; $counter++;
         if ($file!="." && $file!=".." && $file!="Thumbs.db") {
            echo "<td><img src='$file' /></td>";
         }
      }
   }

   closedir($dh);
}

?>
</tr>
</table>
I'm sure you can figure out the rest

for the pages, sorry but i don't have the time to go through that. You can find tutorials on pagination if you google. Or try being creative with your code. If you write some stuff, i'm sure a lot of people will be willing to help.
slater
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 12:13 pm
Location: Nottingham, England

Post by slater »

Just another quick and easy one,

Can i get a number value from $file to put into $totalpics

e.g. if theres 96 images in a folder $totalpics = 96;

Cheers :)
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

slater wrote:Just another quick and easy one,

Can i get a number value from $file to put into $totalpics

e.g. if theres 96 images in a folder $totalpics = 96;

Cheers :)
on each iteration, you could add one to a variable, which would give you the total number of iterations of the loop.

just add $total_pics++; or something similar to the loop
User avatar
werlop
Forum Commoner
Posts: 68
Joined: Sat Mar 22, 2003 2:50 am
Location: /dev/null

Post by werlop »

I found this in the PHP manual, it returns the number of files in a directory of a certain type. I'm sure you could mod it to suit your needs.

Code: Select all

<?php
//php dot net at CamelQuotesNO-SPAM-PLEASE dot com
//04-Aug-2002 11:52 
//Use this function to return the number of files matching a certain type //(extention) in a given folder:

//Using the previous code in the function above, file extentions like .ext.inc.php will be counted as .php files

//usage:
//returns false if dir doesnt exist
//or returns the number of files counted

//DO NOT add '.' to extention you are searching for.

//example usage:

$result = CountFiles("./images", "jpg");
if($result === false) die("dir does not exist!");

function CountFiles($dir, $type)
{
if(!($dh =@opendir("$dir")))
  return false; // directory does not exist
  
// read the directory contents searching for "type" files
// and count how many are found:
  $files = 0;
  while ( ! ( ($fn = readdir($dh)) === false ) )
   {
   $f = strrev($fn);
   $ext = substr($f, 0, strpos($f,"."));
   $f_ext = strrev($ext);
   if(( strcasecmp($f_ext, $type) == 0 )) $files++;
   }
  closedir($dh);
  return $files;
} 
?>
slater
Forum Newbie
Posts: 10
Joined: Tue Mar 23, 2004 12:13 pm
Location: Nottingham, England

Post by slater »

Hey,

Just tried the counting function and im getting

Code: Select all

Fatal error: Call to undefined function: countfiles() in /home/elliottp/public_html/sunshine/index.php on line 102
Any ideas?

Cheers,
Slater
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

here is a simple solution...

Code: Select all

$image_count = array();
// Open a known directory, and proceed to read its contents 
if (is_dir($dir)) { 
   if ($dh = opendir($dir)) { 
      while (($file = readdir($dh)) !== false) { 
         if ($file!="." && $file!=".." && $file!="Thumbs.db"  && $pic_type==$type) { 
            echo "<img src='$file' />"; 
            $image_count[] = $file;
         } 
      } 
   } 
   closedir($dh); 
} 
echo 'There are '.count($image_count).' files in this directory';
Post Reply