gpt4 book ai didi

javascript - MySQL:制作一个博客风格的页面,在不同的点有不同的数据类型

转载 作者:可可西里 更新时间:2023-11-01 07:24:44 25 4
gpt4 key购买 nike

我正在尝试用 PHP 和 MySQL 开发类似的东西:http://a-bittersweet-life.tumblr.com/

我想制作一个主要由文字组成的页面,但可以穿插图像或嵌入视频或其他数据类型。我不确定具体该怎么做,但我有一个想法:

id | entry_date |  text | image_link | vid_embed | sound_embed
0 | June 1st | data | null | null | null
1 | June 1st | null | data | null | null
2 | June 1st | null | data | null | null
3 | June 1st | data | null | null | null
4 | June 2nd | data | null | null | null
5 | June 2nd | null | null | data | null
6 | June 2nd | data | null | null | null
7 | June 2nd | null | data | null | null
8 | June 2nd | null | data | null | null
....
....
....

因此,对于每个博客条目,首先显示日期,然后显示数据放入 SQL 表的顺序(按 ID 排序):

6 月 1 日

  • 正文
  • 图片
  • 图片
  • 正文

6 月 2 日

  • 正文

  • 视频

  • 正文

  • 图片

  • 图片

是否有另一种方法可以做到这一点,或者这看起来是一种相当实用的方法吗?

最佳答案

这是一个使用普通 php 的简单示例。如果你使用一些 php 框架你可以将一些部分替换成更简单的,但这里是 GIST。

我已经创建了 2 个函数,因此您可以创建类和方法或构建 MVC 甚至使用这些函数,这只是概念...

<?php
declare(strict_types = 1);
// This function will provide data from DB.
// Here I`ve skipped validation
// because I had relied on PHP7 scalar type declarations.
function getDataFromDB(int $limit, int $offset) {
$db = new \PDO('mysql:dbname={YOUR_DB};host={YOUR_HOST}', '{USER}', '{PASS}');
// Here I`ve used super simple SQL,
// but you can improve it
// and filters by date or author or category or something else...
$sql = sprintf(
'
SELECT *
FROM {YOUR_TABLE_NAME}
ORDER BY entry_date DESC
LIMIT %d OFFSET %d
',
$limit,
$offset
);
// In this query, I`ve only provided params for pagination
// because they are required on the first stage...
$s = $db->prepare($sql);
if (!$s->execute()) {
throw new \RuntimeException($s->errorInfo());
}
// Next param is very IMPORTANT,
// it will group data into array with format where:
// key - date and value - array of posts, like:
// array (
// 'June 1st' => array (
// 0 => array ('text' => '...', 'image_link' => ...),
// 1 => array ('text' => '...', 'image_link' => ...),
// ),
// 'June 2nd' => array (
// 0 => array ('text' => '...', 'image_link' => ...),
// 1 => array ('text' => '...', 'image_link' => ...),
// 2 => array ('text' => '...', 'image_link' => ...),
// 3 => array ('text' => '...', 'image_link' => ...),
// )
// )
return $s->fetchAll(\PDO::FETCH_GROUP | \PDO::FETCH_ASSOC);

}

现在你可以使用这个函数来有目的地从数据库中获取数据并将它传递给下一个函数,该函数将准备 html 或 json 或其他东西......

// This function will render data.
function render(array $data) {
$html = '';
foreach ($data as $day => $posts) {
// Caption for certain day, like 'June 1st' or 'June 2nd'.
$html .= sprintf('<div class="dayPosts"><div class="day">%s</div>', $day);
foreach ($posts as $post) {
// Particular blog post.
// It is super simple HEREDOC example,
// but you can do here anything you wish.
$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;
}

在这里,我使用普通的 php 函数和 purposer 来准备 html,但是你可以使用更好的东西,比如 twigvoltsmarty`或者是其他东西。此外,您还可以将此功能置于 View 中或以某种方式重构...

最后一步,将所有连接在一起:

// Now you can get and render your content.
echo render(getDataFromDB(20, 0));

PS:Ti 只是原始示例...但现在必须知道下一步该怎么做!
希望对您有所帮助! 😉

关于javascript - MySQL:制作一个博客风格的页面,在不同的点有不同的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44357373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com