Jump to content


Photo

help with displaying arrays


  • Please log in to reply
4 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 03 July 2006 - 12:49 AM

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.

//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>';
}

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?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 03 July 2006 - 12:55 AM

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?

#3 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 03 July 2006 - 12:56 AM

so what would you suggest  ;)

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

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

#4 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 03 July 2006 - 01:26 AM

Is this close to what you are looking to do....

http://www.nstoia.co...stdir/index.php

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

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture

#5 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 07 July 2006 - 06:47 PM

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

<? 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" />';
					
} ?>

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users