943,987 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 8772
  • PHP RSS
Sep 24th, 2006
0

Data from mysql in a 3 columns table

Expand Post »
Hi.
I'm trying to write a script that fetchs image codes from a mysql database to display them into a 3 columns table.

I got this so far:
[php]
$selection = $_GET['scelta'];

$query = 'SELECT photo_code FROM'. $selection .';';
$result = mysql_query($query);
$rows_nb = mysql_num_rows($query);

print('<table width="75%" border="0" align="center">');

while ($row = mysql_fetch_assoc($result))
{
print('<tr>');

for ($tb_rows=0;$tb_rows<3;$tb_rows++)
{
print('<td><div align="center"><a href="show.php?code='. $row['id'] .'"><img src="imagedb/thumbs/'. $row['id'] .'_small.png" border="1" /></a></div></td>');
}

print('</tr>');
}

print('</table>');
[/php]

The for will print 3 columns and then create one more row for the next 3 pictures but now all it does is printing three times the same pic for each row.
How do I change the $row['id'] inside the for cicle?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Sep 24th, 2006
0

Re: Data from mysql in a 3 columns table

I edited my post because the info I placed was inacurate as I misunderstood your problem and I cannot delete the post
Last edited by GliderPilot; Sep 24th, 2006 at 7:49 pm.
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
GliderPilot is offline Offline
57 posts
since Sep 2006
Sep 24th, 2006
0

Re: Data from mysql in a 3 columns table

Okay I understand your problem now... I would have re-edited my post but it won't allow me so I will have ot double post.


You will probably have to add another if statement in there because if you do it this way, and you don't have a number of pictures that is divisable by 3 you will get broken images.

For example if you have 7 images. The first two rows will have images fine, but the third row will have one good image and two broken images.

Try something like this:

 
$selection = $_GET['scelta']; 

$query  = 'SELECT photo_code FROM'. $selection .';'; 
$result = mysql_query($query); 
$rows_nb = mysql_num_rows($query);
 
$pic_num = 0; // Set a new variable for counting our pics, and set it to 0.
$pic_code = mysql_fetch_array($result); Get our array of codes.

print('<table width="75%" border="0" align="center">'); 

while ($row = mysql_fetch_assoc($result)) 
{ 
print('<tr>'); 

for ($tb_rows=0;$tb_rows<3;$tb_rows++) 
{
 
if ($pic_num < $rows_nb)  //check that we have not exceeded the number of pictures
{ 
print('<td><div align="center"><a href="show.php?code='. $pic_code[$pic_num] .'"><img src="imagedb/thumbs/'. $pic_code[$pic_num] .'_small.png"         border="1" /></a></div></td>'); //change our row arrays to pic arrays.
$pic_num++  // Increase the number of pictures we have placed.
}
else
{
print('<td></td>');
}
 
} 

print('</tr>'); 
} 

print('</table>');



Changing you $row['id'] to the new $pic_code[$pic_num] should fix your problem with displaying the same pic as well.
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
GliderPilot is offline Offline
57 posts
since Sep 2006
Sep 25th, 2006
0

Re: Data from mysql in a 3 columns table

Thanks!
I'll see tonight if I can get it working
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Sep 28th, 2006
0

Re: Data from mysql in a 3 columns table

Hi.
The code GliderPilot posted is great, but now it seems like the $pic_code[$pic_num] trick doesn't work.
Only the first picture gets displayed and all the other are broken.
This is what happens
[HTML]
<td><div align="center"><a href="show.php?code=a_011"><img src="imagedb/thumbs/a_011-small.jpg" border="1" /></a></div></td>
<td><div align="center"><a href="show.php?code="><img src="imagedb/thumbs/-small.jpg" border="1" /></a></div></td>
[/HTML]

You can see that the link and location of the first image are ok but all the other aren't.
I tried printing out the $pic_code[$pic_num] value under each picture but apart from the first all are empty.
What should I do?
Reputation Points: 11
Solved Threads: 0
Light Poster
trashed is offline Offline
30 posts
since Oct 2004
Oct 2nd, 2006
0

Re: Data from mysql in a 3 columns table

I know what went wrong. Each time you fetch the array it only gets the first row. So you have to put the fetch array inside the loop so it gets each row (each seperate pic info) like this:

 
 
$selection = $_GET['scelta']; 
 
$query = 'SELECT photo_code FROM'. $selection .';'; 
$result = mysql_query($query); 
$rows_nb = mysql_num_rows($result);
 
$pic_num = 0; // Set a new variable for counting our pics, and set it to 0.
 
 
print('<table width="75%" border="0" align="center">'); 
 
while ($row = mysql_fetch_assoc($result)) 
{ 
 
 
  print('<tr>'); 
 
  for ($tb_rows=0;$tb_rows<3;$tb_rows++) 
  {
 
    $pic_code = mysql_fetch_array($result); //Get our code array for current pic
 
    if ($pic_num <= $rows_nb) //check that we have not exceeded the number of pictures
    { 
 
      print('<td><div align="center"><a href="show.php?code='. $pic_code[0] .'"><img src="imagedb/thumbs/'. $pic_code[0] .'_small.png" border="1" /></a></div></td>'); //change our row arrays to pic arrays.
 
      $pic_num++ // Increase the number of pictures we have placed.
 
    }
    else
    {
      print('<td></td>');
    }
 
  } 
 
  print('</tr>'); 
 
} 
 
print('</table>');

I alos fixed a couple small errors in the code that I missed before. Sorry I wrote it a little quickly and made a couple dumb mistakes. But the code above should work.
Last edited by GliderPilot; Oct 2nd, 2006 at 10:34 pm.
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
GliderPilot is offline Offline
57 posts
since Sep 2006
Feb 6th, 2008
0

Re: Data from mysql in a 3 columns table

The code is not good. With 10 images it return

2,3,4
6,7,8
10

Image no. 1, 5 and 9 is missing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanJ is offline Offline
1 posts
since Feb 2008
Feb 8th, 2008
0

Re: Data from mysql in a 3 columns table

This is how phpBB 3 gets images. Maybe it'll help you out.

PHP Syntax (Toggle Plain Text)
  1. /**
  2. * Get user avatar
  3. *
  4. * @param string $avatar Users assigned avatar name
  5. * @param int $avatar_type Type of avatar
  6. * @param string $avatar_width Width of users avatar
  7. * @param string $avatar_height Height of users avatar
  8. * @param string $alt Optional language string for alt tag within image, can be a language key or text
  9. *
  10. * @return string Avatar image
  11. */
  12. function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR')
  13. {
  14. global $user, $config, $phpbb_root_path, $phpEx;
  15.  
  16. if (empty($avatar) || !$avatar_type)
  17. {
  18. return '';
  19. }
  20.  
  21. $avatar_img = '';
  22.  
  23. switch ($avatar_type)
  24. {
  25. case AVATAR_UPLOAD:
  26. $avatar_img = $phpbb_root_path . "download/file.$phpEx?avatar=";
  27. break;
  28.  
  29. case AVATAR_GALLERY:
  30. $avatar_img = $phpbb_root_path . $config['avatar_gallery_path'] . '/';
  31. break;
  32. }
  33.  
  34. $avatar_img .= $avatar;
  35. return '<img src="' . $avatar_img . '" width="' . $avatar_width . '" height="' . $avatar_height . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
  36. }
SRC: phpBB3.0.0 includes/functions_display.php
Reputation Points: 11
Solved Threads: 5
Junior Poster in Training
Suetan is offline Offline
75 posts
since Feb 2008
Feb 8th, 2008
0

Re: Data from mysql in a 3 columns table

Try this:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $i = 0;
  3. $image_id = '';
  4. $image_path = '';
  5. $td = 0;
  6. echo('<tr>');
  7. while($i <= 10)
  8. {
  9. $sql = "SELECT * FROM ' . IMAGES_TABLE . '
  10. WHERE image_id = $image_id
  11. AND image_path = $image_path";
  12. while($td <=3)
  13. {
  14. echo('<td><img src="');
  15. echo($image_path);
  16. echo('"></td>');
  17. $td++;
  18. if($td == 3)
  19. {
  20. echo('</tr><tr>');
  21. $td = 0;
  22. }
  23. }
  24. $i++;
  25. echo('</tr>');
  26. }

I hope that's going to help some.

Posted a second time because the system wouldn't let me post my edits.

Basicly all I did was take what phpBB3 does and simplify it for displaying multiple images.
Last edited by Suetan; Feb 8th, 2008 at 12:41 am.
Reputation Points: 11
Solved Threads: 5
Junior Poster in Training
Suetan is offline Offline
75 posts
since Feb 2008
Mar 31st, 2010
0
Re: Data from mysql in a 3 columns table
Hi
Maybe this code can do the trick:
PHP Syntax (Toggle Plain Text)
  1. <?
  2. $res = mysql_query([insert your mysql query here]);
  3. $rows = mysql_num_rows($res);
  4. $counter = 1;
  5. $cols = 2;
  6. echo (“<table>\n”);
  7. for($i = 0; $i < $rows/$cols; $i++) {
  8. echo (“<tr>”);
  9. for($j=0; $j < $cols && $counter <= $rows ;$j++, $counter++) {
  10. echo (“<td>[insert the data you want to display here]</td>\n”);
  11. }
  12. echo (“</tr>\n”);
  13. }
  14. echo (“</table>\n”);
  15. ?>
You can see some further explanation here:
<snipped>
Last edited by nav33n; Mar 31st, 2010 at 5:15 am. Reason: URL snipped. Reason: Do not spam, advertise, plug your website, or engage in any other type of self promotion.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tips4php is offline Offline
1 posts
since Mar 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: inserting 2 names from form to mysql
Next Thread in PHP Forum Timeline: session problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC