Grow your CSS skills. Land your dream job.

Help me understand why this works the first time its called and not the second

  • # July 12, 2012 at 3:51 pm

    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


    < ?php
    $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

    < ?php
    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;
    }
    ?>

    < ?php
    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

    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.

    # July 12, 2012 at 3:55 pm

    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.

    # July 12, 2012 at 4:00 pm

    Do you declare $root anywhere?

    # July 12, 2012 at 4:04 pm

    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;

    # July 12, 2012 at 4:42 pm

    $filename is just a boolean, so unset($filename) doesn’t do what you think it does. You need to use closedir() to reset your readdir() function, which means you need to rethink when you use opendir().

    # July 12, 2012 at 4:43 pm
    < ?php
    $page=$_GET;
    $path='/var/www/pages';

    function get_files ($path,$prefix,$needle) {
    $files=array();
    $handle = opendir($path);
    unset($filename);
    while (false !== ($filename = readdir($handle) ) ) {
    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);
    }
    }
    closedir($handle);
    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."
  • ";
    }
    }
    ?>

    # July 12, 2012 at 4:47 pm

    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.

    # July 13, 2012 at 1:14 am

    mutuelle

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".