homepage Welcome to WebmasterWorld Guest from 54.196.197.153
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
search folders with array name
ahmed24




msg:4020550
 6:12 pm on Nov 6, 2009 (gmt 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

 

JohnCanyon




msg:4020675
 9:00 pm on Nov 6, 2009 (gmt 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 =)

ahmed24




msg:4020772
 11:43 pm on Nov 6, 2009 (gmt 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

ahmed24




msg:4020804
 1:01 am on Nov 7, 2009 (gmt 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

TheMadScientist




msg:4020812
 1:36 am on Nov 7, 2009 (gmt 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);
}
}
}

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.

ahmed24




msg:4020915
 10:12 am on Nov 7, 2009 (gmt 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

TheMadScientist




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

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 */
}

ahmed24




msg:4020944
 12:08 pm on Nov 7, 2009 (gmt 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?

ahmed24




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

nevermind, i have done it. many thanks

JohnCanyon




msg:4021085
 10:08 pm on Nov 7, 2009 (gmt 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.

TheMadScientist




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved