logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > General
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 09-09-2007, 08:34 AM   #1
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default PHP How to display multiple images from DB

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........

Code:
 
<?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.......


Code:
 
<?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 is offline   Reply With Quote
Old 09-09-2007, 08:40 AM   #2
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

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.
tux is offline   Reply With Quote
Old 09-09-2007, 10:35 AM   #3
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

where you have

HTML Code:
<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.
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 09-09-2007, 02:16 PM   #4
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

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 is offline   Reply With Quote
Old 09-10-2007, 10:01 AM   #5
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

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.
tux is offline   Reply With Quote
Old 09-10-2007, 10:05 AM   #6
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

its a little bit complex this one

i need to sit down and work it out

i may look at this tonight
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 09-10-2007, 10:14 AM   #7
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

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.
tux is offline   Reply With Quote
Old 09-10-2007, 10:33 AM   #8
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

best way is to perform a count on the $row['photo']

PHP Code:
$count count($row['photo']); 
then divide by 3

PHP Code:
$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


PHP Code:
// 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
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 09-10-2007, 11:30 AM   #9
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

Intersting stuff, I'll have a go at it in my lunch break and post tonight. Cheers.
tux is offline   Reply With Quote
Old 09-11-2007, 05:55 PM   #10
tux
tux's Avatar
 
Join Date: Feb 2007
Location: Isle of Wight, that little island right at the bottom of the UK
Posts: 657
Default

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.

Code:
 
<?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....

Code:
 
<?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??
tux is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:56 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com