need help with ecard home page - was PHP tutor in San Francisco



My thanks to all the people who offered to help me with homework assignment for the PHP class I'm taking. The assignment requires the PHP script to randomly display 16 thumbnails and when the user clicks on one of them, to display the full-size image. Here's what I've done so far:

1) Placed the thumbs in a directory called:

/students/dgoodi01/public_html/images/anna_may_wong/thumbs

2) Placed the fullsizes in a directory called:

/students/dgoodi01/public_html/images/anna_may_wong/fullsize

3) Set permissions for the directories to 711 and for the images to 444

4) Written a script called ecards.php and placed it in a directory called:

/students/dgoodi01/public_html/php

(PHP scripts can *only* be executed from the php directory, or a subdirectory thereof.)

The script can be accessed at:

http://hills.ccsf.edu/~dgoodi01/ecards.php

As you can see, there are a few errors, and I'm not sure what's causing them. The first one seems to be the result of my passing too large a number of random array elements. I don't understand why this is, since there are 139 images. Makes me think the array is not being created properly.

As for the second error, it would appear I have a syntax error, but I'm not sure what it is.

My thanks for any help folks here can provide, and apologies if I don't follow posting or coding conventions here. If I've not followed proper protocols, please let me know and I'll be sure to fix it.

Okay, here's the source code:

<a href="?dir=anna_may_wong">Anna May Wong</a><br />
<?php

//Relative page
//$dir = '../images/anna_may_wong';

//Absolute path
$image_dir = dirname(__FILE__) . '/../images/';
$ecard_dir = 'anna_may_wong/';
$url_dir = 'anna_may_wong';
if(isset($_GET['dir'])) {
$url_dir = $_GET['dir'];
$ecard_dir = $_GET['dir'] .'/';
}
$dir = $image_dir . $ecard_dir;

// How to read the names of files in a directory.
// FUNCTIONS:
// open the directory and get a handle (directory handle)
// read the the names of the files one at a time.
// this reading process doesn't distinguish between files and directories.
// EVERYTHING IS A FILE, particularly directories and regular files.
// opendir()
// readdir()
// closedir()
// array_rand()
// is_dir()

// Open
// shutup operator silences warnings.
$full_sized = "";
if(isset($_GET['img'])) {
$full_sized = "<img src='/~dgoodi01/images/$url_dir/fullsize/".$_GET['img'].'\' />';
}
$images = array();
if($dh = opendir($dir)) {
// read the files with readdir($dh);
while($file = readdir($dh)) {
// we don't want directories: ., .., .DS_Store, thumbs
// our script is in /users/dputnam/public_html/php
if(is_dir($dir.$file)) { continue; }
if(strstr($file,'.jpg')) {
if(file_exists($dir.'thumbs/'.$file)) {
$images[] = $file;
}
}
}
}

// We have an array that contains all of the images.
// $images has all images in it.
// We want a random selection.
// Fortunately, we have array_rand(ANARRAY, number)
$num = 16;
// array_rand returns the index numbers
$indexes = array_rand($images, $num);


closedir($dh);

$for_view = '';
foreach($indexes as $idx) {
$for_view .= "<a href=\"?dir=$url_dir&img={$images[$idx]}\"><img src=\"/~dgoodi01/images/$url_dir/thumbs/" .$images[$idx].'" width="90" /></a>';
}
print $for_view;
print $full_sized;
?>

.



Relevant Pages