Re: php/mysql horizontal listing query
| From: | Hugh Bothwell | Date: | Thu, 28 Jun 2001 18:04:58 +0000 |
| Subject: | Re: php/mysql horizontal listing query | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-9974@lists.php.net to get a copy of this message | ||
>""Matt Nigh"" <matt@mattnigh.com> wrote in message
>news:014501c0fe67$945a3400$e31ed718@mountaincable.net...
>hello knowledgeable people of the php-db list,
;-) flattery will get you everywhere ;-)
>I need a section of a record label site with a releases section listed
>horizontally but here's the twist: after every 4 releases, I want the
releases to >move to the next columns below.
>
>ex.
>release#1, release#2, release#3, release#4
>release#5, release#6
>
>each release is stored in a database called almavale_releases and there are
6 >rows:
>id, bandname, albumtitle, coverpic, albumcode, datereleased
Ig... first thing, be precise. Your *table* has six *fields*.
What do you want 'release#1' to look like? Something like 'albumtitle
(bandname)'?
>so what i need to know is what php code do I need to accomplish this?
There's a bit of a trick to it; a table expects the same number of columns
in each row. Most browsers will handle errors anyway, but it's safest to
get it right.
For your example above, the resulting HTML should look like
<table>
<tr>
<td>Release#1</td>
<td>Release#2</td>
<td>Release#3</td>
<td>Release#4</td>
</tr>
<tr>
<td>Release#5</td>
<td>Release#6</td>
<td></td>
<td></td>
</tr>
</table>
Notice the two empty items in the second block!
Try this:
<?php
// connect to database server
mysql_connect("host", "user", "pwd");
mysql_select_db("database");
// fetch data
$res = mysql_query("SELECT bandname, albumtitle FROM
almavale_releases");
$num = mysql_num_rows($res);
// calculate table dimensions
$per_row = 4;
$rows = (int) (($num + $per_row - 1) / $per_row);
$count = 0;
echo "\n<table>";
for ($i = 0; $i < $rows; $i++) {
echo "\n\t<tr>";
for ($j = 0; $j < $per_row; $j++) {
if ($count < $num) {
$it = mysql_fetch_array($res);
$album = $it["albumtitle"];
$band = $it["bandname"];
$item = "$album ($band)";
}
else
$item = "";
echo "\n\t\t<td>$item</td>";
$count++;
}
echo "\n\t</tr>";
}
echo "</table>";
?>