PDA

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


tux
09-21-2007, 09:54 AM
I have started another thread as my thread on this subject further down the page seems to have gone cold and is getting no replies.

http://www.dreamweaverclub.com/forum/showthread.php?t=26430

If Dj reads this, I have tried to contact you by PM but I think you may have not received the message.

I'm eager not only to solve this problem but to learn Hand Coded PHP too. Please revisit the old post for more.

Thanks, regards

Paul

davidj
09-22-2007, 08:02 PM
i have been very busy and not been able to offer my full attention

i appologise

ive been distracted by going 0>60 in 5.5 seconds

hope you understand

tux
09-22-2007, 09:13 PM
Is that an Audi, I thought you were a BMW guy?? Hope you can find time to help soon. Cheers.

tux
09-28-2007, 10:40 PM
Any thoughts on displaying my images yet David. Thanks.

davidj
09-28-2007, 10:43 PM
ive been very busy lately

ill see if i can look at it over this weekend but i cant promise

tux
09-29-2007, 08:29 AM
Okay David, thanks. If you do get a chance here is my code again. I can only seem to get one image to display. My drop down displays all the names from the DB but when I try to display the images only one displays. Here is my code again. Cheers, have a good weekend.


<?php
require_once("connections/connection.php"); // database connection
///////////////////////////////////////////
$photographerID = $_GET['photographerID'];
$imageID = $_POST{'imageID'};
$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;
}
?>


the drop down...


<form id="menu"
method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<select name="menu" class="photographerlist" onChange="document.forms[0].submit()">
<option value="">Please choose a photographer</option>
<?php do { ?>
<option value="<?php echo $photographerlist['photographerID']; ?>"><?php echo $photographerlist['firstname']; ?> <?php echo $photographerlist['lastname']; ?></option>
<?php }while ($photographerlist = mysql_fetch_array($result)); ?>
</select>
</form>


and for the images....


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


Obviously I'm doing something wrong but I cant see what. Thanks again.

Regards, Paul

tux
10-02-2007, 04:04 PM
Okay everyone, I have been trying to get an answer for this for some time but without any real help.

After death by google and some trial and error I have managed to get the solution. Here is the code.....


///////////////////////////////////////////
$photographerID = $_GET['photographerID'];
$imageID = $_POST{'imageID'};//not needed at this stage
$menu = $_POST['menu'];
///////////////////////////////////////////
///////////////////////////////////////////query for images
$query = sprintf("SELECT thumbURL FROM images where photographerID = '$photographerID'");
$result = @mysql_query($query);
$thumb = @mysql_num_rows($result);
///////////////////////////////////////////
$num_cols = 3; //number of columns required
$num_rows = ceil($thumb / $num_cols); // the number of rows ( total records returned divided by number of columns )
$num = 0; //this zero because the first number of each record inside a record set is zero not 1

///////////////////////////////////////////
$query = sprintf("SELECT * FROM photographers ");
$result2 = @mysql_query($query);
$photographerlist = @mysql_fetch_array($result2);
///////////////////////////////////////////
if($menu){
header("Location: gallery_detail.php?photographerID=$menu");
exit;
}
?>



And here is the code to display the images.......

<?php
echo "<table>";//start the table
for ($rows = 0; $rows < $num_rows; $rows++) {//loop for the table rows
echo "<tr>";
for ($cols = 0; $cols < $num_cols; $cols++) {//loop for the table columns
if ($num < $thumb) { // show records if available (reduce by one because the first record is zero
$thumb_row = mysql_result($result, $num, "thumbURL");//create new variables with the values of the current record
echo "<td>".$thumb_row."<br><td>";
}
else { // show an empty cell
echo "<td>&nbsp;</td>";
}
$num++; // raise the number by one for the next record
}
echo "</tr>"; // no more cols in this row so close the table row
}
echo "</table>"; // close the table
?>

This creates a table and draws the rows and columns populating the cells with the images as it goes.

Hope the comments help anyone understand.

I do still have a problem with it tho'. I have had to fill the database field for the image with the image tag and the full URL of the image. Does anyone know how to break down the $thumb_row variable so the image tag can be hard coded into the script.

Hope this helps someone. Some feedback would be great as the forum seems to have gone very quite.

Cheers all. Regards,

Paul

Duncan
05-18-2011, 03:24 PM
Hi Paul,

I'm new here, and it looks like i'm trying to do the same thing. I have two tables in MySQL that I thought would work out for me. The first table contains product data, and the second one contains the images for the product.

So Product_data has a product_key that gets passed from the master page to the details page... and the Image table contains the Product_Key that could be the same for multiple images (rows). I thought that building the details page using the same Product_key to display multiple images this way would work, but i can only get the first image to print.

I'm using Dreamweaver CS5 with php admin and MySQL. I dont know if that'll help you, but i'm still stuck too :)

- Duncan.

Duncan
05-18-2011, 03:28 PM
This is my code...

<table width="1300" border="0" align="center" cellpadding="1" cellspacing="1">
<tr align="center">
<th scope="col"><img src="Images/Products/<?php echo $row_rs_Images['Image']; ?>" /></th>
</tr>
</table>

Duncan
05-18-2011, 04:25 PM
Well... looks like i figured it out.

Like I said... Two tables. Product details and Images. By creating a repeating group around the Images table it will repeat through all the available images with the same product key.

<?php do { ?>
<img src="Images/Products/<?php echo $row_rs_Images['Image']; ?>" />
<?php } while ($row_rs_Images = mysql_fetch_assoc($rs_Images)); ?>