Solved

Variable inside function

Posted on 2011-03-17
4
250 Views
Last Modified: 2012-06-21
I have a function which lists files in a directory, the first line of my function is as follows:

function listdirectory($c = 'docs/r0jd38dd', $wcwd = false) {

$c is the directory to list. I now want it so that I can pass the folder to the function as the directory will change depending on the page. For this I've tried:

$bookid = 'docs/' . $_GET['id'];
function listdirectory($c = $bookid, $wcwd = false) {

but it doesn't work, where am I going wrong?

Thanks
0
Comment
Question by:djfenom
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 35155893
Functions work like this:

function listdirectory($c, $wcwd) {
    echo $c . '<br />;
    echo $wcwd . '<br />;
}

$bookid = 'docs/' . $_GET['id'];
listdirectory($bookid, false);

Open in new window


And check the manual for some more explanation:
http://php.net/manual/en/language.functions.php
0
 

Author Comment

by:djfenom
ID: 35155967
Hmmm, this is my full function, how would I put it in?
<?php
  $bookid = 'docs/r0jd' . substr($_GET['id'],-2) .'dd';
  function listdirectory($c = $bookid, $wcwd = false) {

                if($wcwd === false)

                  $wcwd = substr($wcwd = $_SERVER['REQUEST_URI'], 0, strrpos($wcwd, '/') + 1);

 

                echo('<ul class="dirlist">');

                $arr = scandir($c);
	        asort( $arr );
                foreach( $arr as $f ) {
				if ($f != 'User guides') {
                  if(strpos($f, '.') === 0) continue;

 

                  $ff = $c . '/' . $f;

                  $folder = substr($c, 14);

                  if(!is_dir($ff)) {

                                  if ($f == 'Thumbs.db' || $f == 'hr_handbook.zip') {

                                  }

                                  else {

                                                  echo '<li><a href="/docs/?file=' . $folder . '/' . $f . '" class="docdl">' . $f . '</a>';

                                  }

                  }

                  else {

                                        echo '<li><strong>' . $f . '</strong>';

                  }

                  if(is_dir($ff)) listdirectory($ff, $wcwd);

                  echo '</li>';

                }

				}
                echo('</ul>');
  } 

listdirectory();
?>

Open in new window

0
 
LVL 8

Accepted Solution

by:
Rik-Legger earned 500 total points
ID: 35155982
<?php
  
  function listdirectory($c, $wcwd) {

                if($wcwd === false)

                  $wcwd = substr($wcwd = $_SERVER['REQUEST_URI'], 0, strrpos($wcwd, '/') + 1);

 

                echo('<ul class="dirlist">');

                $arr = scandir($c);
	        asort( $arr );
                foreach( $arr as $f ) {
				if ($f != 'User guides') {
                  if(strpos($f, '.') === 0) continue;

 

                  $ff = $c . '/' . $f;

                  $folder = substr($c, 14);

                  if(!is_dir($ff)) {

                                  if ($f == 'Thumbs.db' || $f == 'hr_handbook.zip') {

                                  }

                                  else {

                                                  echo '<li><a href="/docs/?file=' . $folder . '/' . $f . '" class="docdl">' . $f . '</a>';

                                  }

                  }

                  else {

                                        echo '<li><strong>' . $f . '</strong>';

                  }

                  if(is_dir($ff)) listdirectory($ff, $wcwd);

                  echo '</li>';

                }

				}
                echo('</ul>');
  } 

$bookid = 'docs/r0jd' . substr($_GET['id'],-2) .'dd';
listdirectory($bookid, false);
?>

Open in new window

0
 

Author Comment

by:djfenom
ID: 35156007
Perfect, thanks!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question