Display Images using while loop Hi All.
I am trying to display images using the while loop but I want the pictures to be displayed like this.
The only thing is, I don't know how to do it. Any help would be much appreciated.
Thank you in advance
Cameron
Attachments
doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
You can use a table, or use div's with css positioning. It depends a bit on what your page html looks like. What is your code ?
pritaeas
Posting Expert
5,446 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
You can use a table, or use div's with css positioning. It depends a bit on what your page html looks like. What is your code ?
This is the page where you can view the pictures but I would like it to look like the picture I attached with my original post.
<?php
require("check_session.php");
$albumid = $_GET['album'];
$sql = mysql_query("SELECT * FROM items WHERE userid='".$_SESSION['id']."' AND albumid='$albumid'");
?>
<font face='arial' size='4'><b><u>Note</u></b>When clicking on the public address link, make sure you only give it to people you know and trust.</font><p>
<?php $num22 = mysql_num_rows($sql);
if($num22>1)
{
?>
<form action='changecover.php?album=<?php echo $albumid; ?>' method='post'>
Change album cover to: <select name="newcover"><?php
while($row2 = mysql_fetch_assoc($sql))
{
echo "<option value=\"".$row2['location']."\">".$row2['name']."</option>";
}
?></select><input type='submit' name='changecover' value='Change Cover' /></form>
<?php
}
else echo "";
$id = $_SESSION['id'];
$album = mysql_query("SELECT name FROM albums WHERE id='$albumid'");
$albumarray = mysql_fetch_assoc($album);
$image = mysql_query("SELECT * FROM items WHERE userid='$id' AND albumid='$albumid'");
if(mysql_num_rows($image)<=0)
die( "<font color='#FF0000'><b>No images uploaded yet.<a href='upload.php'>Upload One?</a></font><font size='4'><a href='albums.php'>Back</a>");
echo "<font face='arial' size='4'>Album: ".$albumarray['name']."<table width='100%' cellpadding='7'>";
while ($row = mysql_fetch_assoc($image))
{
echo"
<tr>
<td width='220'>
<a href='viewpicture.php?imageid=".$row['id']."'><img src='".$row['location']."' width='265' height='200' border='0'></a>
</td>
<td><font face='arial' size='4'>
<b><a href='viewpicture.php?imageid=".$row['id']."'>".$row['name']."</a></b></font>
<font face='arial' size='3'>".$row['description']."
<a href='deletepicture.php?picture=".$row['id']."'>Delete</a>
<a href='publicimage.php?picture=".$row['pubid']."' target='_blank'>Public Address</a>
<form method='post' name='change_cover' action='change_cover.php?album=$albumid'>
</td>
</tr>";
}
echo "</table><font size='4'>";
?>
<a href='upload.php'>Upload another picture</a>
<a href='albums.php'>Back</a>
<?php
include("design/footer.php");
?>
doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
Because within the loop you add every image in it's own they will show below one another. You could only open a when index % 3 == 0 so you get three 's in a single row.
pritaeas
Posting Expert
5,446 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
here is a basic code
$a = 1;
echo '<table><tr>';
while($row = mysql_fetch_assoc($image)){
if($a <= 3){ //number of cells in row
echo '<td>'.$row['image'].'</td>';
}
else {
echo '</tr>\n<tr>'.'<td>'.$row['image'].'</td>';
}
$a++;
}
echo '</tr></table>';
Hope this helps :)
There is a tutorial on printing tables here
Ok thank you so, so much for your help.
I really appreciate it.
doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
sorry just realised that code won't work.
$a = 1;
echo '<table><tr>';
while($row = mysql_fetch_assoc($image)){
if($a <= 3){ //number of cells in row
echo '<td>'.$row['image'].'</td>';
$a++;
}
else {
echo '</tr>\n<tr>'.'<td>'.$row['image'].'</td>';
$a = 1;
}
}
echo '</tr></table>';
that will work better.
$a determines where it is at in a row.
at the end of the row it needs to reset to 1 :)
Thanks but it doesn't work.
doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
if your recieving an error please post it here. you may not be connecting to your database correctly.
have you included $image=mysql_query.....
otherwise look at your source code -> when you open the page in the browser, and see whats happening.
Im am not recieving any errors and it is connecting to the database but all it does is shows two pictures and doesn't show the name of the image. Here is what I did to your code.
$a = 1;
echo '<table><tr>';
while($row = mysql_fetch_assoc($image)){
if($a <= 3){ //number of cells in row
echo '<td><img src="'.$row['location'].'" width="260" height="160"></td>';
$a++;
}
else {
echo '</tr>\n<tr>'.'<td>'.$row['name'].'</td>';
$a = 1;
}
}
echo '</tr></table>';
Thanks for your help so far.
doctorphp
Junior Poster in Training
67 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0