Welcome! Log In Create A New Profile

Advanced

Topic 3B-2

Posted by NicholasP 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Topic 3B-2
August 03, 2006 04:14PM
I have this code:
if (!$dir) 
$dir = ("./"winking smiley;
$dp = opendir($dir);

echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>\n";

while ($filenames[] = readdir($dp));

sort($filenames);
	
for ($i = 0; $i < count($filenames); $i++) { //For loop start
	$item = $filenames[$i];

	if (is_dir($item))	{
	
		if ($item != '.' && $item != '..'winking smiley
			echo "<tr><td width='100%'><a href='$PHP_SELF?dir=$dir$item'>$item/</a></td></tr>\n";
		}  
	else 
		echo "<tr><td width='100%'><a href='$dir/$item'>$item</a></td></tr>\n";	
	} //End for loop
 ?>
</table>

1. I get the directory listing, but when I click on e.g. /Test, nothing happens.

2. The book states that the while loop reads entries into the array... is the while loop that does this that single while statement????

3. I am getting confused with the {} brackets.
Re: Topic 3B-2
August 03, 2006 08:54PM
Hi Nicholas

Try the code as follows:

<?php
if (isset($_GET['dir'])) // If GET variable has been passed, use it
$dir = $_GET['dir'];

if (!isset($dir)) // If the $dir varable hasn't yet been initialised, initialise it
$dir = ("./"winking smiley;
$dp = opendir($dir);

echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>\n";

while ($filenames[] = readdir($dp));

sort($filenames);

for ($i = 0; $i < count($filenames); $i++)
{ //For loop start
$item = $filenames[$i];

if (is_dir($item)) {

if ($item != '.' && $item != '..'winking smiley
echo "<tr><td width='100%'><a href='{$_SERVER['PHP_SELF']}?dir=$dir$item'>$item/</a></td></tr>\n"; //New way of using PHP_SELF
}
else
echo "<tr><td width='100%'><a href='$dir/$item'>$item</a></td></tr>\n";

} //End for loop
?>

Regarding your questions:

1. The above code should work. There were some issues in the old code, mainly PHP version related. Compare the old with the new code and you will spot the differences. If you still are unsure of anything, please let me know and I'll explain further.

2. Yes, it is the single "while" statement. readdir is a function that will continue to return each directory entry until there are none left, then it will return null. So the "while" loop will continue populating the $filenames array until the readdir function runs out of directory entries and returns null.

3. I think that the reason you are getting confused with the squigly brackets is because you don't have to include them if you are only performing one task within the loop. For example:

while ($filenames[] = readdir($dp));

is the same as:

while ($filenames[] = readdir($dp)){
};

Note that the squigly brackets aren't needed because all the work is being done inside the "while" statement.

Another example:

if (!$dir)
$dir = ("./"winking smiley;

is the same as:

if (!$dir) {
$dir = ("./"winking smiley;
}

As a personal preference, I much prefer structuring the squigly brackets as:

if (!$dir)
{
$dir = ("./"winking smiley;
}

That way, all the squigly brackets line up nicely and the code is easier to read, especially if you have nested loops. But that's just me, if the other way works for you, go for it.

Regards,
Dennis






Re: Topic 3B-2
August 04, 2006 07:53AM
Thanks! smiling smiley

Some more questions:
1. Is this an example of a recursive PHP script (the PHP_SELF bit)?
2. I thought the $_GET; $_POST is only used when you pass stuff from a form. I take it $_GET/$_POST is also needed when a script calls itself?
Re: Topic 3B-2
August 04, 2006 08:27AM
No Problem smiling smiley

1. Yup. You could also replace the PHP_SELF bit with the name of the page, it will do the same thing.

2. A form, yes. And also may be used with a hyperlink as in this case.

If you look at the html generated by the sript, note the the GET varable being passed each time, eg:

<a href='testpage.php?dir=./Test'>Test/</a>
Re: Topic 3B-2
August 23, 2006 09:13AM
Completed.
lad
Re: Topic 3B-2
August 28, 2006 01:57PM
Topic completed.
Re: Topic 3B-2
August 30, 2006 04:41PM
Topic 3B-2 completed.
avatar Re: Topic 3B-2
September 11, 2006 10:01PM
done done
Re: Topic 3B-2
September 17, 2006 09:04PM
3B completed
Re: Topic 3B-2
September 18, 2006 08:01PM
Completed
Sorry, only registered users may post in this forum.

Click here to login