/    Sign up×
Community /Pin to ProfileBookmark

Best way to layout a dynamic gallery

I’m trying to make my site as dynamic as possible. Right now I have a main gallery list where people can select which gallery the would like to view and then all of the pictures from that category are displayed with a caption. As it is just now, the first thumbnail will display, followed by the caption, followed by the second thumbnail, followed by the caption, and so on. The link is [url]www.namik.co.uk/gallery.php[/url] (sorry link maker isn’t working at the mo’)
Can anyone advise of a way in which I could have the images displayed with the text beneath them. I was thinking having a loop which would count to 4 while displaying the images then echo a line break, then count to 4 while displaying the captions, then start again until everything has been displayed but all of my attempts have failed so far. I don’t want to go down the road of having to insert the caption as an “alt” text as I know this fecks up the accessibility of the site.

[code=php]<?php
$pagehead = ‘Namik – Gallery’;
include( “guestbook/misc.inc” );
include( “pagehead.php” );

mysql_connect(“$host”, “$user”, “$password”)or die(“Cannot connect to database”);
mysql_select_db(“$database”)or die(“cannot select DB”);

$category = $_GET[‘id’];
$result = mysql_query(“SELECT * FROM gallery_photos WHERE photo_category = $category”);
$result2 = mysql_query(“SELECT * FROM gallery_category WHERE category_id = $category”);

while ($row2 = mysql_fetch_assoc($result2)) {
$name = $row2[“category_name”];
}
echo “<h1>$name</h1>”;
if ($result && mysql_num_rows($result)) {
$numrows = mysql_num_rows($result);
echo ‘<p>There are ‘ . $numrows . ‘ pictures in the ‘ . $name . ‘ category:<br />
Click on a thumbnail to view the full sized image.<br /><a href=”gallery.php”>Back</a></p>’;
echo ‘<p>’;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo “<a class= ‘linksimg’ href=’img/gallery/” . $row[‘photo_filename’] . “‘ ><img src=’img/thumbs/” . $row[‘photo_filename’] . “‘ /></a>”;
echo $row[‘photo_caption’] . “n”;
}
} else {
echo ‘<p>There are currently no images in the ‘ . $name . ‘ category.<br /> Please select another category.’;
}
echo ‘</p>’;
?>
</div>
</body>
</html>[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@swonMay 05.2006 — try something like this:

<table border=0>

<?php

$i = 0;

$modulo = 4;

echo '<tr><td colspan='.$modulo.'><p>There are ' . $numrows . ' pictures in the ' . $name . ' category:<br />

Click on a thumbnail to view the full sized image.<br /><a href="gallery.php">Back</a></p></td></tr><tr>';

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

if($i % $modulo ==1 && i>0){

echo "</tr><tr>";

}

echo "<td><a class= 'linksimg' href='img/gallery/" . $row['photo_filename'] . "' ><img src='img/thumbs/" . $row['photo_filename'] . "' /></a>";

echo $row['photo_caption'] . "</td>n";

$i++;


}

?>

</tr>

</table>

I didn't try it, but something like this should it be
Copy linkTweet thisAlerts:
@DoppleauthorMay 05.2006 — Thanks. I'll try that as soon as I've got a password issue on my forum sorted. ?
Copy linkTweet thisAlerts:
@NogDogMay 05.2006 — Another possibility would be to put each thumbnail and caption into a DIV, which you define via CSS to be a "float: left;" element. This will fill the available space on each line with thumbnails and then automatically wrap to the next line if there are more.

Style:
<i>
</i>div.thumb {
float: left;
width: 200px; /* adjust to desired width */
margin: 10px;
text-align: center;
}

HTML:
[code=html]
<div class='thumb'>
<p><img src="foo.jpg" alt=""></p>
<p>Caption text here</p>
</div>
<div class='thumb'>
<p><img src="foo.jpg" alt=""></p>
<p>Caption text here</p>
</div>
...etc...
[/code]
Copy linkTweet thisAlerts:
@DoppleauthorMay 15.2006 — I have used the code advised by NogDog (to avoid the use of tables) but it doesn't work. What happens is the content is displayed but is competely missing the style. could this be because it's already in a "content" div?

The content div is...
[code=html].content {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #FFFFFF;
background-color: #000000;
margin: 30px 150px 50px;
padding-top: 20px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 30px;
border: 1px solid #ffffff;
}[/code]

and the thumb div is...
[code=html].thumb {
float: left;
width: 150px;
margin: 10px;
font-size: 10px;
text-align: center;
}[/code]


The actual code I'm using to display is below...
[code=php] $result = mysql_query("SELECT * FROM gallery_photos");
echo "<h1>Gallery</h1>n";
if ($result && mysql_num_rows($result)) {
$numrows = mysql_num_rows($result);
echo '<p>There are ' . $numrows . ' pictures in the Gallery.<br /></p>';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "n<div class='thumb'>
<p><a href='img/gallery/" . $row['photo_filename'] . "' ><img src='img/thumbs/" . $row['photo_filename'] . "' /></a></p>
<p>" . $row['photo_caption'] . "</p></div>";
}
} else {
echo '<p>There was an error. Please check back later.</p>';
}[/code]
Copy linkTweet thisAlerts:
@bokehMay 15.2006 — [URL=http://bugster.co.uk/thumbs]Here's a demo[/URL]. CSS is a bit different from Nogdog's. Here's the code I used:[code=php]<?php
if(isset($_GET['src']))
{
!preg_match('@/@', $_GET['src']) or die();
resizeSemiAbstractTop($_GET['src']);

}else{

echo <<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="resource-type" content="document" />
<meta name="description" content="Thumbnails." />
<meta name="keywords" content="Thumbnails" />
<meta name="distribution" content="global" />

<style type="text/css">
ul#gallery {
margin:0 auto;
padding:0;
list-style-type:none;
width:90%;
font-family: Tahoma
}

ul#gallery li {
float: left;
margin:10px 15px;
}

ul#gallery li img {
border:0;
}

ul#gallery li p
{
text-align: center;
margin:5px 0;
}
ul#gallery li p a {
text-decoration: none;
color: #000;
}
</style>

<title>Thumbnails</title>
</head>

<body>

<ul id="gallery">

END;

$dir = opendir($_SERVER['DOCUMENT_ROOT'] . str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']));

while (false !== ($file = readdir($dir))) {
if (eregi('.jpg$', $file))
{
if (!eregi('line.jpg$', $file))print ('<li><a href="' . $file . '"><img src="'.$_SERVER['PHP_SELF'].'?src=' . $file . '" width="80" height="60" alt="" /></a><p><a href="' . $file . '">' . str_replace('.jpg', '', $file) . '</a></p></li>'. "n");
}
}
closedir($dir);

echo <<<END
</ul>

</body>
</html>
END;

}

function resizeSemiAbstractTop($source, $destination = NULL, $w = 80, $h = 60)
{
$source_image = @imagecreatefromstring(
@file_get_contents($source))
or die($source.' is not a valid image');
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$ar = $sw/$sh;
$tar = $w/$h;
if($ar >= $tar)
{
$x1 = round(($sw - ($sw * ($tar/$ar)))/2);
$x2 = round($sw * ($tar/$ar));
$y1 = 0;
$y2 = $sh;
}
else
{
$x1 = 0;
$y1 = 0;
$x2 = $sw;
$y2 = round($sw/$tar);
}
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2);
if(!$destination) header('Content-type: image/jpeg');
@imagejpeg($slate, $destination, 50) or die('Directory permission problem');
ImageDestroy($slate);
ImageDestroy($source_image);
if(!$destination) exit;
return true;
}
?> [/code]
Copy linkTweet thisAlerts:
@DoppleauthorMay 16.2006 — Thanks Bokeh, That has worked. I use short descriptions for the images rather than just the file name. This was causing the text to push the next list item to the side and stagger the appearence so I got around this by putting the text in a div as below...
[code=html].gall {
width:100px;
height:55px;
overflow: visable;
}[/code]

The height property avoided images not being shown below descriptions which wrapped to 3 lines.

Thanks for all of your help in this!
Copy linkTweet thisAlerts:
@bokehMay 16.2006 — What you have done is make the CSS static. With my CSS the page expanded automatically based on the size of the images but with your method you will need to edit your hard coded values contained in the CSS if you change the size of the thumbnails. Instead I would have limited the caption length using substr() or overflow hidden and then used the [I]title [/I]attribute of the <a> element to hold the full version.

If you kept the captions to a realistic length when saving them in the first place none of this would be necessary anyway.
Copy linkTweet thisAlerts:
@DoppleauthorMay 16.2006 — I suppose I'd also have to alter the css if a photo is uploaded with too long a description too. Thanks for the tip. I'm going to leave as it is just now as I'm editing my site bit by bit to be completely database driven but also completely valid XHTML strict. Once I've sorted that out I'm then going to tweak and change until I am completely happy with the site. You can check it out if you like. [URL=http://www.namik.co.uk/gallery.php]Here's the link[/URL].
×

Success!

Help @Dopple spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...