Solved

Variable inside function

Posted on 2011-03-17
4
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

752 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