Forum Moderators: coopster

Message Too Old, No Replies

Simple Image Gallery with PHP + HTML

         

ssandhu

4:32 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



Hi,

I am trying to create a simple image gallery. I have an index page in html that displays 5 pics. The problem I am having is creating the dynamic php page. I want it so that when I click on one of the images, the php page will display the image I clicked on with the description of that image.

The image address and description are stored in a database. Shown below is the code I have done so far:

For the html page:

<form method="post" action="template.php">

<div class="img">
<input type="image" src="Pictures/fireworks.jpg" width="200px" height="180px" alt="Submit Form" id="1">
<div class="desc">Fireworks display</div> </div>

<div class="img">
<input type="image" //image src and height+width id="2">
//another image</div>
</form>.

For the php:
<?php
$id = $_POST['id'];

mysql_connect("localhost", "username", "password") or die('Error: ' . mysql_error());
$db = "db_name";
mysql_select_db($db);

$query1 = "select address from images where imageid = '".$id."'";

$address = mysql_query($query1) or die ('Error retrieving address data');

$query2 = "select name from images where imageid = '".$id."'";

$description = mysql_query($query2) or die ('Error retrieving description data');

?>

<html>
<body>

<div class="img">
<img src="<?php "$address"?>" width="200px" height="180px">
<div class="desc">Description: <?php echo("$description");?> </div>
</div>

</body>
</html>

The table in mysql is images and three fields: imageid, address and description.

Help would be much appreciated.

d40sithui

6:16 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



Instead of $_POST, you should $_GET to pull data off the URL (the id). It's usually not practical to use $_POST for this purpose.

<inside your first page>


<a href="template.php?id=1"><img src="image1thumb.jpg"></a>
<a href="template.php?id=2"><img src="image2thumb.jpg"></a>
<a href="template.php?id=3"><img src="image3thumb.jpg"></a>
...

<inside template.php>


$id = $_GET['id'];
$result = mysql_query("SELECT address, description FROM images WHERE imageid=$id");
while($row = mysql_fetch_assoc($result)){
$address = $row['address'];
$description = $row['description'];

echo "<img src=\"$address\">";
echo $description;
}

ssandhu

7:20 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



Thanks alot d40sithui. Just tried it to simply print out the address and it works.

Thanks, been working on this for hours.

Kind Regards
SSandhu