I'm working on a link-management script, with a cool sorting feature... The idea is that it counts the total amount of links and then devides them equally over a pre-defined set of coloms, whilst remaining listed under its parent categorie.
Some examples:
In case of 34 links and 3 cols: 12-11-11
In case of 65 links and 4 cols: 17-16-16-16
In case of 109 links and 2 cols: 55-54
Notice how the first number is rounded up (like with php's ceil function).
I'm afraid it doesn't stop there... Every link is categorized and should remain listed under its parent category without breaking up. So if we had the following situation:
cat 1 (containing 9 links)
cat 2 (containing 5 links)
cat 3 (containing 2 links)
cat 4 (containing 8 links)
cat 5 (containing 4 links)
the outcome should be a table that's something like:
+--------------+
|[b]CAT4[/b]|[b]CAT2[/b]|[b]CAT1[/b]|
|Lnk1|Lnk1|Lnk1|
|Lnk2|Lnk2|Lnk2|
|Lnk3|Lnk3|Lnk3|
|Lnk4|Lnk4|Lnk4|
|Lnk5|Lnk5|Lnk5|
|Lnk6| |Lnk6|
|Lnk7|[b]CAT5[/b]|Lnk7|
|Lnk8|Lnk1|Lnk8|
| |Lnk2|Lnk9|
|[b]CAT3[/b]|Lnk3| |
|Lnk1|Lnk4| |
|Lnk2| | |
+--------------+
Let me explain that; First of all php should do some math (28/3) to be able to come up with 3 ($cols) almost equal numbers: 10, 9, 9. Then, it should take a look at the hierarchy of the links and then, based on those 3 numbers, calculate how to devide the cats in such a way they are devided as equally as possible.
r u still with me..? 😃
Got something going here: http://links.nr78.net : but the three coloms should've been as equally devided as possible.
So far i came up with this (stripped out some parts for the sake of clarity):
<?php
$cols = 3;
$result = mysql_query("SELECT * FROM $tbl[cat] ORDER BY 'name' ASC");
while ($row = mysql_fetch_array($result)) {
$catid = $row["id"];
$naam = $row["naam"];
$links .= '<p><span class="header">'.$naam.'</span><br />'."\n";
$sql = "SELECT * FROM $tbl[links] WHERE catid = '$catid'";
$res = mysql_query($sql) or die (mysql_error());
// links
while ($list = mysql_fetch_object($res)) {
$links .= '· <a>'.$list->naam.'</a><br />'."\n";
}
}
// calculate table
echo '<table cellpadding="5" cellspacing="1"><tr>';
for($i=1;$i<=$cols;$i++) {
echo '<td bgcolor="#eeeeee" nowrap>'.$links[$i].'</td>';
}
echo '</tr></table>';
?>
As you can probably see it doesn't even come close to what i described above 🙁
Geez, hope i'm made myself clear and i hope someone is able to help me, 'cause i ain't got much hair left...
Thanks in advance!
appel