Forum Moderators: coopster

Message Too Old, No Replies

search array until file valid is found

         

sssweb

8:40 pm on Feb 13, 2006 (gmt 0)

10+ Year Member



I have a script that searches an array for a particular filename. My array looks like:

$array = array(
'file1.htm',
'file2.htm',
'file3.htm'
);

The script searches the array for a particular file ($filename), and returns the array value after that:

$array[array_search($filename, $array) + 1]

I need to test whether the new value is on my system, and if not, cycle through the array until I find one that is. I'm using this statement to test the condition:

if (!file_exists('/home/mysite.com/public_html/'.$array[array_search($filename, $array) + 1])) {
die("");
}

This tells the script to die if the next file after $filename isn't found, but I want it to cycle all the way through the array until it finds a file on my system. Cycle should start at $filename + 1 and start again at the beginning of the array if it reaches the end.

sssweb

4:23 pm on Feb 14, 2006 (gmt 0)

10+ Year Member



I found something that seems to work. If anyone sees an error, let me know:

$start = array_search($currentpage, $array);
foreach ($array as $key => $value) {
if ($key > $start) {
if (file_exists('/home/mysite.com/public_html/' . $value)) {
header('location: [mysite.com...] . $value);
exit();
}
}
}

(This doesn't restart at the beginning when it reaches the end, but I've worked around that.)

coopster

4:46 pm on Feb 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How about just adding else logic to your if control logic? Or, better yet, build the file listing that the user will select options from first, display that, and then let the user select?