php - Bootstrap 模式显示 MySQL 数据两次

标签 php mysql bootstrap-modal

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

相关文章:

asp.net-mvc - 在 Asp.Net MVC 中使用 Bootstrap 模式删除确认

javascript - Bootstrap 模态弹出窗口在 PHP 页面上不起作用 |控制台没有显示错误

php - 从多列中获取结果 MySQL PHP

php - 二叉树,如何统计所有的左右子节点 php, mysql

php - 将字符串编码为json

php - 如何在采购订单表单中插入多个 `product` 和多个其他变量

mysql - 数据库查询选择客户信息

php - JSON 与 PHP Slim 和 AngularJS

php - 为什么我不应该在 PHP 中使用 mysql_* 函数?

javascript - knockout JS : How should "with" or clearnNode be used to deal with Multiplle bindings error with knockout js?