我正在尝试显示一个包含多个标签的表格'到给定提案的一列,而不是为每个提案每个标记创建一个新行。 有可能这样做吗?如果是这样的话?我不确定采取什么方法。
目前表格如下:
您可以看到它创建了一个新行,因为该提案有多个标记。我想要它,所以标签列有与提案相关的每个标签(最多只有3个),如人工智能,神经网络,贝叶斯。而不是每一行都有一个单独的行。
php文件是:
<?php
include "db_conx.php";
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db_conx->prepare('SELECT DISTINCT p.proposal_id, p.proposal_title, p.description, c.course_title, t.tag_title FROM proposal p
LEFT JOIN proposal_tags pt on pt.proposal_id = p.proposal_id
LEFT JOIN tag_details t on t.tag_code = pt.tag_code
LEFT JOIN course_details c on c.course_code = p.course_code');
$stmt->execute();
$proposals = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
<div class="btn-group-vertical btn-lg pull-right" role="group">
<div class="btn-group" role="group">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#searchCModal" data-id="#">Search By Course</button>
</div><p/>
<div class="btn-group" role="group">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#searchLModal" data-id="#">Search By Lecturer</button>
</div>
</div>
<!-- <div class="container">
<div class = "container-fluid"> -->
<h4><center>View All Proposals</center></h4>
<div class = "container container-fluid"id = "table_container" style="width:auto; margin-top:50px;" class="mainbox col-md-6">
<div class="row clearfix">
<div class="col-md-12">
<table class="table table-bordered table-hover" id="tab_logic">
<thead>
<tr >
<th class="text-center">
Proposal ID
</th>
<th class="text-center">
Proposal Title
</th>
<th class="text-center">
Proposal Description
</th>
<th class="text-center">
Course
</th>
<th class="text-center">
Tags
</th>
</tr>
</thead>
<tbody>
<!-- populating the table with information from mysql database -->
<?php foreach ($proposals as $proposal) {
echo "<tr><td>";
echo $proposal['proposal_id'];
echo "</td><td>";
echo substr($proposal['proposal_title'],0,30) ."...";
echo "</td><td>";
echo substr($proposal['description'],0,50) ."...";
echo "</td><td>";
echo $proposal['course_title'];
echo "</td><td>";
echo $proposal['tag_title'];
echo "</td><td>";
echo '<p data-placement="top"
data-toggle="tooltip"
style="margin-left:5px"
title="Full View">';
echo '<button class="btn btn-info btn-xs"
data-title="View"
data-toggle="modal"
data-id="';
echo $proposal['proposal_id'];
echo '" data-proposal="';
echo substr($proposal['proposal_title'],0,30) ."...";
echo '" data-desc="';
echo substr($proposal['description'],0,50) ."...";
echo '" data-course="';
echo $proposal['course_title'];
echo '" data-tag="';
echo $proposal['tag_title'];
echo '" data-target="#viewModal">';
echo '<span class="glyphicon glyphicon-eye-open" />';
echo '</button></p>';
echo "</td>";
echo "</td><td>";
echo '<p data-placement="top"
data-toggle="tooltip"
style="margin-left:5px"
title="Add to Favourites">';
echo '<button class="btn btn-success btn-xs"
data-title="Favourites"
data-toggle="modal"
data-id="';
echo $proposal['proposal_id'];
echo '" data-proposal="';
echo substr($proposal['proposal_title'],0,30) ."...";
echo '" data-desc="';
echo substr($proposal['description'],0,50) ."...";
echo '" data-course="';
echo $proposal['course_title'];
echo '" data-tag="';
echo $proposal['tag_title'];
echo '" data-target="#favouritesModal">';
echo '<span class="glyphicon glyphicon-heart" />';
echo '</button></p>';
echo "</td>";
echo "</td><td>";
echo '<p data-placement="top"
data-toggle="tooltip"
style="margin-left:5px"
title="Print">';
echo '<button class="btn btn-primary btn-xs"
data-title="Print"
data-toggle="modal"
data-id="';
echo $proposal['proposal_id'];
echo '" data-proposal="';
echo $proposal['proposal_title'];
echo '" data-desc="';
echo $proposal['description'];
echo '" data-course="';
echo $proposal['course_title'];
echo '" data-tag="';
echo $proposal['tag_title'];
echo '" data-target="#printModal">';
echo '<span class="glyphicon glyphicon-print" />';
echo '</button></p>';
echo "</tr>"; }
?>
</tbody>
</table>
</div>
</div>
</div>
非常感谢任何帮助。感谢
答案 0 :(得分:0)
您需要GROUP_CONCAT(t.tag_title)
,然后GROUP BY p.proposal_id
http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_group-concat