use $menucode .= instead of $menucode = $menucode . etc. this won't fix your problem, but it will clean up the code a bit :) In general, anytime you are trying to do something like x = x +1 or x = x - 1, there is an operator to do this, ie += and -=, respectively.
there are two problems here...