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

标签 javascript php html mysql

我正在尝试用 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/

相关文章:

php - 带连接和运算符的 SQL 查询

javascript - jQuery/JS - 变量似乎没有更新?

javascript - 使用 jquery 更改事件时,tab 键无法按预期工作

javascript - 错误 : Unknown provider: translateFilterProvider <- translateFilter angularjs

javascript - 上传后如何用javascript显示文件名

php - 想要显示按月分组的得分最高者的详细信息

php - 如何在连接表(laravel)中获得最低价格

jquery - 如何清除 DIV 的内容?

javascript - 为什么 jQuery.length == 2?

javascript - 在 Highcharts 中,是否可以为图例符号添加边框?