Read Records
Adopt MVC for the read records feature.
We'll cover the following...
Our project has adopted the MVC architecture thus far. Now it’s time for us to refactor the read records feature so it becomes MVC friendly.
Let’s first consider our previous code to get a list of projects.
The non-MVC code
<?php
try {
require "./config.php";
$connection = new PDO($dsn, $username, $password, $options);
$sql = 'SELECT * FROM projects ORDER BY title';
$statement = $connection->query($sql);
$projectCount = $statement->rowCount();
} catch (PDOException $err) {
echo $sql . "<br>" . $err->getMessage();
}
?>
<div>
<h1>Project list (<?php echo $projectCount ?>)</h1>
<!-- If there's not yet data -->
<?php
if ($projectCount <= 0) {
echo "<p>You have not yet added any project </p>";
echo "<p><a href='#'>Add project</a></p>";
}
?>
<?php foreach ($statement as $row) : ?>
<ul>
<li>
<?php echo $row["title"] ?>
</li>
...