gpt4 book ai didi

php - 可以在一列php中显示来自mysql的多个值

转载 作者:行者123 更新时间:2023-11-29 22:43:48 24 4
gpt4 key购买 nike

我正在尝试显示一个表格,该表格在给定提案的一列中具有多个“标签”,而不是为每个提案每个标签创建一个新行。有可能这样做吗?如果是这样怎么办?我不确定该采取什么方法。

当前表格如下所示:

Proposal Table

如您所见,它创建了一个新行,因为提案有多个标签。我希望标签列包含与提案相关的每个标签(最多只有 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>

任何帮助将不胜感激。谢谢

最佳答案

您需要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

关于php - 可以在一列php中显示来自mysql的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29218358/

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