2017-06-13 45 views
1

我正在開發一個應用程序 - 一種微型生物指標。在這個應用程序中,管理員將創建一個特定的用戶併爲他創建其他用戶。主要用戶創建的用戶將是爲人員創建註冊的用戶。我有兩個表,一個用於管理員用戶,另一個用於用戶註冊。我想要的是通過此子用戶已添加到users表中的用戶數來獲取管理員下的所有子用戶。如何從mysql數據庫中選擇主用戶及其子用戶

這裏的源代碼。它沒有給我我需要的東西。

<div class="content-loader"> 
    <?php 
    if($pri == 'user' && $utype == 'super_admin'){ 
    ?> 

    <table cellspacing="0" width="100%" id="example" class="table display table-striped table-hover table-responsive"> 
    <thead> 
    <tr> 
    <th>#ID</th> 
    <th>Name</th> 
    <th>username</th> 
    <th>no of people registered</th> 
    <th>Date Added</th>  
    <th></th> 

    </tr> 
    </thead> 
    <tbody> 
    <?php 


    $i =1; 
    $stmt = $db_con->prepare("SELECT * FROM users where adder in (select uid from admin_user where adder = '$id')"); 
    $stmt->execute(); 
    $row=$stmt->fetch(PDO::FETCH_ASSOC); 
    $ad = $row['adder']; 
    $stmt = $db_con->prepare("SELECT * FROM admin_user where uid = '$ad' "); 
    $stmt->execute(); 
    while($rows=$stmt->fetch(PDO::FETCH_ASSOC)) 
    { 

    ?> 
     <tr class="delete_user<?php echo $rows['uid']; ?>"> 
     <td><?php echo $i; $i++;?></td> 
     <td><?php echo $rows['name']; ?> </td> 
     <td><?php echo $rows['username'];?></td> 
     <td><?php //echo $rows['tot'];?></td> 
     <td><?php echo $rows['date_added'];?> (<?php echo timeAgo($rows['time_added']) ?>)</td> 
     <td class="td-actions text-right"> 
     <!--<a id="<?php echo $rows['uid']; ?>" class="edit-link btn btn-primary btn-simple btn-xs" rel="tooltip" href="#" title="Edit User"><i class="material-icons">edit</i></a>--> 
    <a id="<?php echo $rows['uid']; ?>" class="btn btn-danger btn-user"> 
     <span class="glyphicon glyphicon-trash"></span> 
     </a> 
    </td> 

     </tr> 
     <?php 

    } 
    ?> 
    </tbody> 

    </table> 
    <?php 
    }else{ 
    ?> 
     <div class="panel-warning" style="padding:7px; text-align:center; font-size:18px;">This page is for the administrator!</div> 
     <?php 
    } 
    ?> 

    </div> 


        </div> 

爲了清楚起見,這裏有兩個表: 管理員用戶表

enter image description here 管理員用戶表圖。 1

用戶由管理員都和他的子用戶從你的代碼 enter image description here

用戶表Img.2

+1

到目前爲止您嘗試過什麼? –

+0

@AhmedGinani,我發佈了源代碼。 –

+0

另外,在執行新的查詢之前,您應該「清理」stmt:oci_free_statement($ stmt); –

回答

0

隨着關節如果你想知道每個管理員已

SELECT ad.name as nom_admin, count(users.society_name) as nbSociety //users.IDsociety 
FROM users 
LEFT JOIN admin_user ad ON ad.Uid = name.adders 
GROUP BY cl.classe 

你的表聽起來奇怪的用戶數量是非常簡單

SELECT ad.name as nom_admin, users.society_name as mySociety // I guess society is the name of the user 
FROM admin_user ad 
LEFT JOIN users ON users.adder= ad.Uid 

。排列好田地

我的朋友,是我們做的計算機科學嗎? :不,我們做工藝品

1

什麼我的寶貴意見是執行只有一個查詢添加表。您所需的結果只能用於查詢。嘗試用這種

$stmt = $db_con->prepare("SELECT * FROM admin_user where uid = (SELECT adder FROM users where adder in (select uid from admin_user where adder = '$id'))"); 
$stmt->execute(); 
+0

我試過了,但沒有奏效。 –

相關問題