Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop for array

Status
Not open for further replies.

buzzt

Programmer
Joined
Oct 17, 2002
Messages
171
Location
CA
I need a way to make a loop for the section in red below. I tried to write a loop, but it won't allow it. The $reqfiles is an array of 4 names ($reqfiles = array("1.php", "2.php", "3.php", "4.php");) specified earlier in the code which I would like to add to in the future without having to add another of the lines in red each time.

Any ideas?

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkFileName($f) {
global $subdir, $thisfile, $hidedotfiles, $hidefilepattern, $trashcan, $trashcaninfofileext, $showimagesdir, $imagesdir, $readmefile, $loginfile, $logoutfile, $showreadmefile, $showloginfile, $showlogoutfile, $filealiases, $filealiasext,$reqfiles,$showreqfiles;

$f = basename($f);

return !( (($subdir == "") && (strtolower($f) == $thisfile))
|| (($subdir == "") && ($f == $trashcan))
|| (!$showimagesdir && ((($subdir == "") && ($f == $imagesdir)) || ($subdir == $imagesdir)))
|| ($hidedotfiles && ($f[0] == '.'))
|| (($hidefilepattern != "") && ereg($hidefilepattern, $f))
|| ($filealiases && ereg("^.*\.".strtolower($filealiasext)."$", strtolower($f)))
|| ($f == "..")
|| (!$showreadmefile && ($f == $readmefile)) // hides the readme file
|| (!$showloginfile && ($f == $loginfile)) // hides the login file
|| (!$showlogoutfile && ($f == $logoutfile)) // hides the logout file
|| (!$showreqfiles && ($f == $reqfiles[0]))
|| (!$showreqfiles && ($f == $reqfiles[1]))
|| (!$showreqfiles && ($f == $reqfiles[2]))
|| (!$showreqfiles && ($f == $reqfiles[3]))

|| (($subdir == $trashcan) && (($f == $readmefile) || ereg(".*\.".strtolower($trashcaninfofileext)."$", strtolower($f)))) );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
 
I'd do something like this, and then work this function into that statement...
Code:
function separateTest($f, $reqfiles) {
  foreach ($reqfiles as $file) {
    if ($f == $file) {
      ..
    } else {
      ..
    }
  }

  return whatever variable I set;
}

That is if I ever made such a crazy return statement that's how I would do it.

Personally I think the better way to do those kind of complex expressions is to write your boolean value to a variable, and then return that variable, that way if you need to break out one section like this it is trivial, rather than the situation you're in now.

-Rob
 
I'd do something like this:

[tt]function checkFileName($f) {
global $subdir, $thisfile, $hidedotfiles, $hidefilepattern, $trashcan, $trashcaninfofileext, $showimagesdir, $imagesdir, $readmefile, $loginfile, $logoutfile, $showreadmefile, $showloginfile, $showlogoutfile, $filealiases, $filealiasext,$reqfiles,$showreqfiles;

$f = basename($f);

$return_value =
(($subdir == "") && (strtolower($f) == $thisfile))
|| (($subdir == "") && ($f == $trashcan))
|| (!$showimagesdir && ((($subdir == "") && ($f == $imagesdir)) || ($subdir == $imagesdir)))
|| ($hidedotfiles && ($f[0] == '.'))
|| (($hidefilepattern != "") && ereg($hidefilepattern, $f))
|| ($filealiases && ereg("^.*\.".strtolower($filealiasext)."$", strtolower($f)))
|| ($f == "..")
|| (!$showreadmefile && ($f == $readmefile)) // hides the readme file
|| (!$showloginfile && ($f == $loginfile)) // hides the login file
|| (!$showlogoutfile && ($f == $logoutfile)) // hides the logout file
|| (($subdir == $trashcan) && (($f == $readmefile) || ereg(".*\.".strtolower($trashcaninfofileext)."$", strtolower($f))));

foreach ($reqfiles as $filename)
{
$return_value |= (!$showreqfiles && ($f == $filename));
}

return !($return_value);

}[tt]


Want the best answers? Ask the best questions: TANSTAAFL!
 
That works great! Thanks a bunch.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top