我正在尝试使用 PHP 7、MySQL 和 Bootstrap 创建一个搜索模块。唯一的问题是,如果一个角色有更多域,则该角色会使用 2 个不同的域显示两次。
这是弹出窗口 Popup window
这是第 3 个表: Tables
我试图从数据库中选择多个数据并将其添加到数组中,但我是 PHP 和 MySQL 的新手。 这是 PHP 代码:
$output = '';
$sql="SELECT * FROM (SELECT role.role_id AS roleID, role.role_name AS roleNAME, domain.domain_name AS domainNAME
FROM role
LEFT OUTER JOIN role_domain ON role.role_id=role_domain.role_id
LEFT OUTER JOIN domain ON domain.domain_id=role_domain.domain_id
UNION
SELECT role.role_id AS roleID, role.role_name AS roleNAME, domain.domain_name AS domainNAME
FROM role_domain
RIGHT OUTER JOIN domain ON domain.domain_id=role_domain.domain_id
RIGHT OUTER JOIN role ON role.role_id=role_domain.role_id) AS U
WHERE U.roleID='".$_POST["szerep_id"]."'";
$result = mysqli_query($conn,$sql);
// A query that stores the multiple roles
$query="SELECT role.role_name AS roleName, COUNT(role_domain.role_id) as role_count FROM role
LEFT OUTER JOIN role_domain ON role.role_id = role_domain.role_id
GROUP BY role_domain.role_id
HAVING role_count > 1";
$result1= mysqli_query($conn, $query);
// Put the multiple roles to an array
while($row=mysqli_fetch_array($result1))
{
$inspector[]=$row["roleName"];
}
// Create the table
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
// If the query has an error, it writes the error
if($result===false)
{
die(print_r(mysqli_error($conn), true));
}
// Fill up the Popup Window with the information provided
while($row=mysqli_fetch_array($result))
{
$output .= '
<tr>
<td><label><b>Role name:</b></label></td>
<td>'.$row["roleNAME"].'</td>
</tr>
<tr>
<td><label><b>Domain:</b></label></td>
<td>'.$row["domainNAME"].'</td>
</tr>
';
}
$output .= "</table></div>";
echo $output;
// Frees all resources for the specified statement
$stmt=$conn->prepare($sql);
mysqli_stmt_free_result($stmt);
}
}
else
{
echo "Connection could not be established.<br />";
die(print_r(mysqli_error($conn), true));
}
感谢您的帮助!
最佳答案
根据需要显示:
Roles with all their domains
您应该将布局更改为:
.table {
border-collapse: collapse;
width: 100%;
}
.table td, .table th {
border: 1px solid #ddd;
padding: 8px;
}
<table class="table">
<tr>
<td>Role name</td>
<td>role1</td>
</tr>
<tr>
<td>Domains</td>
<td>
<ul>
<li>domain1</li>
<li>domain2</li>
</ul>
</td>
</tr>
</table>
关于 SQL:
将数据存储在数组中,然后对该数组进行分组: Group array values based on key in php?
组将使我可以轻松地以我提供的格式输出表格
更新:如果您不想对数组进行分组,您可以简单地按角色排序查询,并在角色 ID 更改时创建新的角色字段
关于php - Bootstrap 模式显示 MySQL 数据两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49857475/