Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-15-2006, 10:48 AM Php code help...
praveen's Avatar
Life is a Dream

Posts: 3,591
Name: Praveen
Location: Chennai, India
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
praveen is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-15-2006, 12:22 PM Re: Php code help...
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
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..
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Old 08-15-2006, 12:30 PM Re: Php code help...
ChancesAre's Avatar
Skilled Talker

Posts: 84
Trades: 0
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
ChancesAre is offline
Reply With Quote
View Public Profile
 
Old 08-15-2006, 12:33 PM Re: Php code help...
praveen's Avatar
Life is a Dream

Posts: 3,591
Name: Praveen
Location: Chennai, India
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
praveen is offline
Reply With Quote
View Public Profile
 
Old 08-15-2006, 09:49 PM Re: Php code help...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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..
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-16-2006, 12:11 AM Re: Php code help...
praveen's Avatar
Life is a Dream

Posts: 3,591
Name: Praveen
Location: Chennai, India
Trades: 0
Quote:
Originally Posted by mgraphic View Post
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
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
praveen is offline
Reply With Quote
View Public Profile
 
Old 08-16-2006, 12:02 PM Re: Php code help...
praveen's Avatar
Life is a Dream

Posts: 3,591
Name: Praveen
Location: Chennai, India
Trades: 0
any one?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
praveen is offline
Reply With Quote
View Public Profile
 
Old 08-16-2006, 12:36 PM Re: Php code help...
OmuCuSucu's Avatar
Vi Veri Veniversum Vivus

Posts: 1,168
Name: Dragos-Valentin
Location: Cluj-Napoca, RO
Trades: 0
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
OmuCuSucu is offline
Reply With Quote
View Public Profile
 
Old 08-16-2006, 11:00 PM Re: Php code help...
Ultra Talker

Posts: 256
Location: Auckland, New Zealand
Trades: 0
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 != && $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.
mastercomputers is offline
Reply With Quote
View Public Profile Visit mastercomputers's homepage!
 
Old 08-16-2006, 11:14 PM Re: Php code help...
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Quote:
Originally Posted by mastercomputers View Post
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.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 08-16-2006, 11:35 PM Re: Php code help...
praveen's Avatar
Life is a Dream

Posts: 3,591
Name: Praveen
Location: Chennai, India
Trades: 0
Quote:
Originally Posted by mastercomputers View Post
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 != && $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
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
praveen is offline
Reply With Quote
View Public Profile
 
Old 08-17-2006, 02:00 AM Re: Php code help...
ablaye's Avatar
Ultra Talker

Posts: 437
Location: WebmasterGround.com
Trades: 5
Well, I am going to suggest something else but glad to see this lattest code worked out for you.
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
ablaye is offline
Reply With Quote
View Public Profile Visit ablaye's homepage!
 
Old 08-17-2006, 02:03 AM Re: Php code help...
praveen's Avatar
Life is a Dream

Posts: 3,591
Name: Praveen
Location: Chennai, India
Trades: 0
Quote:
Originally Posted by ablaye View Post
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
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
praveen is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Php code help...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37688 seconds with 12 queries