Jump to content

Dynamic Navigation Menu


zucler

Recommended Posts

Hi guys!

 

I need a php page that is splitted into 2 screens. One will display the navigation menu and the second one is the iframe.

 

As for the navigation menu, I am getting the list of all files in the current directory with readdir(). Then I create an array with all of the items that are folders (is_dir() function) and apply the same script on them. My problem is to correctly display all of the information I gather in an adequate manner. Can anyone help me with that? Also if anyone has ready-to-go scripts, I would highly appreciate that too.

 

Also, once a user choose a file, it should be loaded in the iframe. How do I specify "src" attribute for the iframe, in that case?

 

Thanks!

Link to comment
Share on other sites

Googling for "how to replace iframe content" gave this, which answers your second question: http://www.gotfusion.com/tutorials/tut.cfm?itemID=327

 

If you make an attempt at the navigation menu we might be able to help.  Otherwise all I can suggest for that is to look for tutorials in google, eg "display table in php" or "display html list in php"

Link to comment
Share on other sites

Well, I am really not sure if I chose the appropriate algorithm. I will highly appreciate if anyone could look at my source code and help me out. Well, at least tell me if I can finish the script in the way I started it.

 

The problems with the script so far are:

 

[*]It only displays folders at the first level. (That's not a big issue. I think I know how to fix it)

[*]If you click on a folder, it will display its' contents under ALL of the folders, not only under the specific one. I'm not sure what condition I can use to fix that.

[*]It only scans one sublevel. For example it scans folder/subfolder. And I need it to scan any number of sub-levels, such as folder/subfolder/subfolder/subfolder/...

 

So yeah, again, I'm not even sure if I can finish it with the algorithm I've chosen. I will highly appreciate any help!

 

Thanks in advance!

 

 

The source code is:

 

        <?php
            error_reporting( E_ALL & E_STRICT );
            read(".");

            // This function reads the directory specified, creating an array with all files and folders.
            function read($path) {
                if ($dir = opendir($path)) {
                    while (false !== ($file = readdir($dir))) {
                        if ($file != "." && $file != ".." && $file != ".DS_Store") { // These files will not be displayed
                            array($m_list[] = $file); // Creates a list of all objects.
                        }
                    }
                }
                closedir($dir);
                $n=0; // n represents the $key value of a particular folder inside $m_list array.
                foreach ($m_list as $m_object) {
                    if (is_dir($m_object)) { // Check if current object is a directory.
                        $dir_link = $m_object;
                        $headers[] = array($m_object => "index.php?s=". $n); // Assign a unique link for each of the folders.
                        $sublist[$n] = subdir($m_list, $dir_link); // Acquire the list of all objects inside a folder.
                    }
                    $n++;
                }
                display($headers, $sublist); // Display the results.
            }

            // This function aquires a complete list of all objects inside a folder and links to them.
            function subdir($m_list, $dir_link) {
                $path = $dir_link;
                if ($dir = opendir($path)) {
                    while (false !== ($file = readdir($dir))) {
                        if ($file != "." && $file != ".." && $file != ".DS_Store") {
                            $sublist[$file] = "/".$dir_link."/".$file; // For example if file me.html was inside main/sub/ folder then the value will be main/sub/me.html
                        }
                    }
                }
                closedir($dir);
                return ($sublist);
            }

            //This function displays the results
            function display($headers, $sublist) {
                $s = $_GET['s']; // Aqcuire a sequence number of a folder the user wants to display.
                foreach ($headers as $count) {
                    foreach ($count as $key => $value){
                        echo "<a href = $value>$key</a><br>";
                        if ($s != null && $s >= 0) { // Check if this folder should be expanded.
                            foreach ($sublist as $one => $two) {
                                if ($one == $s) {
                                    foreach ($two as $kkey => $vvalue) {
                                        echo "<ul><a href = $vvalue>$kkey<a><br></ul>";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        ?>

 

P.S. you should name the file index.php in order for script to work.

P.P.S. That's my very first script, so sorry if there is something really silly  :D

Link to comment
Share on other sites

It's a good idea to terminate the script with die() or exit() if something doesn't work.

 

"1. It only displays folders at the first level. (That's not a big issue. I think I know how to fix it)" - What is it supposed to do instead?

"2. If you click on a folder, it will display its' contents under ALL of the folders, not only under the specific one. I'm not sure what condition I can use to fix that." - How does the link clicked on by the user tell your script which folder to use?  Eg the link could be "/?path=foo/bar", and then the path is available as $_GET['path'] or $_REQUEST['path'].  Then you can use that in your code.

"3. It only scans one sublevel. For example it scans folder/subfolder. And I need it to scan any number of sub-levels, such as folder/subfolder/subfolder/subfolder/... " - Do you need to scan every level every time?  If so, does that mean you are displaying links to every subfolder on a single page?

 

Link to comment
Share on other sites

"1. It only displays folders at the first level. (That's not a big issue. I think I know how to fix it)" - What is it supposed to do instead?

It supposed to display both folders and files.

 

 

"2. If you click on a folder, it will display its' contents under ALL of the folders, not only under the specific one. I'm not sure what condition I can use to fix that." - How does the link clicked on by the user tell your script which folder to use?  Eg the link could be "/?path=foo/bar", and then the path is available as $_GET['path'] or $_REQUEST['path'].  Then you can use that in your code.

 

Thanks, I'll try this one.

 

"3. It only scans one sublevel. For example it scans folder/subfolder. And I need it to scan any number of sub-levels, such as folder/subfolder/subfolder/subfolder/... " - Do you need to scan every level every time?  If so, does that mean you are displaying links to every subfolder on a single page?

 

Well, all I need is to have a directory list. So I guess it's good to scan one level at a time (e.g. at a user request).

I'm trying to make the folder link to be expandable. So not to display all link on a single page.

 

Link to comment
Share on other sites

OK, I think the "displays folder only" issue may be here:

 

                foreach ($m_list as $m_object) {
                    if (is_dir($m_object)) { // Check if current object is a directory.
                        $dir_link = $m_object;
                        $headers[] = array($m_object => "index.php?s=". $n); // Assign a unique link for each of the folders.
                        $sublist[$n] = subdir($m_list, $dir_link); // Acquire the list of all objects inside a folder.
                    }
                    $n++;
                }

 

Here you have a condition for is_dir(), but there is no "else" branch to handle the things which are not folders.

 

I think you general algorithm should be:

 

1.  Get path from user input, or default to "/"

2.  Get list of all folders and files in that path

3.  Display them (making sure folders are links which include the full path name to that folder).

 

Unless you want those folder links to be expandable without a request back to the server.  IE the expansion and collapsing is done in javascript, and the entire directory structure is provided to the web browser with each request.  If that's the case then you do need to read them all..  If that is what you want to do, then your subdir() is going to need another call back to either subdir() or read(), so it can continue going down the levels until it reaches the end.

 

By far the simplest option is to send the current path back to PHP and have it generate the list for just that path.

Link to comment
Share on other sites

Thanks for the help mate!

 

I have solved this one, here is the code, just in case anyone needs it too.

 

        <?php
            error_reporting( E_ALL & E_STRICT );
            read(".", "");

            // This function  reads the directory specified, creating an array with all files and folders.
            function read($path, $begin) {
                $folder = scandir($path);
                foreach ($folder as $file) {
                    if ($file=="." or $file==".." or $file==".DS_Store") continue; // These files will not be displayed
                    $f0 = $path."/".$file;
                    $link = create_link($f0);
                    echo "<a href=$link>".$begin.$file."</a>", "<br>";
                    $s = $_GET['s'];
                    $p = $_GET['p'];
                    if (is_dir($f0) && $p != null ) {
                        if (strlen (stristr($p,"./"))==0) continue;
                        if (strlen(stristr($p, $f0))!=0) {
                            read($f0, $begin."  ");
                            if ($f0 == $p) continue;
                        }
                    }
                    if (is_file($f0) && $s != null) {
                        $tag = substr($s, 2);
                        echo "the link is ".$tag."<br>";
                        break;
                    }
                }
            }

            function create_link($f0) {
                if (is_file($f0)) {
                    $s0= "index.php?s=".$f0;
                    return $s0;
                }
                if (is_dir($f0)) {
                    $p0 = "index.php?p=".$f0;
                    return $p0;    
                }
            }
        ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.