Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

help with displaying arrays

Recommended Posts

hey guys,

I'm trying to get a script to read the contents of a directory, store the file names in an array and then displaying those files. So far I got the 2 first parts to work like a charm but I'm having trouble displaying the elements of the array.

Here's what I got so far.

[code]
//this part works perfectly
$dir = "img/events/" . $name . "/thumbnails/";
$aNames = array();
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file_names = readdir($dh)) !== false) {
           if ($file_names != "." && $file_names != "..") {
   array_push($aNames,$file_names);
   }
       }
       closedir($dh);
   }
}

//this part doesn't
for ($i ='0'; $i <= $numFiles; $i++) {
  echo array_values($aNames);
  echo '<tr><td> <img src="' . $dir . '' . $aNames[i] . '" alt="' . $aNames[i] .'" /> </td></tr>';
}[/code]

the second part only displays the path but the array returns nothing, If echo just the array for instance echo $aNames[0]; it does return a value. Any ideas what I can do?

Share this post


Link to post
Share on other sites
You need to initialy set $i to 0 not '0' in your for(). Also... where do you declare $numFiles? Also, this $aNames[i] should be $aNames[$i].

Sorry, but the second part of the code is a bit of a mess.

To be honest, looping through a directory and then again looping through an array is a waste of resources. If you really dont need to, why use more than one loop?

Share this post


Link to post
Share on other sites
so what would you suggest  ;)

$numFiles is declared right under the first part, just forgot to copy paste it...
[code]
$numFiles = count($aNames);
[/code]

and the first echo was just a test, forgot to use the []  :P

Share this post


Link to post
Share on other sites
Is this close to what you are looking to do....

http://www.nstoia.com/toh/technical/listdir/index.php

(click on the example to see an... well example)

Lite...

Share this post


Link to post
Share on other sites
thanks for the link llitebearer works perfectly now.

I got another question though, does anyone know how I can add a delete button so that when it gets clicked the pic gets deleted? This is what I tried so far

[code]<? echo '<script>
function del()
{
var delimgfinal = confirm("Are you sure you want to delete, ' . $aNames[$i] . '?")
if(delimgfinal == true)
{
unlink($path.$aNames[$i]);
                unlink($dir.$aNames[$i]);
} else {

}
}
</script>'?>

//......
<? for ($i =0; $i <= $numFiles--; $i++) {
echo '<tr><td> <a href="' . $path . ''. $aNames[$i].'" target="_blank"> <img src="' . $dir . '' . $aNames[$i] . '" alt="' . $aNames[$i] .'" /></a> </td></tr><input type="button" onclick="del()" value="Delete Image" />';

} ?>
[/code]

but that's not working.... any ideas why it's not working?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.