Welcome to WebmasterWorld Guest from 54.162.136.26

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

search folders with array name

     
6:12 pm on Nov 6, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


I have an array called: $folders in it i have defined some folder names like this:

$folders=array("folder1","folder2","folder3","folder4","folder5");

Only one of these folders will contain a file called data.csv and i want to know how i can get php to tell me which one but starting from folder3 onwards (skip folder 1 and 2)

can anyone please tell me how i can do this?

thanks

9:00 pm on Nov 6, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 10, 2004
posts:126
votes: 0



for($i=2;$i<count($folders);$i++) {
if(is_dir($folders[$i])) {
if($dh = opendir($folders[$i])) {
while(($file = readdir($dh)) !== false) {
if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}
}
closedir($dh);
}
}
}

Albeit.. not tested =)
11:43 pm on Nov 6, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


thanks for that, works very well. can anyone tell me how i can make that code stop checking any further once it finds the data.csv file? i dont want it to continue to look in other directories after it has already located the data.csv file

thanks

1:01 am on Nov 7, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


it seems if i put something like exit; or die; where the break; is, then it terminates at the first found result which is what i want, but it also terminates the rest of my html code from loading. so can anyone please tell me how i can do this?

thanks

1:36 am on Nov 7, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62



for($i=2;$i<count($folders);$i++) {
if(is_dir($folders[$i])) {
if($dh = opendir($folders[$i])) {
while(($file = readdir($dh)) !== false) {
if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}
}
closedir($dh);
}
}
}

You've got two loops and one break()...
(This can be made more efficient after testing.)


for($i=2;$i<count($folders);$i++) {
if(is_dir($folders[$i])) {
if($dh = opendir($folders[$i])) {
while(($file = readdir($dh)) !== false) {
if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}
}
closedir($dh);
[b]if($file == 'data.csv') {
echo "Eureka! I found data.csv in {$folders[$i]}";
break;
}[/b]
}
}
}

@ JC: How are you getting the spacing for code on the forum? I tried using the code formatting and (obviously) it didn't work... How are you indenting? I don't want to have to find it myself (gettin' lazy or something). lol.

10:12 am on Nov 7, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


thats great.

one last thing i hope someone can help with. this code is checking forward from folder 2 onwards, how about if i wanted to check backwards from folder lets say 4. So in other words not check anything after folder4 but check folder3, 2, and 1 ?

many thanks

11:42 am on Nov 7, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


Change the counter to work in reverse... Something to the effect of the following for the for() statement should be close:

for($i=3;$i>=1;$i--) {
/* Same as above here */
}

12:08 pm on Nov 7, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


thanks for your reply, so if i want it to check backwards from folder 4 i put

for($i=3

and if i want it to check backwards from folder 5 i put:

for($i=4

is that correct?

if so then i cant seem to get it to work when it comes to folders lower than 4, so if i want it to check before folder 3 i would put

for($i=2

but that doesnt work

any ideas?

2:10 pm on Nov 7, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:July 17, 2006
posts:137
votes: 0


nevermind, i have done it. many thanks
10:08 pm on Nov 7, 2009 (gmt 0)

Junior Member

10+ Year Member

joined:Sept 10, 2004
posts:126
votes: 0


@ahmed24 - Glad I was able to get you started.

@TheMadScientist - I use pre in brackets and spaces, still not perfect, but better than nothing. I am surprised they haven't added code formatting. Oh well.

Cheers.

11:30 pm on Nov 7, 2009 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member themadscientist is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 14, 2008
posts:2910
votes: 62


@ JC Thanks!

I am surprised they haven't added code formatting. Oh well.

AND: I actually sent Brett a sticky about it, and he said it was in the works for the next version, so we should be able to indent easily at some point in the future...

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members