There are some things I want to say about your code before helping you with the problem you are asking about. First you can merge both queries, that way you only need to do one query instead of one + one per result. It could be done this way:
$typesSQL = "(
SELECT taskcode, taskname, tu, sum(tu) AS 'total'
FROM user_tasks
WHERE custid LIKE '$username'
AND buildid LIKE '$buildingid'
AND tu > 0)
ORDER BY taskcode";
Second you should NOT use LIKE in queries if you don't need to. Assume that I have username 'dg' and another person have username 'badge'. When searching with LIKE I would get his results as well. Instead use = as below:
$typesSQL = "(
SELECT taskcode, taskname, tu, sum(tu) AS 'total'
FROM user_tasks
WHERE custid = '$username'
AND buildid = '$buildingid'
AND tu > 0)
ORDER BY taskcode";
Then for your actual question. You have two arrays that you want to handle in the loop, that makes foreach a bad choise. Instead I suggest that you use a for-loop like below:
for ($i = 0; $i < count($types); $i++)
{
$pdf->SetX (50);
$pdf->Cell (80,15,$i, 1, 0, 'C');
$pdf->Cell (450,15, " ".$types[$i], 1, 0);
$pdf->Cell (250,15, $assoc[$i], 1, 1, 'C');
}
Or it may be possible and probably better to use the loop you already have, something like this:
$typesSQL = "(SELECT taskcode, taskname, tu, sum(tu) AS 'total' FROM user_tasks WHERE custid = '$username' AND buildid = '$buildingid' AND tu > 0) ORDER BY taskcode";
$typesResults = mysql_query ($typesSQL);
while ($typesRow = mysql_fetch_assoc ($typesResults)){
$test = $typesRow["taskcode"];
$types[$test] = $typesRow["taskname"];
$assoc[$test] = $typesRow["total"];
$pdf->SetX (50);
$pdf->Cell (80,15,$test, 1, 0, 'C');
$pdf->Cell (450,15, " ".$types[$test], 1, 0);
$pdf->Cell (250,15, $assoc[$test], 1, 1, 'C');
}