PDA

View Full Version : PHP How to display multiple images from DB


tux
09-09-2007, 08:34 AM
I have now got the drop menu working and have managed to display a selected photographers image on the detail page. I now need some guidance as to how I can display all of the images from the selected photpgrapher. Nine images will be displayed per page in a 3x3 table. As more images are available next/prev links will be required.

Here my code so far........


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
$photographerID = $_GET['photographerID'];
$menu = $_POST['menu'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM images where photographerID='$photographerID'");
$result = @mysql_query($query);
$thumb = @mysql_fetch_array($result);
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers ");
$result = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result);
///////////////////////////////////////////
if($menu){
header("Location: gallery_detail.php?photographerID=$menu");
exit;
}
?>


and to display the image.......



<?php do { ?>
<td><div align="center"><img src="Images/gallery/thumbs/<?php echo $thumb['thumbURL']; ?> alt=<?php echo $thumb['title']; ?> ></div></td>
</tr>
<tr>
<td><div align="center"></div></td>
</tr>
<?php }while ($thumb = mysql_fetch_array($result)); ?>

tux
09-09-2007, 08:40 AM
Another point I have just noticed is that the 'alt' for the image only displays the first word of the title from the database when hovered over in the browser. Any ideas on this.

davidj
09-09-2007, 10:35 AM
where you have

<img src="Images/gallery/thumbs/<?php echo $thumb['thumbURL']; ?> alt=<?php echo $thumb['title']; ?> >

you have a src attribute with a starting quote but you never terminate the quote after the src attribute. Also you have missed the quotes in your ALT attribute.

tux
09-09-2007, 02:16 PM
Thanks Dj, the quote in the src was in my code so I must have deleted when I copied it to the post. I had missed the quotes from the alt though...doh

Can you give me some direction regarding displaying the images ( see first post ) thanks.

tux
09-10-2007, 10:01 AM
Hi David, could you give me some guidance on repeat region for displaying the images. I have the first image displaying using the photographerID now. Thanks.

davidj
09-10-2007, 10:05 AM
its a little bit complex this one

i need to sit down and work it out

i may look at this tonight

tux
09-10-2007, 10:14 AM
Thanks for your help as always, I'm at work now so this evening will great. The camera club have postponed the deadline and will continue with the ASP version for a while so the pressure is off me a bit.

In the ASP version I have 3 tables. The first displays image1 and counts to three, the next shows image4 and counts to 6, then image7 to 9. I had to change the code by hand to achieve it. Not sure if this helps.

In PHP would it be some kind of 'while' statement.

Regards, Paul.

davidj
09-10-2007, 10:33 AM
best way is to perform a count on the $row['photo']

$count = count($row['photo']);

then divide by 3

$count = $count /3;

this will produce a float or a whole number

all you need to do is detect the whole numbers and add a </tr> dynamically



// ok here we check that $count is a float. If true just draw a <td> as we tont want to teminate that row just yet.

if(is_float($count)){

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

// here we discover an integer (divide by 3) so we have come to the end of the row so lets terminate it by </tr> and setting a new row while we are here.

}else{

echo "<td>".$row['photo'].</td></tr><tr>;

}


the above needs to be in your do while loop for it to work.

this may not work as i havent tested it but will tonight

i think we need to increment the count within the loop but will have to wait to see

tux
09-10-2007, 11:30 AM
Intersting stuff, I'll have a go at it in my lunch break and post tonight. Cheers.

tux
09-11-2007, 05:55 PM
Okay, I have given your suggestion a bit of a go but I am still only getting one image to display. Here is my code so far, not sure if it is the best way of writing it but the page still works so hopefully it is some way to being correct.


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
$photographerID = $_GET['photographerID'];
$menu = $_POST['menu'];
///////////////////////////////////////////
///////////////////////////////////////////
$query = sprintf("SELECT * FROM images where photographerID='$photographerID'");
$result = @mysql_query($query);
$thumb = @mysql_fetch_array($result);
///////////////////////////////////////////
///////////////////////////////////////////
$count = count($thumb['thumbURL']);
$count = $count /3;
///////////////////////////////////////////
echo $count; // with $count = $count /3; prints 0.333333333, without prints "1"
///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers ");
$result = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result);
///////////////////////////////////////////
if($menu){
header("Location: gallery_detail.php?photographerID=$menu");
exit;
}
?>


and....


<?php do {
if(is_float($count)){
echo "<td>" ?><div align="center"><img src="Images/gallery/thumbs/<?php echo $thumb['thumbURL']; ?>" alt=" <?php $thumb['title']; ?>"></div></td>yvie
<?php }else{
echo "<td>" ?><div align="center"><img src="Images/gallery/thumbs/<?php echo $thumb['thumbURL']; ?>" alt=" <?php $thumb['title']; ?>"></div></td></tr><tr></tr>paul
<?php }}while ($thumb = mysql_fetch_array($result)); ?>


you will see I have added an echo $count; to see what its returning and have commented out what it returns.

Also in the lower code I have added 2 names so I know which one is being used. It seems to me that $count is returning only 1 image when there is 3 images in the DB for each photographer???

What do you think??

davidj
09-12-2007, 08:59 AM
will need to write a demo for you to help you understand

will look into this tonight (GMT)

tux
09-14-2007, 12:59 PM
Hi David, I was wondering if you have had a chance to think any more about this. Thanks.

Kind regards, Paul. :)

PS: I noticed you put (GMT) on your last post, are you out of the country as we never seem to be on the forum at the same time. It would be handy to know the best times to catch you. Thanks again.

davidj
09-14-2007, 03:15 PM
will look tonight (GMT)

tux
09-18-2007, 02:39 PM
Hi again David,

Sorry to keep asking but I really would like to get to the bottom of this problem. If you think its too complex for the stage I am at with PHP ( total novice ) then showing me more basic stuff to get me upto speed would be great. As you know I have done your tuts but would like to progress.

The site I am trying to change to PHP from ASP is okay as it is for now so I have time on my side again.

I have the current site to do, a site for my brother before finally having time to do my own site. All of them will be PHP and I want all of them to be hand coded. So by the time they are all done I hope to be well on my way to being competant in PHP.

So I am open to your suggestions as to my best options to learn how to code like you. If you can spare some time I really want to learn. PM me if its easier than keep posting.

Thanks, regards

Paul