 |
|
|
08-15-2006, 10:48 AM
|
Php code help...
|
Posts: 3,591
Name: Praveen
Location: Chennai, India
|
Code:
<?
$path = "path to folder";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo "<table width=\"100%\" align=\"center\">
<tr>
<td style=\"padding-right: 10px;\"><div class=\"text\"><div align=\"center\">Just copy and paste the code where you would like the picture to be!<br></div>
<br>";
//running the while loop
while ($file = readdir($dir_handle))
{
if($file!="." && $file!="..")
echo " <table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td style=\"padding-right: 10px;\" class=\"too\"><a href=\"http://www.mywebsitename.com/folder//$file\" target=\"_blank\"><img src=\"http://www.mywebsitename.com/folder//$file\" width=\"150\" height=\"150\" border=\"1\" style=\"border-color: 000000;\"></a><br><div align=\"center\"><a href=\"http://www.mywebsitename.com/folder//$file\" target=\"_blank\">Click to Enlarge</a></div></td>
</td>
</tr>
</table>
<br />
<br>";
}
echo "
</td>
</tr>
</table>";
closedir($dir_handle);
?>
Ok, the above code parses through a specified directory and lists all the images in it in a list format.
now what i want is
display x number of images per row and repeat until all the images have been displayed.
to make it clear
if the folder has 20 images,
i want 5 rows of 4 images in each row.
any idea on how that can be achieved?
Thanks.
|
|
|
|
08-15-2006, 12:22 PM
|
Re: Php code help...
|
Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
|
now i'm a bit rusty with php ... but did i understand you right?
if you had 20 imgs you want 5 rows with 4 images,
if you had 18 you would want 6 rows with 3 imgs (a) or would you prefer 4 rows with 4 and the last one with 2(b)?
first you have to count the number of images.
a) a for loop for say x from 1 to 5.
b) mathematically you could try and see what the modulo is for 4,5,6 (or 2,3,5) with fmod() and if it is 0 in one of the cases use that in the for loop descibed.
ps: sorry if i complicate things, i just came from work and am tired :S ... also i'm not sure i understood you right :S
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
Last edited by OmuCuSucu; 08-15-2006 at 12:24 PM..
|
|
|
|
08-15-2006, 12:30 PM
|
Re: Php code help...
|
Posts: 84
|
Get the number of images in a folder.
LOOP until the last image:
$imgechoed++;
if $imgechoed % $perrow eq 0 then next row.
LOOP:
--
Something like that... I hope it make sense 
|
|
|
|
08-15-2006, 12:33 PM
|
Re: Php code help...
|
Posts: 3,591
Name: Praveen
Location: Chennai, India
|
thats a good point
ideally i want a fixed number of columns and the rows to increase based on the number of images.
so if the number of columns is kept as 4
total of 20 images would produce 5 rows
and 18 images would produce
4 complete rows and another row with 2 images.
Quote:
|
would you prefer 4 rows with 4 and the last one with 2(b)?
|
this would be nice.
unfortunately my knowledge of php is zero. so someone has to write that piece of code for me plz
|
|
|
|
08-15-2006, 09:49 PM
|
Re: Php code help...
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
This should help:
PHP Code:
<?php $path = 'path/to/folder/'; $columns = 4; if ($file_dir = @dir($path)) { $i = 1; while ($file = $file_dir->read()) { echo '<a href="http://www.mywebsitename.com/' . $path . $file . '" target="_blank">' . "\n" . ' <img src="http://www.mywebsitename.com/' . $path . $file . '" width="150" height="150" border="1" style="border-color: 000000;">' . "\n" . '</a>' . "\n"; if ($i == $columns) { echo '<br />'; $i = 1; } else { $i++; } } } else { die("Unable to open $path"); } ?>
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
Last edited by mgraphic; 08-15-2006 at 09:51 PM..
|
|
|
|
08-16-2006, 12:11 AM
|
Re: Php code help...
|
Posts: 3,591
Name: Praveen
Location: Chennai, India
|
Quote:
Originally Posted by mgraphic
This should help:
PHP Code:
<?php $path = 'path/to/folder/'; $columns = 4; if ($file_dir = @dir($path)) { $i = 1; while ($file = $file_dir->read()) { echo '<a href="http://www.mywebsitename.com/' . $path . $file . '" target="_blank">' . "\n" . ' <img src="http://www.mywebsitename.com/' . $path . $file . '" width="150" height="150" border="1" style="border-color: 000000;">' . "\n" . '</a>' . "\n"; if ($i == $columns) { echo '<br />'; $i = 1; } else { $i++; } } } else { die("Unable to open $path"); } ?>
|
Yes, this works Perfectly
but there are 2 things which needs a lil change.
in the a href="http://www.websitename.com" , i gave the http://websitename.com/folder/
and in the path, i gave the name of the sub folder which has all the images.
so the full path would look like
http://www.websitename.com/folder/subfolder/
the images in the subfolder were displayed perfectly but it also tried to display a graphic for "folder" and "subfolder"
i dont want to give the url here, but if u want, i can pm you so that you can take a look at what i am saying.
Thank you 
|
|
|
|
08-16-2006, 12:02 PM
|
Re: Php code help...
|
Posts: 3,591
Name: Praveen
Location: Chennai, India
|
any one?
|
|
|
|
08-16-2006, 12:36 PM
|
Re: Php code help...
|
Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
|
maybe this?
PHP Code:
<?php $path = 'path/to/folder/'; $columns = 4; if ($file_dir = @dir($path)) { echo '<a href="http://www.mywebsitename.com/folder/subfolder/" target="_blank">' . "\n" . ' <img src="http://www.mywebsitename.com/blahblah/subfolder.jpg" width="150" height="150" border="1" style="border-color: 000000;">' . "\n" . '</a>' . "\n"; echo '<a href="http://www.mywebsitename.com/folder/" target="_blank">' . "\n" . ' <img src="http://www.mywebsitename.com/blahblah/folder.jpg" width="150" height="150" border="1" style="border-color: 000000;">' . "\n" . '</a>' . "\n"; $i = 3; while ($file = $file_dir->read()) { echo '<a href="http://www.mywebsitename.com/' . $path . $file . '" target="_blank">' . "\n" . ' <img src="http://www.mywebsitename.com/' . $path . $file . '" width="150" height="150" border="1" style="border-color: 000000;">' . "\n" . '</a>' . "\n"; if ($i == $columns) { echo '<br />'; $i = 1; } else { $i++; } } } else { die("Unable to open $path"); } ?>
__________________
.
» Please remember to add to my Talkupation if you enjoyed my post. Thank you :)
.
Last edited by OmuCuSucu; 08-16-2006 at 12:38 PM..
Reason: small correction :S
|
|
|
|
08-16-2006, 11:00 PM
|
Re: Php code help...
|
Posts: 256
Location: Auckland, New Zealand
|
I decided to add more to this script, since I feel security wise, it would be a good idea to hardcode the file extensions required, in this case image file extensions, could have gone further with mime-type checking, recursive directory looking, etc but already I think a class would be better in dealing with this.
Simply changing the $fixed_columns variable, allows you to alter how many images you'll store per row. I also decided on storing the images in an array and closing the directory handler before working on displaying the information. Fixed other problems like '.' current directory and '..' parent directory as well as directory names.
PHP Code:
<?php $path = '/path/to/images/'; $fixed_columns = 4; $ext_list = array('png','jpg','gif','ico');
if($dir_handle = @opendir($path)) { $images = array(); while(false !== ($file = readdir($dir_handle))) { if($file != '.' && $file != '..' && !is_dir($file)) { foreach($ext_list as $ext) { if(substr($file,-4,4) == '.' . $ext) { $images[] = $file; } } } } closedir($dir_handle);
$row = 0;
if(count($images) > 0) { echo '<table style="margin: 0 auto; text-align: center; width: 100%;" cellspacing="0" cellpadding="0">'."\n\t".'<tr>'."\n\t"; foreach($images as $image) { if($row != 0 && $row % $fixed_columns == 0) { echo '</tr>'."\n\t".'<tr>'."\n\t"; } echo "\t".'<td style="padding-right: 10px;" class="too"><a href="http://www.yourwebsitename.com/blah"><img style="border: 1px solid #000;" src="'.$path.$image.'" width="150" height="150" alt="'.substr($image,0,-4).'" /></a></td>'."\n\t"; $row++; } echo '</tr>'."\n".'</table>'."\n"; } else { echo '<p>There are no images in this directory.</p>'; } } else { exit('<p>Error: Could not open directory</p>'); } ?>
Cheers,
MC
__________________
#------------------------------ signature---------------------------------------------------------------------------------#
Quote:
|
I am well recognised for what I don't do than what I do. Chores are just one of those things.
|
|
|
|
|
08-16-2006, 11:14 PM
|
Re: Php code help...
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Quote:
Originally Posted by mastercomputers
I decided to add more to this script, since I feel security wise, it would be a good idea to hardcode the file extensions required, in this case image file extensions, could have gone further with mime-type checking, recursive directory looking, etc but already I think a class would be better in dealing with this.
Simply changing the $fixed_columns variable, allows you to alter how many images you'll store per row. I also decided on storing the images in an array and closing the directory handler before working on displaying the information. Fixed other problems like '.' current directory and '..' parent directory as well as directory names.
|
I was just about to go further, but you gave a better example than I could 
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
08-16-2006, 11:35 PM
|
Re: Php code help...
|
Posts: 3,591
Name: Praveen
Location: Chennai, India
|
Quote:
Originally Posted by mastercomputers
I decided to add more to this script, since I feel security wise, it would be a good idea to hardcode the file extensions required, in this case image file extensions, could have gone further with mime-type checking, recursive directory looking, etc but already I think a class would be better in dealing with this.
Simply changing the $fixed_columns variable, allows you to alter how many images you'll store per row. I also decided on storing the images in an array and closing the directory handler before working on displaying the information. Fixed other problems like '.' current directory and '..' parent directory as well as directory names.
PHP Code:
<?php $path = '/path/to/images/'; $fixed_columns = 4; $ext_list = array('png','jpg','gif','ico');
if($dir_handle = @opendir($path)) { $images = array(); while(false !== ($file = readdir($dir_handle))) { if($file != '.' && $file != '..' && !is_dir($file)) { foreach($ext_list as $ext) { if(substr($file,-4,4) == '.' . $ext) { $images[] = $file; } } } } closedir($dir_handle);
$row = 0;
if(count($images) > 0) { echo '<table style="margin: 0 auto; text-align: center; width: 100%;" cellspacing="0" cellpadding="0">'."\n\t".'<tr>'."\n\t"; foreach($images as $image) { if($row != 0 && $row % $fixed_columns == 0) { echo '</tr>'."\n\t".'<tr>'."\n\t"; } echo "\t".'<td style="padding-right: 10px;" class="too"><a href="http://www.yourwebsitename.com/blah"><img style="border: 1px solid #000;" src="'.$path.$image.'" width="150" height="150" alt="'.substr($image,0,-4).'" /></a></td>'."\n\t"; $row++; } echo '</tr>'."\n".'</table>'."\n"; } else { echo '<p>There are no images in this directory.</p>'; } } else { exit('<p>Error: Could not open directory</p>'); } ?>
Cheers,
MC
|
This works perfectly Fine
Great Stuff.
Thanks a Lot  
|
|
|
|
08-17-2006, 02:00 AM
|
Re: Php code help...
|
Posts: 437
Location: WebmasterGround.com
|
Well, I am going to suggest something else but glad to see this lattest code worked out for you.
|
|
|
|
08-17-2006, 02:03 AM
|
Re: Php code help...
|
Posts: 3,591
Name: Praveen
Location: Chennai, India
|
Quote:
Originally Posted by ablaye
Well, I am going to suggest something else but glad to see this lattest code worked out for you.
|
i am still all ears to other suggestions.
never know when it might come in handy 
|
|
|
|
|
« Reply to Php code help...
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|