【问题标题】:Retrieving multiple tags per article into one Bootstrap table column将每篇文章的多个标签检索到一个 Bootstrap 表列中
【发布时间】:2022-01-18 16:33:14
【问题描述】:

我有一个可排序的 Bootstrap 表,其中有一个 foreach(我很自豪),它从我的文章 db 表中填充该表。

目前,我的文章表有 5 个额外的代表标签的列,每个列都是单独填充的。

foreach 完全按照要求工作(在此下方有 JS 用于排序,但已从此处排除)。

    <div class="MTable">
    <?php
    include 'conn.php';
    $result = $conn->query("SELECT * FROM table_a WHERE Column_1 ='Value' OR Column_2 ='Value'");
    $table_a = $result->fetch_all(MYSQLI_ASSOC);
    ?>

   <table id="table_1" class="table display table-sortable table-hover table-dark">
    <thead>
        <tr>
            <th onclick="sortTable(0)">Column 1</th>
            <th onclick="sortTable(1)">Column 2</th>
            <th onclick="sortTable(2)">Column 3</th>
            <th onclick="sortTable(3)">Column 4</th>
            <th onclick="sortTable(4)">Column 5</th>
            <th onclick="sortTable(5)">Column 6</th>
            <th onclick="sortTable(6)">Column 7</th>
        </tr>
        </thead>
       
  <tbody>      
     <?php foreach($table_a as $return) : ?>
        <tr>
            <td><?= $return['Column_1']; ?></td>
            <td><?= $return['Column_2']; ?></td>
            <td><?= $return['Tag_1']; ?></td>
            <td><?= $return['Tag_2']; ?></td>
            <td><?= $return['Tag_3']; ?></td>
            <td><?= $return['Tag_4']; ?></td>
            <td><?= $return['Tag_5']; ?></td>
       </tr>
    <?php endforeach; ?>
   </tbody>
   </table>

我已经开始构建一个标记系统,并计划从文章表中删除这额外的 5 列。

做了一些研究并查看了以前关于堆栈溢出的问题,并构建了一个三表系统(“毒”解决方案)。

我的表格布局如下,斜体代表外键;

  • Table_a(文章)article_id,更多列
  • Table_b(标签)tag_id、tag_name
  • Table_c (Articles_Tags) id、article_idtag_id

目标:

我想将 3-7 列合并为一列,并返回以 ; 分隔的所有 tag_names在那一列里面。像这样的..

AS-IS and TO-BE BS Table Design

我可以使用 INNER JOIN 来组合我的三个表(如下所示),但我不确定如何处理多个查询和管理 foreach。有点出乎我的意料。

SELECT tag_name
      FROM table_a
          INNER JOIN table_c
                  ON table_c.article_id = table_a.article_id 
          INNER JOIN table_b
                  ON table_c.tag_id = table_b.tag_id
       WHERE table_c.article_id = 'SelectedArticleName'

【问题讨论】:

    标签: php mysql database twitter-bootstrap foreach


    【解决方案1】:

    这可以通过 MySQL 的GROUP_CONCAT 功能实现(检查您的 MySQL 版本和相关文档)。它使您能够在单个请求中获取所有数据。它会是这样的:

    SELECT article_id, article_name,
           GROUP_CONCAT(DISTINCT tag_name, 
                      ORDER BY tag_name DESC SEPARATOR ';') AS tags
      FROM table_a
          INNER JOIN table_c
                  ON table_c.article_id = table_a.article_id 
          INNER JOIN table_b
                  ON table_c.tag_id = table_b.tag_id 
          GROUP BY table_a.article_id, table_a.article_name
    

    注意 GROUP_CONCAT 的结果长度是有限的

    结果被截断为 group_concat_max_len 系统变量给出的最大长度,其默认值为 1024。该值可以设置得更高,尽管返回值的有效最大长度受值的限制max_allowed_pa​​cket。

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多