PHP runs from the top down. You put the code that displays the contents of the arrays before the code that puts the stuff in them. They're always empty because the code that puts the results in hasn't been run yet. Move the code block that shows the array entries (lines 19 to 40) to line 319 (after the queries and if (!$q#) stuff but before the footer) and it should work.