Ok, I feel like a dork. g I'm sure it's something impossibly obvious that I'm missing just because I've been looking at it too long, but I can't get this to work quite right yet. sigh
Goal: split the results of a mysql query and distribute the results relatively evenly over three columns of a table, filling the first column, then the second, then the third.
Well, the code works when the number of results returned is evenly divisible by three, but only returns one column when the results aren't evenly divisible by three. >.< By the logic I've got set up here, I thought the remainder results should be falling into the last column, but they don't. 🙁
Arg! What am I missing?
function display_cat($cat) {
$sql = "SELECT c.catid, category
FROM subcat as s LEFT JOIN cat as c on subcat=c.catid
WHERE s.catid=\"$cat\" AND active=\"1\" and private=\"0\"
ORDER BY category ASC";
$result = @mysql_query($sql)
or die("Couldn't get the information from the database.");
$num_rows = @mysql_num_rows($result);
$html = "<TR>\n<TD VALIGN=TOP ALIGN=LEFT WIDTH=\"33%\">\n";
$html .= "<P CLASS=\"mainmenu\">";
$partresults = ($num_rows / 3);
$counter = 0;
while($row = mysql_fetch_array($result)) {
$html .= "<A HREF=\"showfic.php?cat=".$row["catid"];
$html .= "\" CLASS=\"submenu\">".$row["category"]."</A><BR>\n";
$counter++;
if ($counter == $partresults) {
$html .= "</P>\n</TD>\n\n";
$html .= "<TD VALIGN=TOP ALIGN=LEFT WIDTH=\"33%\">\n";
$html .= "<P CLASS=\"mainmenu\">";
}
if ($counter == ($partresults * 2)) {
$html .= "</P>\n</TD>\n\n";
$html .= "<TD VALIGN=TOP ALIGN=LEFT WIDTH=\"33%\">\n";
$html .= "<P CLASS=\"mainmenu\">";
}
if ($counter == $num_rows) {
$html .= "</P>\n"; // the row closes in the footer code
}
}
... // page header info query, has no effect on the columns
echo $html;
}
Thanks so much for the help. 🙂