Simple Gallery From Scratch

Hi There,

I’m looking to redevelop my photography site using php and mysql. I have done basic work in php and mysql, but am now finding myself stuck with writing the code for the following idea…

I am planning on producing a photo gallery, with 15 thumbnails per page, in the following format:
http://www.equinefocus.co.uk/personal/sample_gallery.htm

If clicking the thumbnail, I would want the large version of the image to open up.

I have the relevant details in my database, as shown in this screenshot:
http://www.equinefocus.co.uk/personal/database_info.jpg

Questions

  1. I have previously used the following piece of code to turn a date into a link - can this be adapted to turn the thumbnail into a link too?
<a href="<?php echo $info['url']; ?>"><?php echo $info['date']; ?></a>
  1. How would I achieve the table layout I want in terms of outputting the mysql data?

Any assistance would be appreciated, as would any redirection to suitable tutorials. I have googled it, but the pages I have looked at generally refer to using php for resizing of images, which is not needed for me. Many thanks!
[ul][/ul]

sample_gallery.htm shows a very decent start. I’ll tell you why: PHP and MySQL are serverside applications that you will want to keep separated from the ‘clientside’ code, ie. the HTML and CSS. What you do is make a template (as shown in sample_gallery.htm) and fill in the ‘blanks’ using MySQL values.

<table>
  <tr>
    <td rowspan="2" width="50%">
      <a href="http://www.equinefocus.co.uk/photography/others/[URL]"><img
        src="http://www.equinefocus.co.uk/photography/others/[THUMB]"
        border="0" width="122" height="122"></a></td>
        <td width="50%"><strong>Photo Reference: [REF]</strong></td>

    </tr>
    <tr>
        <td width="50%"><strong>Photo Details:</strong><br>
        [DETAILS]</td>
    </tr>
</table>

This will allow you to load this HTML (from a file, or the database) and process it in PHP:

<?php

$sql = "SELECT url, thumb, ref FROM myTable LIMIT 15";
$result = mysql_query($sql, $db);

$table_template = file_get_contents("mytemplate.htm");

while ($data = mysql_fetch_assoc($result)) {
  $mytable = $table_template;
  foreach ($data as $key => $value) {
    $mytable = str_replace("[".strtoupper($key)."]", $value, $mytable);
  }
}

?>

Many thanks for your quick and helpful response. Am I correct in thinking it works in a similar fashion to a php include?

I will start playing tonight, thank you!

Right, I have progressed a little further, but now have issues regarding debugging. I have read the link provided in your signature, which gives me a vague idea (being a vague sort of person), but I’ve only ever really had to deal with parse, which to my knowledge is largely missing ., or '! This code is slightly different to anything I’ve done, as I generally use:

<?php mysql_connect("*****", "*****", "*****") or die mysql_select_db("******") or die

How do I go about debugging the following code with the echo statement, as per the debugging thread?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<head><link rel="stylesheet" type="text/css" href="http://www.equinefocus.co.uk/style/20080114.css"/></head> 

	<body>

	<div id="banner"><img src="/images/banner_20080114.jpg"/></div>

	<?php	$page_title='Equine Focus';
		include($_SERVER['DOCUMENT_ROOT'].'/style/20080114menu.php');?>

	<div id="main">

<?php

$hostname = "*****";
$username = "*****";
$password = "*****"; 

$db = mysql_connect($hostname, $username, $password) or die ("Unable to connect to MySQL");
$sql = "SELECT url, thumb, ref, info FROM ***** LIMIT 15";

$result = mysql_query($sql, $db);

$table_template = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/style/gallery_template.php') or die ("Template Failed");;

while ($data = mysql_fetch_assoc($result)) {
  $mytable = $table_template;
  foreach ($data as $key => $value) {
    $mytable = str_replace("[".strtoupper($key)."]", $value, $mytable);
  }
}

?>
</div></body>
Sponsor our Newsletter | Privacy Policy | Terms of Service