Jump to content


Photo

[SOLVED] opendir


  • Please log in to reply
10 replies to this topic

#1 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 27 April 2006 - 04:33 AM

why those this doesn't show up the images?
<?
$path = "/Gallery/thumbs/";
$handle = opendir($path);
$row = 3;
$col = 3;

while(false !==($file = readdir($handle))){
if($file !="." && $file !=".."){
 continue;
 }
     for($r=1;$r <= $row; $r++){
     echo "<tr>";
}

 for($c=1; $c <= $col; $c++){
 echo " <td><a href=$path/$file><img height=\"100\" width=\"100\" src=$path/$file></a></td> ";
 }
 echo "</tr>";
}
closedir($handle);


?>
</table>


#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 27 April 2006 - 11:31 AM

This will display all files of the type you specify in the directory you specify.
Using *.jpg will display all jpg files.. obviously.

<?php
foreach (glob("/Gallery/thumbs/*.jpg") as $filename) 
{
echo "<IMG SRC='$filename' ALT='$filename'><br>";
}
?>

wisewood: proven fact, I am both wise, and wooden.

#3 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 27 April 2006 - 06:06 PM

i used that code and tried to use it with a table and it returns the amount of pictures per row the same one, if i put 5 rows it show the first pitures 5 times than the next row 5 pics of the same and so on..

<table border="1">
<?php
$dir = "Gallery/thumbs/";
$row=2;
$col=2;
foreach (glob("$dir/*.jpg") as $filename) 
{
for($r=1;$r <= $row; $r++){
     echo "<tr>";
}
for($c=1; $c <= $col; $c++){
echo "<td><IMG SRC='$filename'></td>";
}
echo "</tr>";
closedir($dir);
}

?>
<table>



#4 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 27 April 2006 - 08:02 PM

man can someone just answer this question!

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 April 2006 - 09:06 PM

As I understand your question, you're trying to display your pictures n to a row for as many rows it takes.

The reason the current code doesn't work is that the "for" loops are within the foreach loop that select the picture.

Try this:

<?php
$cols = 5;
$cnt = 0;
$tmp = array();
$tmp2 = array();
foreach (glob("$dir/*.jpg") as $filename) {
    $tmp2[] = '<td><IMG SRC="' . $filename . '"></td>';
    $cnt++;
    if ($cnt % $cols == 0) {
        $tmp[] = implode('',$tmp2);
        $tmp2 = array();
    }
}
echo "<tr>\n" . implode("</tr>\n<tr>",$tmp) . "</tr>\n";
?>

The "%" is the [a href=\"http://us2.php.net/manual/en/language.operators.arithmetic.php\" target=\"_blank\"]modulus operator[/a]
This code will take each filename and put the string '<td><img ... ></td>' into the array $tmp2. When the number of pictures processed divided by the number of columns is zero, use implode to combine those strings into one and put it into the $tmp array.

When there are no more pictures to process, use the implode() function to put the string "</tr>\n<tr>" between those strings.

Ken

You also can remove the "closedir($dir)" statement.

Ken

#6 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 27 April 2006 - 09:40 PM

yeaaaaaaaaaaaaaaaaaaaaaaaaaaah.. thats what i was looking for.. :-D bout time.. thank you very much ken :)

#7 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 27 April 2006 - 09:59 PM

oh wait one last thing.. how do i do to break it for another row..i try my ways but i'm getting no where..hope i'm not been to muhc trouble :|

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 April 2006 - 11:53 PM

What do you mean?

Ken

#9 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 28 April 2006 - 12:37 AM

[!--quoteo(post=369399:date=Apr 27 2006, 07:53 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 27 2006, 07:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What do you mean?

Ken
[/quote]
like if i have 6 pictures that are showing in a row.. and i want then to show and different rows like
this is how they show |1|1|1|1|1|1|
and this is wha yti'm trying to get

|1|1|
|1|1|
|1|1|
in 3 rows..

like with the td the more pictures i add the line get larger, i want to breake it in to rows..

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 April 2006 - 03:47 AM

In my code, just make the variable "$cols" to be equal to the number of columns you want.

I just found a small bug with my code while testing to make sure the above worked. :-)

Just before this line
<?php echo "<tr>\n" . implode("</tr>\n<tr>",$tmp) . "</tr>\n"; ?>
Add the line
<?php if (!empty($tmp2)) $tmp[] = implode('',$tmp2); ?>

This takes care of any pictures on the last line.

Ken

#11 corillo181

corillo181
  • Members
  • PipPipPip
  • Advanced Member
  • 896 posts

Posted 28 April 2006 - 04:34 AM

yeah that solves the problem.. well thank you very much man :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users