Wrapping the for each loop in conditionals so that it avoids the error " Invalid argument supplied for foreach()"

@kgibs this will fix the error