Forum Moderators: coopster
I would like to output the information into a table so that users don't have to scroll so much.
I would like the table to be 3 or 4 cells wide.
[table]
[tr]
[td]IMAGE 1[/td]
[td]IMAGE 2[/td]
[td]IMAGE 3[/td]
[/tr]
[tr]
[td]IMAGE 4[/td]
etc....
Here is the code I have so far.
// generate and execute query
$query = "SELECT id, country, region, flag FROM countries WHERE region = '$region' ORDER BY country ASC";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print title with links to edit and delete scripts
while($row = mysql_fetch_object($result))
{
?>
<tr>
<td align="center"><a href="country.php?country=<? echo $row->id;?>®ion=<? echo $region;?>"><img border="0" src="images/flags/<? echo $row->flag;?>"><br><b><? echo $row->country;?></a></b></td>
</tr>
<?
}
}
// if no records present
// display message
else
{
?>
<font size="-1">No countries currently available</font><p>
<?
}
// close connection
mysql_close($connection);
?>
</table>
Cursory look and the PHP looks okay. Are you not getting the result you want?
As for the HTML....
I would like the table to be 3 or 4 cells wide.
Use CSS with a left float. Then you can minimize both vertical and horizontal scrolling (if you don't know how, post to the CSS forum).
Also, try running your code through a validator.
<a href="country.php..."><img border="0" ...><br><b>...</a></b>
A validator will tell you
- tags are not properly nested
- border may be decprecated (depending on the x/html version)
Also, instead of <b>, use CSS.
What I would like to do is have php loop through 3 [td] cells, then close of the table with a [/tr] and open a new one to continue printing out the results from the DB. I remeber looking at a Perl script that had a similar function a few years ago, but can't remember how it was done.
$tdcount = 1;
$numtd = 3;
echo "<table>";
while($row = mysql_fetch_object($result)) {
if ($tdcount == 1) echo "<tr>";
echo "<td>some stuff: $tdcount</td>";
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// time to close up our table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td> </td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";
by the way, untested, just off the top of my head. I wanted a dynamic one that would allow you to change the value in $numtd to adjust the number of cells across. I may have messed up my counting, I wrote it really quickly. :)