Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Variable inside function

Posted on 2011-03-17
4
Medium Priority
?
273 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

886 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