Welcome to WebmasterWorld Guest from 174.129.135.89

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

search folders with array name

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

5+ Year Member



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)

10+ Year Member




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)

5+ Year Member



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)

5+ Year Member



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)

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




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)

5+ Year Member



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)

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



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)

5+ Year Member



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)

5+ Year Member



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

10+ Year Member



@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)

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



@ 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...