What are best practices for structuring PHP code to display database-driven content?

When displaying database-driven content in PHP, it is best practice to separate your code into different layers for better organization and maintainability. One common approach is to use a Model-View-Controller (MVC) design pattern, where the database interactions are handled in the model, the presentation logic in the view, and the overall control flow in the controller.

// Model (e.g., database connection and query)
class Database {
    public function connect() {
        // Code to connect to the database
    }

    public function getData() {
        // Code to fetch data from the database
    }
}

// Controller
class Controller {
    public function displayData() {
        $db = new Database();
        $data = $db->getData();

        // Pass data to the view
        $view = new View();
        $view->render($data);
    }
}

// View
class View {
    public function render($data) {
        // Code to display data on the webpage
        foreach($data as $row) {
            echo $row['column_name'];
        }
    }
}

// Usage
$controller = new Controller();
$controller->displayData();