mechanics

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
b00f3d
Forum Newbie
Posts: 1
Joined: Mon Oct 21, 2002 4:34 pm
Location: Kentucky (buh)
Contact:

mechanics

Post by b00f3d »

<?

$num = 0;

if(!isset($count)) {$count = 0;}

if ($handle = opendir("../dread")) {

while (false !== ($file = readdir($handle)) && $num < ($count + 5)) {
if ($file != "." && $file != ".." && $file != "dread.php" && $file != "songimg.jpg" && $num >= $count) {

echo "<img src=\"songimg.jpg\"><a href=\"$file\">$file</a><BR>\n";
$num++;
}

else if($file != "." && $file != ".." && $file != "dread.php" && $file != "songimg.jpg") { $num++; }
}

closedir($handle);
}

?>

my code is correct and it works perfectly. i want to know how it decides what file to use when it increments the $num variable. if you all could shed some light on that i'd be gracious.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe this format is simpler to understand

Code: Select all

&lt;?php
$num = 0; 
if(!isset($count))
	$count = 0;
if ($handle = opendir("../dread"))
{ 
	while ($file = readdir($handle) &amp;&amp; $num &lt; ($count + 5))
	{ 
		if ($file != "." &amp;&amp; $file != ".." &amp;&amp; $file != "dread.php" &amp;&amp; $file != "songimg.jpg")
		{
			if($num &gt;= $count)
				echo "&lt;img src="songimg.jpg"&gt;&lt;a href="$file"&gt;$file&lt;/a&gt;&lt;BR&gt;\n"; 
			
			$num++; 
		}
	} 
	closedir($handle); 
}
?&gt;
readdir returns the filenames in a specific order.
$file != "." && $file != ".." && $file != "dread.php" && $file != "songimg.jpg"
. and .. are two 'directories' that are always present (except .. in / ) and dread.php & songimg.jpg are two files that shall be invisible (even if present). All other files are counted ($num++). If there are more than $count files in the directory (note: those files in the if-clause are not counted) the next five will be displayed as link.

edit: still no improvements in english spelling and grammar ;)
Last edited by volka on Tue Oct 22, 2002 1:35 am, edited 2 times in total.
SuperHuman
Forum Newbie
Posts: 10
Joined: Wed Oct 16, 2002 10:00 pm

Post by SuperHuman »

readdir returns the filenames in a specific order.
Unix returns in order as last inode change, or in order of creation in the filesystem. Now, on a recent problem that a member from my home forums had was when using IIS, this was not the case. IIS running PHP was returning the files in a set order, as sorted by name. Just one more difference to add to the cross platform dillemas between WIN/*NIX PHP.... :)
Post Reply