For whatever reason, this part:
for ($z = 1; $z <= $Number_of_Categories; $z++)
is the offender. even though $Number_of_Categories was defined above where it is used, the value wasn’t holding. I had to build a function instead, and do this:
function get_Number_of_Categories(){
return "2";
}
for ($z = 1; $z <= get_Number_of_Categories(); $z++)
once I did that, the loop worked.