![]() Topic awaiting preservation: PHP Photo Album (Page 1 of 1) |
|
|---|---|
|
Obsessive-Compulsive (I) Inmate From: Australia |
posted 02-19-2007 07:13
I just can't seem to get my head around PHP. I've been trying to learn it for a couple of weeks now and my brain just fuzzes over. In any case, I was following this tutorial: http://www.zend.com/zend/trick/tricks-jan-2003.php code: <?php
#thumbnail width and height
define("THUMBNAIL_WIDTH", "100");
define("THUMBNAIL_HEIGHT", "75");
# maximum number of photos displayed on an album page
define("MAX_DISPLAYED_PHOTOS","36");
# The image directory's relative and absolute path
define("IMAGE_PATH", "images");
define("ABSOLUTE_PATH","/reference-desk.net/gallery/images");
# Connect to the MySQL server and select the database
$connection = mysql_pconnect(Passwords removed by TP);
$db = mysql_select_db("green");
function add_photo($photoFileName, $title, $description)
{
$query = "INSERT INTO photo SET photoID=NULL,
photoFileName='$photoFileName',
title='$title',
description='$description'";
$result = mysql_query($query);
return $result;
}
function retrieve_album_page($offset)
{
$rows = MAX_DISPLAYED_PHOTOS;
$query = "SELECT photoID, photoFileName, title
FROM photo ORDER BY photoID
LIMIT $offset, $rows";
$result = mysql_query($query);
return $result;
}
function next_page($offset)
{
$photoCount = retrieve_photo_count();
if ($offset < $photoCount) {
echo "<a href='view.php?offset=$offset'>Next Page</a> >>";
} else {
echo "Next Page >>";
}
}
function retrieve_photo_count()
{
$countQuery = "SELECT count(photoID)
AS photoCount FROM photo";
$countResult = mysql_query($countQuery);
return mysql_result($countResult,0,"photoCount");
}
function previous_page($offset, $totalRows)
{
if ($offset - MAX_DISPLAYED_PHOTOS > 0)
{
$offset = $offset - $totalRows - MAX_DISPLAYED_PHOTOS;
echo "<< <a href='view.php?offset=$offset'>Previous Page</a>";
} else {
echo "<< Previous Page";
}
}
function retrieve_photo($photoID)
{
$query = "SELECT photoID, photoFileName, title,
description FROM photo WHERE photoID='$photoID'";
$result = mysql_query($query);
return $result;
}
AddPic:
<?php
include "config.php";
// Use Heredoc syntax to format the XHTML form
$addPhotoForm = <<< addPhotoForm
<p>Add a photo to the album</p>
<form action="addpic.php"
enctype="multipart/form-data" method="post">
<p>
Photo:<br />
<input name="photo" type="file" />
</p>
<p>
Descriptive Title:<br />
<input type="text" name="title"
size="40" maxlength="80" value="" />
</p>
<p>
Description:<br />
<textarea name="description"
rows="3" cols="40"></textarea>
</p>
<p>
<input type="submit" value="Add photo!" />
</p>
</form>
addPhotoForm;
// If the user has submitted a file for uploading.
if (is_uploaded_file($_FILES['photo']['tmp_name'])) {
# Add the photo to the MySQL database
add_photo($_FILES['photo']['name'], $_POST['title'],
$_POST['description']) or die("couldnt add photo");
# Move the photo from the temp directory to
# the images directory.
move_uploaded_file($_FILES['photo']['tmp_name'],
ABSOLUTE_PATH.$_FILES['photo']['name']);
echo "<p>The photo was successfully uploaded .</p>";
}
// display the form on every page instance
echo $addPhotoForm;
?>
View:
<?php
include "config.php";
$offset = isset($_GET['offset']) ? $_GET['offset'] : 0;
$offsetOriginal = $offset;
$photoQueryResult = retrieve_album_page($offset);
$totalRows = mysql_numrows($photoQueryResult);
while ($row = mysql_fetch_array($photoQueryResult))
{
$photoID = $row['photoID'];
$photoFileName = $row['photoFileName'];
$title = $row['title'];
echo "<div class='float'><a href='viewphoto.php?photoID=$photoID&offset=$offsetOriginal'>
<img src='".IMAGE_PATH."/$photoFileName' width='".THUMBNAIL_HEIGHT."' height='".THUMBNAIL_HEIGHT."' border='0' alt='$title' /></a><br /><p>$title</p></div>";
$offset++;
}
echo "<div><p>";
echo previous_page($offset, $totalRows);
echo "<br />";
echo next_page($offset);
echo "</p></div>";
?>
</body>
</html>
Big:
<?php
include "config.php";
// Display the photo
$photoQueryResult = retrieve_photo($_GET[photoID]);
list($photoID, $photoFileName, $title, $description) = mysql_fetch_array($photoQueryResult);
// Retrieve the image size.
$size = getimagesize(IMAGE_PATH."/$photoFileName");
echo "<div class='float'><img src='".IMAGE_PATH."/$photoFileName' $size[3] alt='$title' />
<br /><p>$title<br />$description</p>
<p><a href='view.php?offset=$_GET[offset]'>Back</a></p></div>";
?>
</body>
</html>
|
|
Bipolar (III) Inmate From: Underneath a mountain of blankets. |
posted 02-19-2007 14:23
It might help if you tell us what exactly goes wrong. |
|
Obsessive-Compulsive (I) Inmate From: Australia |
posted 02-19-2007 21:41
Eep, just goes to show you how much my brain DESPISES me :-P Cheers. |