At First you should consider that using user input data directly in SQL query without sanitizing it, is very dangerous and is not recommended!
2) it is better to set default value for parent_id to 0, I'm not sure null will get same result
3) You may escape date and name column in your mysql query it can conflict with mysql native vars & functions
<?php
//Code in view
$comments = getComments($cmtid,$page_type);
echoComments($comment);
//This method will take all Comments with their replies at once;
function getComments($cmtid,$page_type,$parent=0)
{
GLOBAL $mysqli;
$comments = $mysqli->query("SELECT `id`,parent_id,member,`name`,email,ip,`comment`,img,`date`
FROM cs_comments
WHERE (row_id='{$cmtid}' AND page_type='{$cmtt}' AND parent_id = {$parent})
ORDER BY date(format)");
$comment_data = array();
while($comments_array = $comments->fetch_object())
{
$comments_array->replies = $comments_array->parent_id ? getComments($cmtid,$page_type,$comments_array->parent_id) : array();
$comment_data[] = $comments_array;
}
return $comment_data;
}
//This method will print comment with replies
function echoComments($comment_data)
{
foreach ($comment_data as $comment):
echo '<div class="comment">
'.$comment->comment.'
<hr />
'.echoComments($comment->replies).'
</div>';
endforeach;
}
?>
You can use this style sheet to give some indent for nested replies:
<style>
.comment{
background:#fff;
}
.comment .comment{
margin-left:5px;
background:#eee;
}
<style>