Whats the best way to output data from a database using php?

Im relatively new to php, and im working on a project using a mysql database. The project consists of users being able to write posts, which are then shown in a list format. The problem is, the posts are shown in different locations on the site, like the index (main) page, and the users profile page. Similar to twitter if your confused. My question is, what is the best way to display the posts? currently I'm using a class that i created. It has functions to retrieve posts from the database, save them all in a multidimensional array. Then another function in the class formats the entire list using foreach, and then returns the formatted HTML list of posts. All i have to do is echo what is returned. But i read somewhere that its bad practice to write functions (especially class functions) that output html. What would be the best way to do this, without having to rewrite the same code on every page the posts are shown. Is it really bad practice to use html in functions?
Example...
a profile page looks something like this.
$class = new Class();
$posts = $class->GetUserPosts($userID);
echo $posts;

And the main page looks something like this
$class = new Class();
$posts = $class->GetAllPosts();
echo $posts.

where each function returns a determined number of posts
 
Back
Top