[SOLVED] Simple Counting Solution Needed
Moderator: General Moderators
[SOLVED] Simple Counting Solution Needed
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
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
- binary_w0lf
- Forum Newbie
- Posts: 14
- Joined: Mon Mar 22, 2004 11:48 am
- Location: Greece : Salonika
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
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.
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
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
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.
- andre_c
- Forum Contributor
- Posts: 412
- Joined: Sun Feb 29, 2004 6:49 pm
- Location: Salt Lake City, Utah
Here you go: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.
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>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.
on each iteration, you could add one to a variable, which would give you the total number of iterations of the loop.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
just add $total_pics++; or something similar to the loop
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;
}
?>Hey,
Just tried the counting function and im getting
Any ideas?
Cheers,
Slater
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 102Cheers,
Slater
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';