| | |
Data from mysql in a 3 columns table
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
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?
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?
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.
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:
Changing you $row['id'] to the new $pic_code[$pic_num] should fix your problem with displaying the same pic as well.
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.
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
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]
<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?
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?
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:
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.
$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.
•
•
Join Date: Feb 2008
Posts: 55
Reputation:
Solved Threads: 4
This is how phpBB 3 gets images. Maybe it'll help you out.
SRC: phpBB3.0.0 includes/functions_display.php
PHP Syntax (Toggle Plain Text)
/** * 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) . '" />'; }
•
•
Join Date: Feb 2008
Posts: 55
Reputation:
Solved Threads: 4
Try this:
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.
PHP Syntax (Toggle Plain Text)
<?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.
Last edited by Suetan; Feb 8th, 2008 at 12:41 am.
![]() |
Similar Threads
- writitng data from php to mysql (PHP)
- Error message while importing data to Excel from the MySQL db (MySQL)
- How to make the columns in a table resizable (JavaScript / DHTML / AJAX)
- Script stores data in wrong MySQL columns (MySQL)
Other Threads in the PHP Forum
- Previous Thread: Update HTML page using PHP
- Next Thread: Run time error!!!!!! Pls help...
| Thread Tools | Search this Thread |
ajax apache api array arrays beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external file files folder form forms forum function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail malfunction menu method mlm multiple mysql neutrality oop paypal pdf php phpmysql play problem query question radio random recursion regex remote root script search select server sessions sms soap source space sql syntax system table tutorial update upload url validator variable video web xml youtube





