Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
asgozzi
Forum Newbie
Posts: 3 Joined: Sun Sep 24, 2006 5:34 pm
Location: Modena, Italy
Post
by asgozzi » Sun Sep 24, 2006 5:39 pm
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:
Code: Select all
$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>');
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?
scorphus
Forum Regular
Posts: 589 Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:
Post
by scorphus » Sun Sep 24, 2006 6:10 pm
Try this:
Code: Select all
$query = 'SELECT photo_code AS id FROM '. $selection;
-- scorphus
asgozzi
Forum Newbie
Posts: 3 Joined: Sun Sep 24, 2006 5:34 pm
Location: Modena, Italy
Post
by asgozzi » Sun Sep 24, 2006 6:27 pm
scorphus wrote: Try this:
Code: Select all
$query = 'SELECT photo_code AS id FROM '. $selection;
-- scorphus
Yeah you're right I pasted the code from the wrong file
Anyway the problem is still there because when the FOR prints 3 times the same image.
Let's say the first result from the db is
img001 then in the WHILE loop
$row['id'] =
img001 but inside the FOR loop is executed three times and prints three times the same table cell, containing image
img001 _small.png
This will only change when FOR ends and WHILE goes to next query result.
I really don't know how to fix this
scorphus
Forum Regular
Posts: 589 Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:
Post
by scorphus » Sun Sep 24, 2006 6:43 pm
Ok, let's try again:
Code: Select all
$selection = $_GET['scelta'];
$query = 'SELECT photo_code AS id 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++) {
if (!empty($row)) {
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>');
}
else {
print('<td> </td>');
}
if ($tb_rows < 2) {
$row = mysql_fetch_assoc($result);
}
}
print('</tr>');
}
print('</table>');
Remember that this is not (by far) the best way to do what you want in this case, it just _fixes_ your code
Hope that helps!
asgozzi
Forum Newbie
Posts: 3 Joined: Sun Sep 24, 2006 5:34 pm
Location: Modena, Italy
Post
by asgozzi » Sun Sep 24, 2006 7:06 pm
thanks!
it's just for a small thing i'm doing but I'll go on from here and try to make it better