looping problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

looping problem

Post by corillo181 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


can someone tell me how to make this work? becuase if i put it in side the while it works, but it only loops the same file.

Code: Select all

<?
$path = "/Gallery/thumbs/";
$handle = opendir($path);
$row = 3;
$col = 3;

while(false !==($file = readdir($handle))){
if($file !="." && $file !=".."){
 continue;
}
closedir($handle)
}
     for($r=1;$r <= $row; $r++){
	 echo "<tr>";
 for($c=1; $c <= $col; $c++){
 echo "<td><img src=$path/$file[]></td>";
 }
 echo "</tr>";
}

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need the for loops. What you do need is conditionals (if's) to know when and where to write out the table row ends.

Have a look at the first two threads referenced in Useful Posts.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

mm can you just put the code in a way that would work so i can then fidgure it out that would be much better..

thank you if you do :)..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

corillo181 wrote:mm can you just put the code in a way that would work so i can then fidgure it out that would be much better..

thank you if you do :)..
We can only show you the light, you have to walk the tunnel.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

thats one tunel i'm having big problems with and i know if i get that i wont need any more help cuause i havent found a way to loop tabls like that and i guess i wont be doing any better if i keep trying.. i'm stuck here it wont hurt you and it would be a move on for me :|
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

As mentioned previously, look at the first two listings in the Useful Post thread
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

thats the best i could come up with using the if , and it wont come out because i dont know ...i saw the other thread that i could do because is uusing the data base but i'm doing it while opening a directory..

Code: Select all

<table border="1">
<?
$path = "/Gallery/thumbs/";
$handle = opendir($path);
$row = 3;
$col = 3;

while(false !==($file = readdir($handle))){
if($file !="." && $file !=".."){
     if($r=1;$r <= $row; $r++){
	 echo "<tr>";
 if($c=1; $c <= $col; $c++){
 echo "<td><img src=$path/$file></td>";
 }
 echo "</tr>";
}
}
}

?>
</table>
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

now i got it to wokr but my pictures wont display :| can't help me with that either ?

Code: Select all

<table border="1">
<?
$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><img height='100' width='100' src=$path/$file></td>";
 }
 echo "</tr>";
}
closedir($handle);

?>
</table>
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

That should work

Post by dibyendrah »

Is "Gallery " the sub-folder of the current script directory ?
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

it is a sub foder the scrip is bene places on a the same level as the gallery folder so i need the /gallery to go inside of it.. but i don't see why is not displaying .. just shows [x] and if i make the pictures a <a href> it just takes me to the directory .
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

i guess they really want me to find the light on my own.. :| do you know any place where they tut about displaying pictures? becuase every code i try always something wrong with it

Code: Select all

<table border="1">
<?php
$dir = "Gallery/thumbs/";
$row=4;
$col=4;
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>";
}
?>
<table>
this one repeat the same image in every row.. :|
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You are always outputting $filename... In a loop where only $c changes...
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

timvw wrote:You are always outputting $filename... In a loop where only $c changes...
oh how do i correct that?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

By reading (and understanding): http://be2.php.net/manual/en/control-structures.for.php

Probably you want to checkout the other control structures too.
corillo181
Forum Commoner
Posts: 76
Joined: Wed Apr 26, 2006 3:02 pm

Post by corillo181 »

oh that great man exactly wha ti needed a site with exaplems 3 feet long :evil:
Post Reply