943,020 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2319
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 11th, 2010
0

Display Images using while loop

Expand Post »
Hi All.

I am trying to display images using the while loop but I want the pictures to be displayed like this.
Click image for larger version

Name:	Photo.png
Views:	235
Size:	7.0 KB
ID:	16585

The only thing is, I don't know how to do it. Any help would be much appreciated.

Thank you in advance
Cameron
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
doctorphp is offline Offline
62 posts
since Apr 2010
Aug 11th, 2010
0
Re: Display Images using while loop
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 ?
Sponsor
Featured Poster
Reputation Points: 546
Solved Threads: 716
Bite my shiny metal ass!
pritaeas is offline Offline
4,142 posts
since Jul 2006
Aug 11th, 2010
0
Re: Display Images using while loop
Click to Expand / Collapse  Quote originally posted by pritaeas ...
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 Syntax (Toggle Plain Text)
  1. <?php
  2. require("check_session.php");
  3. $albumid = $_GET['album'];
  4.  
  5. $sql = mysql_query("SELECT * FROM items WHERE userid='".$_SESSION['id']."' AND albumid='$albumid'");
  6.  
  7.  
  8.  
  9.  
  10. ?>
  11. <font face='arial' size='4'><b><u>Note</u></b><br />When clicking on the public address link, make sure you only give it to people you know and trust.</font><p>
  12. <?php $num22 = mysql_num_rows($sql);
  13.  
  14. if($num22>1)
  15. {
  16. ?>
  17. <form action='changecover.php?album=<?php echo $albumid; ?>' method='post'>
  18. Change album cover to: <select name="newcover"><?php
  19.  
  20. while($row2 = mysql_fetch_assoc($sql))
  21. {
  22. echo "<option value=\"".$row2['location']."\">".$row2['name']."</option>";
  23. }
  24. ?></select><input type='submit' name='changecover' value='Change Cover' /></form>
  25. <?php
  26. }
  27. else echo "";
  28.  
  29. $id = $_SESSION['id'];
  30.  
  31. $album = mysql_query("SELECT name FROM albums WHERE id='$albumid'");
  32. $albumarray = mysql_fetch_assoc($album);
  33.  
  34.  
  35. $image = mysql_query("SELECT * FROM items WHERE userid='$id' AND albumid='$albumid'");
  36.  
  37.  
  38. if(mysql_num_rows($image)<=0)
  39. die( "<font color='#FF0000'><br><br><b>No images uploaded yet.<br /><a href='upload.php'>Upload One?<br /></a></font><br><font size='4'><a href='albums.php'>Back</a>");
  40.  
  41. echo "<br><br><font face='arial' size='4'>Album: ".$albumarray['name']."<table width='100%' cellpadding='7'>";
  42. while ($row = mysql_fetch_assoc($image))
  43. {
  44.  
  45. echo"
  46. <tr>
  47. <td width='220'>
  48. <a href='viewpicture.php?imageid=".$row['id']."'><img src='".$row['location']."' width='265' height='200' border='0'></a><br>
  49. </td>
  50. <td><font face='arial' size='4'>
  51. <b><a href='viewpicture.php?imageid=".$row['id']."'>".$row['name']."</a></b></font><br>
  52. <font face='arial' size='3'>".$row['description']."<br>
  53. <a href='deletepicture.php?picture=".$row['id']."'>Delete</a><br />
  54. <a href='publicimage.php?picture=".$row['pubid']."' target='_blank'>Public Address</a>
  55.  
  56.  
  57. <form method='post' name='change_cover' action='change_cover.php?album=$albumid'>
  58.  
  59.  
  60. </td>
  61. </tr>";
  62. }
  63. echo "</table><font size='4'>";
  64. ?>
  65.  
  66.  
  67. <br />
  68. <a href='upload.php'>Upload another picture</a>
  69. <br />
  70. <a href='albums.php'>Back</a>
  71.  
  72. <?php
  73. include("design/footer.php");
  74. ?>
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
doctorphp is offline Offline
62 posts
since Apr 2010
Aug 12th, 2010
1
Re: Display Images using while loop
Because within the loop you add every image in it's own <tr><td> they will show below one another. You could only open a <tr> when index % 3 == 0 so you get three <td>'s in a single row.
Sponsor
Featured Poster
Reputation Points: 546
Solved Threads: 716
Bite my shiny metal ass!
pritaeas is offline Offline
4,142 posts
since Jul 2006
Aug 12th, 2010
1
Re: Display Images using while loop
here is a basic code
PHP Syntax (Toggle Plain Text)
  1. $a = 1;
  2. echo '<table><tr>';
  3. while($row = mysql_fetch_assoc($image)){
  4. if($a <= 3){ //number of cells in row
  5. echo '<td>'.$row['image'].'</td>';
  6. }
  7. else {
  8. echo '</tr>\n<tr>'.'<td>'.$row['image'].'</td>';
  9. }
  10. $a++;
  11. }
  12. echo '</tr></table>';

Hope this helps

There is a tutorial on printing tables here
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010
Aug 12th, 2010
0
Re: Display Images using while loop
Click to Expand / Collapse  Quote originally posted by metalix ...
here is a basic code
PHP Syntax (Toggle Plain Text)
  1. $a = 1;
  2. echo '<table><tr>';
  3. while($row = mysql_fetch_assoc($image)){
  4. if($a <= 3){ //number of cells in row
  5. echo '<td>'.$row['image'].'</td>';
  6. }
  7. else {
  8. echo '</tr>\n<tr>'.'<td>'.$row['image'].'</td>';
  9. }
  10. $a++;
  11. }
  12. 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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
doctorphp is offline Offline
62 posts
since Apr 2010
Aug 12th, 2010
0
Re: Display Images using while loop
sorry just realised that code won't work.

PHP Syntax (Toggle Plain Text)
  1. $a = 1;
  2. echo '<table><tr>';
  3. while($row = mysql_fetch_assoc($image)){
  4. if($a <= 3){ //number of cells in row
  5. echo '<td>'.$row['image'].'</td>';
  6. $a++;
  7. }
  8. else {
  9. echo '</tr>\n<tr>'.'<td>'.$row['image'].'</td>';
  10. $a = 1;
  11. }
  12. }
  13. 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
Last edited by metalix; Aug 12th, 2010 at 5:32 pm.
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010
Aug 14th, 2010
0
Re: Display Images using while loop
Click to Expand / Collapse  Quote originally posted by metalix ...
sorry just realised that code won't work.

PHP Syntax (Toggle Plain Text)
  1. $a = 1;
  2. echo '<table><tr>';
  3. while($row = mysql_fetch_assoc($image)){
  4. if($a <= 3){ //number of cells in row
  5. echo '<td>'.$row['image'].'</td>';
  6. $a++;
  7. }
  8. else {
  9. echo '</tr>\n<tr>'.'<td>'.$row['image'].'</td>';
  10. $a = 1;
  11. }
  12. }
  13. 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.
Last edited by doctorphp; Aug 14th, 2010 at 1:15 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
doctorphp is offline Offline
62 posts
since Apr 2010
Aug 14th, 2010
0
Re: Display Images using while loop
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.
Reputation Points: 13
Solved Threads: 34
Posting Whiz in Training
metalix is offline Offline
218 posts
since Mar 2010
Aug 17th, 2010
0
Re: Display Images using while loop
Click to Expand / Collapse  Quote originally posted by metalix ...
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.

PHP Syntax (Toggle Plain Text)
  1.  
  2. $a = 1;
  3. echo '<table><tr>';
  4. while($row = mysql_fetch_assoc($image)){
  5. if($a <= 3){ //number of cells in row
  6. echo '<td><img src="'.$row['location'].'" width="260" height="160"></td>';
  7. $a++;
  8. }
  9. else {
  10. echo '</tr>\n<tr>'.'<td>'.$row['name'].'</td>';
  11. $a = 1;
  12. }
  13. }
  14. echo '</tr></table>';

Thanks for your help so far.
Last edited by doctorphp; Aug 17th, 2010 at 8:22 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
doctorphp is offline Offline
62 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: I am New In PHP i have a problem with while loop please help
Next Thread in PHP Forum Timeline: Data not getting displayed in Browser





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC