Jump to content

Recommended Posts

Hi,

 

I have created a form for users to upload photos.

When they upload photos the photo name and caption is stored in the database along with club_id.

 

Now I want the images to be displayed on the page on the page where club_id = '$id'. I could just do a simple while loop to show all the images and captions WHERE club_id = '$id' but this is an untidy way of displaying them, as I dont no how many images the club has.

 

Is there anyway I can display the images so that they show 4 across with the caption below it, then say there is 5 images it creates a new row, otherwise it dosent?

 

Then is it possible to say if there are more than 16 images create another page rather than keep displaying them down the page?

 

Thanks for your help.

 

 

Link to comment
https://forums.phpfreaks.com/topic/83709-solved-displaying-images/
Share on other sites

for the 16 images your need to readup on pagination, as for the displaying them in a grid you could to something like this

<?php
$dbitems = array("img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg");
$maxcol = 4;
echo "<table>";
$c = $maxcol;
foreach($dbitems as $img)
{
if($c==0) echo "<tr>";

echo "<td>";
echo "<img src=\"$img\">";
echo "</td>";

if($c==0)
{
	echo "</tr>";
	$c = $maxcol;
}else{
	$c--;
}
}
echo "</table>";
?>

 

untested (used a array to same me typing the sql stuff)

Thanks for the help.

 

I added this mysql in replace of the array:

 

$query = "SELECT * FROM images WHERE club_id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$dbitems = $row['picture_name'];
$caption = $row['caption'];

 

And it gives the error:

 

An error occured in script /home/mysport/public_html/gallery.php on line 99: Invalid argument supplied for foreach()

 

Thanks for your help

your need a while loop with the DB,

 

try this (again untested)

 

<?php
$query = "SELECT * FROM images WHERE club_id = '$id'";
$result = mysql_query($query);


//$dbitems = array("img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg", "img5.jpg");
$maxcol = 4;

echo "<table>";
$c = $maxcol;
//foreach($dbitems as $img)
while($img = mysql_fetch_assoc($result))
{
$imgName= $img['picture_name'];
$caption = $img['caption'];

if($c==0) echo "<tr>";

echo "<td>";
echo "$imgName<br>";//debugging
echo "$caption <br>";//debugging
echo "<img src=\"$imgName\">";
echo "</td>";

if($c==0)
{
	echo "</tr>";
	$c = $maxcol;
}else{
	$c--;
}
}
echo "</table>";
?>

 

Excellent, thanks will look into it.

 

Ive progressed a bit further now with it, however I have just noticed that when you have say 5 images.

 

So that will be 4 across the top then another 1 on the next row?

 

If I add another image in this situation it creates a new row again rather than putting the picture alongside the 5th image?

 

Appreciate your help

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.