Here's my code and the result (without names being paired up) can be seen in
http://www.brfskalen11.org/claire/test5.php
If I add your code (see further below how I did that) the result can be seen in
http://www.brfskalen11.org/claire/test6.php
$hostname = "localhost";
$mysqluser = "xxx";
$mysqlpassword = "xxx";
$medlemmardb = "xxx";
$andelardb = "xxx";
function initdb($db) {
global $hostname, $mysqluser, $mysqlpassword;
mysql_pconnect($hostname,$mysqluser,$mysqlpassword) or die("Unable to connect to SQL server");
mysql_select_db($db) or die("Unable to select database");
}
$startWithWeek = 1; // The week of the current year to start with
$numberOfWeeks = 52; // How many weeks to distribute
$startWithPerson = 2; // The person to start with (counting from 0 onward)
$currentYear = 2002; // The current year
$distribution = array(); // This will store the actual distribution
// Populate the $persons array from the DB
initdb($medlemmardb);
$svar = mysql_query("SELECT Andel, substring_index(Namn,' ',1) AS surname FROM Uppgifter WHERE Andel<=23 OR Andel >=49 GROUP BY Andel, surname ORDER BY Andel, Namn ") or die ("unable to perform query");
while ($row = mysql_fetch_array ($svar)) {
$persons[] = $row['surname'];
}
// Fill the first few arrayspots up to the starting person with 0s to make table creation easier later
for ($i = 0; $i < $startWithPerson; $i++) {
$distribution[$i][0] = 0;
}
// Distribute the weeks among the persons
for ($i = $startWithWeek; $i < $numberOfWeeks + $startWithWeek; $i++) {
$distPersonIndex = ($startWithPerson + $i - $startWithWeek) % sizeof($persons);
$newWeekIndex = isset($distribution[$distPersonIndex]) ? sizeof($distribution[$distPersonIndex]) : 0;
$distribution[$distPersonIndex][$newWeekIndex] = $i;
}
// Fill the last few arrayspots from the last person till the end of that "column" with 0s (for table creation)
for ($i = ($startWithPerson + $numberOfWeeks - 1) % sizeof($persons) + 1; $i < sizeof($persons); $i++) {
$newWeekIndex = isset($distribution[$i]) ? sizeof($distribution[$i]) : 0;
$distribution[$i][$newWeekIndex] = 0;
}
// $distribution is now a matrix with each row corresponding to the weeks assigned a particular person
// print the results in a table
// The outer for loop: Print a row to display each person's information
// The inner for loop: Print a cell for each week assigned to the current person
// The if statement: Check whether the array contains a 0 (replace that with a blank space), or if the week nr is bigger 52
// in which case it's in on of the next years.
echo "<table cellpadding=\"2\" border=\"1\">";
for ($i = 0; $i < sizeof($distribution); $i++) {
echo "<tr><td width=\"100\">";
echo $persons[$i]." ";
echo "</td>";
for ($j = 0; $j < sizeof($distribution[$i]); $j++) {
if ($distribution[$i][$j] == 0) {
$toPrint = " ";
} else if ($distribution[$i][$j] > 52) {
$toPrint = ((($distribution[$i][$j]-1) % 52)+1)." år ".($currentYear + intval(($distribution[$i][$j] - 1) / 52));
} else {
$toPrint = $distribution[$i][$j];
}
echo "<td nowrap width=\"30\">".$toPrint."</td>";
}
echo "</tr>";
}
echo "</table>";
When I added your code I made the following adjustments:
while ($row = mysql_fetch_array ($svar)) {
$list[] = $row['surname'];
}
//Group people with matching pairs:
foreach($list as $person)
{ $name = $person['Namn'];
$pair = $person['Andel'];
$persons[$pair][]=$name;
}
//Turn those arrays into space-separated strings.
foreach($persons as $k=>$v)
$persons[$k]=join(', ',$v);
// $persons is an associative
// array of strings indexed by $pair.
// To make it numerically-indexed:
$persons = array_values($persons);
// Fill the first few arrayspots up to the starting person with 0s to make table creation easier later
Can anyone help me get this right?