954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Data from mysql in a 3 columns table

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('');

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

for ($tb_rows=0;$tb_rows<3;$tb_rows++) { print(''. $row['id'] .'_small.png'); }

print('');
}

print('');
[/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?

trashed
Light Poster
30 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

I edited my post because the info I placed was inacurate as I misunderstood your problem and I cannot delete the post

GliderPilot
Junior Poster in Training
86 posts since Sep 2006
Reputation Points: 8
Solved Threads: 4
 

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.

GliderPilot
Junior Poster in Training
86 posts since Sep 2006
Reputation Points: 8
Solved Threads: 4
 

Thanks!
I'll see tonight if I can get it working

trashed
Light Poster
30 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

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] a_011-small.jpg -small.jpg [/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?

trashed
Light Poster
30 posts since Oct 2004
Reputation Points: 11
Solved Threads: 0
 

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.

GliderPilot
Junior Poster in Training
86 posts since Sep 2006
Reputation Points: 8
Solved Threads: 4
 

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.

DanJ
Newbie Poster
1 post since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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

/**
* Get user avatar
*
* @param string $avatar Users assigned avatar name
* @param int $avatar_type Type of avatar
* @param string $avatar_width Width of users avatar
* @param string $avatar_height Height of users avatar
* @param string $alt Optional language string for alt tag within image, can be a language key or text
*
* @return string Avatar image
*/
function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR')
{
	global $user, $config, $phpbb_root_path, $phpEx;

	if (empty($avatar) || !$avatar_type)
	{
		return '';
	}

	$avatar_img = '';

	switch ($avatar_type)
	{
		case AVATAR_UPLOAD:
			$avatar_img = $phpbb_root_path . "download/file.$phpEx?avatar=";
		break;

		case AVATAR_GALLERY:
			$avatar_img = $phpbb_root_path . $config['avatar_gallery_path'] . '/';
		break;
	}

	$avatar_img .= $avatar;
	return '<img src="' . $avatar_img . '" width="' . $avatar_width . '" height="' . $avatar_height . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
}

SRC: phpBB3.0.0 includes/functions_display.php

Suetan
Junior Poster in Training
76 posts since Feb 2008
Reputation Points: 11
Solved Threads: 5
 

Try this:

<?php
$i = 0;
$image_id = '';
$image_path = '';
$td = 0;
echo('<tr>');
while($i <= 10)
{
     $sql = "SELECT * FROM ' . IMAGES_TABLE . '
     WHERE image_id = $image_id
     AND image_path = $image_path";
     while($td <=3)
     {
          echo('<td><img src="');
          echo($image_path);
          echo('"></td>');
          $td++;
          if($td == 3)
          {
               echo('</tr><tr>');
               $td = 0;
          }
     }
     $i++;
     echo('</tr>');
}


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.

Suetan
Junior Poster in Training
76 posts since Feb 2008
Reputation Points: 11
Solved Threads: 5
 

Hi
Maybe this code can do the trick:

<?
$res = mysql_query(“[insert your mysql query here]“);
$rows = mysql_num_rows($res);
$counter = 1;
$cols = 2;
echo (“<table>\n”);
for($i = 0; $i < $rows/$cols; $i++) {
echo (“<tr>”);
for($j=0; $j < $cols && $counter <= $rows ;$j++, $counter++) {
echo (“<td>[insert the data you want to display here]</td>\n”);
}
echo (“</tr>\n”);
}
echo (“</table>\n”);
?>

You can see some further explanation here:

tips4php
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You