Jump to content

Archived

This topic is now archived and is closed to further replies.

corillo181

[SOLVED] opendir

Recommended Posts

why those this doesn't show up the images?
[code]<?
$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>[/code]

Share this post


Link to post
Share on other sites
This will display all files of the type you specify in the directory you specify.
Using *.jpg will display all jpg files.. obviously.

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

Share this post


Link to post
Share on other sites
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..

[code]<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>

[/code]

Share this post


Link to post
Share on other sites
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:

[code]<?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";
?>[/code]

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

Share this post


Link to post
Share on other sites
yeaaaaaaaaaaaaaaaaaaaaaaaaaaah.. thats what i was looking for.. :-D bout time.. thank you very much ken :)

Share this post


Link to post
Share on other sites
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 :|

Share this post


Link to post
Share on other sites
[!--quoteo(post=369399:date=Apr 27 2006, 07:53 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 27 2006, 07:53 PM) [snapback]369399[/snapback][/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..

Share this post


Link to post
Share on other sites
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 [code]<?php echo "<tr>\n" . implode("</tr>\n<tr>",$tmp) . "</tr>\n"; ?>[/code]
Add the line [code]<?php if (!empty($tmp2)) $tmp[] = implode('',$tmp2); ?>[/code]

This takes care of any pictures on the last line.

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.