Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
no! don't panic!

look at your php code, above. you've got "images/photos" hardcoded in there. start by making its value the variable you pass in from the URL.

then simplify your code to display only what's in that directory. for each image you're putting to the screen, make sure its URL conforms to what you're coding to. i.e. the page will have links to itself, but with different values for the variable.

so instead of:
http://69.68.181.132/~joetalerico/gallery.php

you'd have something like:
http://69.68.181.132/~joetalerico/gallery.php?display_dir="images/photos/house"

...and then, when the user clicked on the directory for kitchen...

http://69.68.181.132/~joetalerico/gallery.php?display_dir="images/photos/house/kitchen"

does that make sense?
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
just for fun, i mocked up a little prototype here. the start page is hardcoded. the images will take you to a subdirectory and run showdir.php with the URL of the subdirectory as an argument.

showdir.php will parse the directory and perform the following:
1. if it finds a file, it will assume it's an image and display it
2. if it finds a subdirectory, it will make a link to the subdirectory

so, if you click on the link, it calls showdir.php again, but with the new value. the best example of this is the bricks, you can follow that down a couple directories.

it's not the best code, or the best way to do it, but it's a working prototype. here's the php code:

showdir.php:
PHP:
<?
//----------------------------------------------------------------
//
// %Z%%M% %I% %G%
//
// copyright Steve Zimmers 2006
//
//----------------------------------------------------------------

include "header.inc";
include "style.css";
?>

<body>

<?
        //------------------------------------------------------------
        // display_image
        //------------------------------------------------------------

        function display_image($file, $dir)
        {
                $pathed_file = $dir . "/" . $file;

                $img_string = '<img src="' .  $pathed_file . "\" " .  'width="200" height="150" />';

                echo $img_string . '<br />' . '<br />' . "\n";
        }

        //------------------------------------------------------------
        // make_url
        //------------------------------------------------------------

        function make_url($file, $dir)
        {
                $pathed_file = $dir . "/" . $file;

                $href_string = '<a href="showdir.php?display_dir=' .  $pathed_file . "\"" .'>' . $file . '</a>';

                echo $href_string . '<br />' . '<br />' . "\n";
        }

        //------------------------------------------------------------
        // main
        //------------------------------------------------------------

        // display_dir is our passed in variable. first we change to
        // that directory in order to grab the file contents

        chdir($display_dir);

        // next we create our directory object in order to get
        // the file list

        $dir = dir('.') or die($php_errormsg);

        // process each file

        while (false !== ($f = $dir->read()))
        {
                // skip this directory and its parent

                if ($f == "." || $f == "..")
                        continue;

                // if it's a file, assume it's an image to display

                if (is_file($f))
                {
                        display_image($f, $display_dir);
                        continue;
                }

                // if it's a directory, make an href

                if (is_dir($f))
                {
                        make_url($f, $display_dir);
                        continue;
                }


        } // while

        // clean up

        $dir->close();

?>

</body>

<? 
include "footer.inc"; 
?>

index.php:
PHP:
<?
include "header.inc";
include "style.css";
?>

<body>

<a href="showdir.php?display_dir=images/chairs">
<img src=images/chairs.jpg width="200" height="150" />
</a>

<br />
<br />

<a href="showdir.php?display_dir=images/bricks">
<img src=images/bricks.jpg width="200" height="150" />
</a>

<br />
<br />

<a href="showdir.php?display_dir=images/trees">
<img src=images/trees.jpg width="200" height="150" />
</a>

</body>

<? 
include "footer.inc"; 
?>

header.inc:
PHP:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title>silly</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

footer.inc:
PHP:
</html>

style.css is empty.
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
Why does php think my two arrays are not arrays?

Warning: Invalid argument supplied for foreach() in /Users/joetalerico/Sites/gallery.php on line 23

Warning: natsort() [function.natsort]: The argument should be an array in /Users/joetalerico/Sites/gallery.php on line 32

Warning: natsort() [function.natsort]: The argument should be an array in /Users/joetalerico/Sites/gallery.php on line 33
PHP:
<?
include ("global.inc"); 
top();
topmain();
bannertbl();
endtbl();
maintbl();
menu();
frstbl();
#Set directories...
$opendir = opendir('images/photos');
#Get the Files
while($files = readDir($opendir)){
	#Dont want .something files
	if(is_dir($file) == true){
		if(substr($files, 0,1) != "."){
			#put them in an array
			$directory[] = $files;
		}
	}
}
#Go through the array of dir names
foreach($directory as $file){
	#checks to see if it is a dir
		$imgdir = opendir('images/photos/$file');
		while($images = readDir($imgdir)){
			if(substr($images, 0,1) != "."){
				$files[] = $images;
				}
		}
}
natsort($files);
natsort($directory);
$num = 0;
echo $holddir[1];
while($dirs[num] != null && $img[num] != null){
	$dirname = $dirs[num];
	$image = $img[num];
	echo '<a href=images/photos/?name='.$dirname.'>';
	echo '<img src=images/photos/'.$dirname.' width=150 height=150>';
	echo '</a>';
	$num++;
}
endtbl();
endhtml();
?>
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
couple things i noticed:

1) line 6: 'ture' should be 'true'
2) your lines don't match up with the compiler messages (need whole file or numbered lines)
3) because of 'ture', that array will never be filled; print them out to check their values as a debugging step
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
zimv20 said:
couple things i noticed:

1) line 6: 'ture' should be 'true'
2) your lines don't match up with the compiler messages (need whole file or numbered lines)
3) because of 'ture', that array will never be filled; print them out to check their values as a debugging step

Sorry about that, fixed the true still doesnt work.. There is the whole code
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
I found another problem.. File and files..


Also if i remove the if(substr($file, 0,1) part it does not error out...


Never mind!!
Got that working :)
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
So after learning some more php.. I have finally got this working... Pretty simple Gallery that is very upgradeable... (I am soon going to add comments for each gallery just a short blurb)..

So here it is... In all of its greatness.


gallery.php
PHP:
<?
include ("global.inc"); #your global, or whatever you use to make it purrdy! 
echo "<html>";
echo "<body>";
#Set directories...
$opendir = opendir("images/photos");
#Get the Files
$length = 0;
while($file = readdir($opendir)){
	if((substr($file, 0,1)) != '.'){
		#Dont want .something files
		if(is_dir("images/photos/".$file) == true){
			#put them in an array
			$directory[] = $file;
			$length++;
		}
	}
}
#Go through the array of dir names
foreach($directory as $dir){
	#checks to see if it is a dir
		$imgdir = opendir("images/photos/".$dir);
		$fstimg = 0;
		#Get the dir contents
		while($images = readdir($imgdir)){
			#No need to get more than one image per dir.
			if($fstimg < 1){
			if((substr($images, 0,1)) != "."){
				$files[] = $images;
				$fstimg++;
				}
			}
		}
}
#No need to be open anymore!
closedir($imgdir);
closedir($opendir);
$num = 0;    
#Get all the goods out of the array
while(sizeof($directory) > $num){
	$dirname = $directory[$num];
	$image = $files[$num];
	#Make my front page!!
	echo "<br>";
	echo "<br>";
	echo "<a href=gallery_img.php?dir_name=".$dirname.">";
	echo "<img src=images/photos/".$dirname."/".$image. " width=200 height=150>";
	echo '</a>';
	#Update num so this doesn't go on for ever.. Trust me.. Firefox wont like it.
	$num++;
}
echo "</body></html>";
?>

gallery_img.php
PHP:
<?
include ("global.inc"); #your global, or whatever you use to make it purrdy! 
echo "<html>";
echo "<body>";
#Grab everything after the ?
parse_str($_SERVER['QUERY_STRING'],$vars);
$dir_name = $vars['dir_name'];
#Opens Main Dir
$hdl = opendir("images/photos/".$dir_name);
#Grabs all the files of it except ones that being with '.'
while ($dirEntry = readdir($hdl)){
	if (substr($dirEntry, 0,1) != '.') {
 		$listing[] = $dirEntry;
 	}
} 
#Sort the entries, not that it matters...
natsort($listing);
#Close the files
closedir($hdl);
foreach($listing as $file){
	#Grabbin all the files as long as they are not dirs.
	if(!is_dir("images/photos/".$dir_name."/".$file)){
		#Put it out to the site!
		echo  "<a href=images/photos/".$dir_name."/".$file." target=none>";
       	echo  "<img src=images/photos/".$dir_name."/".$file. " width=150 height=100>   ";
        echo  "</a>";
	}
}
echo "</body></html>";
?>

Thats it... Hope it looks like decent code!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.