Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/13506-help-with-displaying-arrays/
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?
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?
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.