Jump to content

PHP Help


Recommended Posts

Hey guys,ok, well, so far I have this bit of code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"		"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />	<title>Directory Contents</title></head><body>// Set the directory name and open it.$search_dir = '.';$dp = opendir ($search_dir);// List the files.while ($item = readdir ($dp) ) {	if ( (is_file ($item)) AND (substr ($item, 0, 1) != '.') ) {					// Print the information.		print $item;		} // Close the IF.} // Close the WHILE.closedir ($dp); // Close the directory.?></body></html>

that I made up using the PHP Manual mostly,but instead of printing out the files to the screen, how do I convert the filenames into an array? and how do I limit the listing to only jpg, gif, and png file extensions?I realize that PHP 5 uses the "scandir()" function that automatically does this, but my host has me on PHP 4.4.4 so I am forced to figure this out the manual waythanks guys, Basically what I am trying to do, is a Photo gallery like this one except, instead of using XHTML to insert the image filenames manually, get a list of image files inside the current directory and display them to that CSS style photo gallery.. I'll probably also remove the "mouseover" thing so the gallery always stays on the current page, but thats basically what I am trying to do.then I'll name that file "index.php" and put it inside my "/photos/home_pics" directory with a bunch of images in it. thanks guys.almost forgot.I know to create arrays you do like$variable = array (1 => 'something', 'something2');etc but I don't understand how to put $item into an array thats within a while loop like thatthanks

Link to comment
Share on other sites

Adding to an array is called pushing something onto the array. In C people had to deal with a structure called a stack, and you would push an item onto the end of the stack, and pop an item off the front of it. So, adding an element onto the end of an array is called pushing the element on to it. There is an array_push function, and a shortcut. Both of these statements in the loop do the same thing:

$array1 = array();$array2 = array();$i = 0;while ($i < 10){  array_push($array1, $i);  //push $i onto the end of $array1  $array2[] = $i;  //this does the same thing to $array2  $i++;}print_r($array1); //print $array1print_r($array2); //print $array2

Here is the reference page for array_push:http://www.php.net/manual/en/function.array-push.phpYou can use array_push to add more then one thing at a time, but if you are only adding one item at a time it's better to use the second way.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...