Wrap php in shortcode

From Codex Shortcode Examples

// In case there is opening and closing shortcode.
echo do_shortcode(‘[iscorrect]’.$text_to_be_wrapped_in_shortcode.'[/iscorrect]’);

Basically, you can’t use do_shortcode() two separate times for what is one shortcode consisting of a pair of tags.

In your code you need to save your output to variables instead of using echo. Then at the end wrap it in the shortcode like the example.