, php. , , GIST.
2 , MVC , ...
<?php
declare(strict_types = 1);
function getDataFromDB(int $limit, int $offset) {
$db = new \PDO('mysql:dbname={YOUR_DB};host={YOUR_HOST}', '{USER}', '{PASS}');
$sql = sprintf(
'
SELECT *
FROM {YOUR_TABLE_NAME}
ORDER BY entry_date DESC
LIMIT %d OFFSET %d
',
$limit,
$offset
);
$s = $db->prepare($sql);
if (!$s->execute()) {
throw new \RuntimeException($s->errorInfo());
}
return $s->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_ASSOC);
}
db , html json - ...
function render(array $data) {
$html = '';
foreach ($data as $day => $posts) {
$html .= sprintf('<div class="dayPosts"><div class="day">%s</div>', $day);
foreach ($posts as $post) {
$html .= <<<"BLOGPOST"
<div class="post">
<h3>{$post['title']}</h3>
<div class="content">
<img src="{$post['image_link']}">
<p class="text"{$post['test']}></p>
<video controls>
<source src="{$post['vid_embed']}" type="video/mp4">
</video>
</div>
</div>
BLOGPOST;
}
$html .= '</div>';
}
return $html;
}
ve used plain php functions with purposer to prepare html, but you cna use something better like twig or volt or smarty` - . Alsom -...
, :
echo render(getDataFromDB(20, 0));
PS: Ti - ... , !
, ! π