getAttribute() undefined, use attributes(), remove extra endforeach

getAttribute() undefined, use attributes(); remove extra endforeach - had to make these changes just to get it to run without PHP errors.