Page 1 of 1

for loop not showing my array

Posted: Mon Nov 02, 2009 10:52 pm
by psychotomus

Code: Select all

$datas = array();
if ($handle = opendir("downloads/")) 
{
 
    while (($file = readdir($handle)) !== false) 
    {
        $datas[] = $file;
    }
    
    closedir($handle);
}
 
echo count(datas); //displays 18
for($i=0; $i<count(datas); $i++)
{   echo $datas[$i];
}

Re: for loop not showing my array

Posted: Tue Nov 03, 2009 10:06 am
by N1gel
Try this to print out your array and see what data you have

Code: Select all

print_r($datas)
or this to check that your for loop is being executed

Code: Select all

for($i=0; $i<count(datas); $i++)
{  
echo "<p>$i -".$datas[$i]."</P>";
}
 

Re: for loop not showing my array

Posted: Tue Nov 03, 2009 10:10 am
by xtiano77

Code: Select all

 
[color=#FF0000]$[/color]datas = array();
echo count([color=#FF0000]$[/color]datas); //displays 18
for($i=0; $i<count([color=#FF0000]$[/color]datas); $i++)
{   echo $datas[$i];
}
 
I believe you declared the array as $datas, but later referred to it as "datas" omitting the "$". I think that if you put in the "$" it will fix the problem.

Just my two cents.