Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Help me understand why this works the first time its called and not the second

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #38865
    i3iorn
    Member

    I’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:

            
    #105973
    i3iorn
    Member

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

    #105974
    i3iorn
    Member

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

    #105977
    i3iorn
    Member

    Yes 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;

    #105984
    i3iorn
    Member

    Okay, 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.

    #106013
    mutuelle
    Member
Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.