bootstrap模式两次显示mysql数据

cgvd09ve  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(262)

我正在尝试用PHP7、mysql和bootstrap创建一个搜索模块。唯一的问题是,如果一个角色有多个域,那么该角色将在两个不同的域中显示两次。
这是弹出窗口
这是3张table:table
我试图从数据库中选择多个数据并将其添加到一个数组中,但我对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));
}

谢谢你的帮助!

njthzxwz

njthzxwz1#

当您需要显示时:
角色及其所有域
您应该将布局更改为:

.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:
将数据存储在数组中,然后对数组进行分组:在php中根据键对数组值进行分组?
组将使以我提供的格式输出表变得容易
更新:如果不想对数组进行分组,只需按角色排序查询,并在角色id更改时创建新的角色字段

cgh8pdjw

cgh8pdjw2#

假设你的代码正常工作,
您的目标是每列只显示一条记录,其值与simplu可以使用的值相同:select distinct(you\u column\u name)
例如,如果您希望您的角色在同一域中只出现一次,则可以使用distinct(domain.domain\u id)
我希望这能解决你的问题。

相关问题