That’s not a proper snippet. Snippets are ONLY php code.
The code in a snippet is run through the PHP eval() function, so you need to gather all output into a variable, then when it’s all finished, return it as the return value of the function.
In your case, you could set up the outer container in the chunk, then call the snippet with the snippet tags like [!SnippetName!] in the chunk where the rows of data are to be displayed. The snippet code would only generate the rows, and return the finished rowset.
Error messages would also be assigned to a variable and returned (exiting the snippet at that point, just like any function).
Here’s a very simple list I wrote some years ago; you can see how snippets work from it:
/**** PopupList **************************
* Usage:
* [[PopupList?id=x]]
*****************************************/
if(!isset($id)) return; // quit if no id
// get the pagetitle and content of the children
// of the given folder, sorted by their menuindex
$children = $modx->getActiveChildren($id,'menuindex','ASC','pagetitle, content');
if($children == false) return; // quit if no documents
$list = '<ul id="popupList">'; // start the list
foreach($children as $child) {
$list .= '<li><a href="#">'.$child['pagetitle'].'<span>'.$child['content'].'</span></a></li>'; // create the links and content blocks
}
$list .= '</ul>'; // close the list
return $list; // and send it off.