Click to See Complete Forum and Search --> : Dynamic Arrays


Vasilli
08-07-2003, 07:47 PM
I am trying to create an array of files in a directory, the code i have so far is as follows. What i am trying to do is make an array of the files, and then post them to the screen in lots of three (3 files to a line, i will place them into cells eventually). I have no problems with the code, it posts them to the screen, however it dosn't take any notice of the line which tells it to, after 3 entries, insert 2 <br>. can anyone tell me why?

Thankyou in advance
Jono


$sImageDir = 'path/to/files';
if (is_dir($sImageDir)){

$vOpenedDir = opendir($sImageDir);

while (($file = readdir($vOpenedDir)) != false) {
$sDirItem = $sImageDir. $file;

$sIgnoreList = $sDirItem != $sImageDir."." && $sDirItem != $sImageDir."..";
if ($sIgnoreList){

$iFile = array($file);

for($file = 0; $file < count($iFile); $file++)
{
if ($file % 3 == 0 && $file != 0) echo "<br><br>";

echo "<img src=". $sImageDir . $iFile[$file] ." width=\"130\"><br>";

//echo $iFile[$file] ."<br>";
}
}
}
}
closedir($vOpenedDir);}

Dragons_Bane
08-07-2003, 08:16 PM
I don't know why that's not triggering, but you could make another var... ($counter) and have it count 1,2,3 and have an...

if ($counter == 3) : $counter = 1;

and see if that does it.

DaiWelsh
08-08-2003, 10:18 AM
You are creating a fresh one item array each time through the loop with this line

$iFile = array($file);

so this line

for($file = 0; $file < count($iFile); $file++)

will always be effectively

for($file = 0; $file < 1;$file++)

and hence your <br> code will never fire.

You need to either load the files into the aray first then loop through the array, or as suggested use a seperate counter to keep track of when to break. Assuming you intended the first option youi could change your code to something like:-

$sImageDir = 'path/to/files';
if (is_dir($sImageDir))
{
$iFile = Array();
$vOpenedDir = opendir($sImageDir);
while (($file = readdir($vOpenedDir)) != false)
{
$sDirItem = $sImageDir. $file;
$sIgnoreList = $sDirItem != $sImageDir."." && $sDirItem != $sImageDir."..";
if ($sIgnoreList)
{
array_push($iFile,$file);
}
}
for($file = 0; $file < count($iFile); $file++)
{
if ($file % 3 == 0 && $file != 0) echo "<br><br>";
echo "<img src=". $sImageDir . $iFile[$file] ." width=\"130\"><br>";
}
closedir($vOpenedDir);}
}