Forum Moderators: coopster
I would like to know how to arrange a list of sql query results into a 3-column (i.e. 3-cell) table, such that they read alphabetically going down column 1, then column2, etc.
So far I have only found advice for arranging values across table columns.
Any help will be gratefully appreciated.
Here's the code I'm currently using (single-cell table):
echo '<table class="catlist"><tr><td>';
while ($row = mysql_fetch_array($result)) {
echo '<a href="list.php?no=',$row['id'],'">',$row['category'],'</a> <font size="-1">[Fics: x]</font><br>';
}
echo '</td></tr></table>';
~FairyV
Try something like:
$rows_returned = mysql_num_rows($result);
$base = $rows_returned/3;
$first_column = $second_column = $third_column = $base;
if ($base % 3 > 0) {
$first_column++;
}
if ($base % 3 > 1) {
$second_column++;
}
$first_break = $first_column;
$second_break = $first_column + $second_column;
echo '<table class="catlist"><tr><td>';
for ($i = 1; $i <= $rows_returned; $i++) {
$row = mysql_fetch_row ($result);
echo '<a href="list.php?no=',$row['id'],'">',$row['category'],'</a><font size="-1">[Fics: x]</font><br>';
if ($i == $first_break ¦¦ $i == $second_break) {
echo '</td></td>';
}
}
echo '</td></tr></table>';
arran.
Thank you for that script. However, I am getting a parse error for the line:
if ($i == $first_break ¦¦ $i == $second_break) {
Since I don't fully understand what it means I cannot correct it.
I also wondered whether the following line:
echo '</td></td>';
should read:
echo '</td></tr>';
With thanks, FairyV