Home › Forums › Back End › Help me understand why this works the first time its called and not the second
- This topic is empty.
-
AuthorPosts
-
July 12, 2012 at 3:51 pm #38865
i3iorn
MemberI’ve just recently started learning php and its going rather well. I have created a script that goes through the files in a folder and looks for files matching a few conditions. It then creates list items in a menu. It works perfectly for my main navigation. But when I call it to create a secondary menu it returns an empty array instead of the file names as it is supposed to.
Variables and includes
$page=$_GET;
$path=opendir('/var/www/pages');
include 'script/php/get_files.php';
include "script/php/nav.php";
?>
Here are the two functions that do the heavy lifting
function get_files ($path,$prefix,$needle) {
$files=array();
unset($filename);
while (false !== ($filename = readdir($path)) ) {
if (false !== strpos($filename,$prefix) && false == strpos($filename,".swp")) {
$filename = str_replace(".php","",$filename);
$filename = str_replace($prefix,"",$filename);
if (isset($needle)) {
strstr($filename,$needle);
}
array_push($files,$filename);
}
}
return $files;
}
?>
function nav($path,$prefix,$page) {
$files = array();
if(!isset($path) || !isset($prefix)){ echo "Could not load menu"; break;}
$files = get_files($path,$prefix);
sort($files);
foreach ($files as $document) {
if( isset($page) ) {
if ( $page == $document ) {
$class="selected";
}
else {
$class=" ";
}
}
$class = "";
$adress=$_SERVER;
echo "- ".$document."
";
}
}
?>
And the part that calls for the nav() function:
July 12, 2012 at 3:52 pm #105973i3iorn
MemberThe first call to nav() generates three filenames as I expect it to. The second call returns empty though I have double and tripple checked that I have two files that should match.
July 12, 2012 at 3:55 pm #105974i3iorn
MemberFeel free to suggest other changes that could be made to reduce the ammount of code or if there are better, safer, ways to write any of this. I know its alot of code but I would be very greatfull.
July 12, 2012 at 4:04 pm #105977i3iorn
MemberYes sorry forgott to include that.
$root = $path;
It was a part of my attempt to troubleshoot and I forgott to change $root back to $path;
July 12, 2012 at 4:47 pm #105984i3iorn
MemberOkay, that will put me on the right track. I was thinking about rewriting the entire thing anyway because it started out as 4 line function and I’ve just put stuff in and try to make it work.
Thank you so much.
July 13, 2012 at 1:14 am #106013 -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.