2010-11-04 47 views
0

經理角色3和團隊領導者基於role2我怎麼能重複查詢

//get userid from login session 

    $memberid = $_SESSION['SESS_MEMBER_ID']; 

//get role from session 
$role = $_SESSION['SESS_ROLE_ID'] 

    while($res = mysql_fetch_array($role)){ 
if($role=='3'){ 
$teamleader = mysql_query("select * from dept_user where lead_id='$member_id'"); 

     while ($data = mysql_fetch_assoc($teamleader)) { 
     $user = mysql_query("select firstname, lastname, email from user where userid in(select userid from dept_user where lead_id=$data)"); 
      $result = mysql_query($user); 
      print $teamleader; 
print $result; 
       } 
    } 

我的問題是,我需要通過lead_id領域獲得hiarchy的所有用戶,我跑2查詢,但是仍然無法得到的結果

+1

什麼是同時($水庫= mysql_fetch_array($角色)){在這裏做什麼?你在會話參數上運行mysql函數? – 2010-11-04 08:24:23

回答

0

我不確定以下是否會有任何幫助,因爲我沒有太多的想法給你想要做的除之外「我需要讓所有的用戶都是喜歡的]由lead_id字段「

所以,如果你是tryi NG生成和顯示部門/員工的層次結構,這是一個可行的方法(被警告這是一個很大的消化!)

完整的腳本在這裏:http://pastie.org/1271804

希望它能幫助:)存儲

例過程調用

單非遞歸存儲過程調用,生成各部門工作人員的層次結構

call department_hier(<dept_id>,<staff_id>); 

call department_hier(2,1); 

示例PHP腳本

這個腳本生成的數據的XML DOM表示,但你可以處理它,只要你喜歡。

<?php 

header("Content-type: text/xml"); 

$conn = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306); 

// one non-recursive db call to get the tree 

$result = $conn->query(sprintf("call department_hier(%d,%d)", 2,1)); 

$xml = new DomDocument; 
$xpath = new DOMXpath($xml); 

$dept = $xml->createElement("department"); 
$xml->appendChild($dept); 

// loop and build the DOM 

while($row = $result->fetch_assoc()){ 

    $staff = $xml->createElement("staff"); 
    //foreach($row as $col => $val) $staff->setAttribute($col, $val); 

    $staff->setAttribute("staff_id", $row["staff_id"]); 
    $staff->setAttribute("name", $row["name"]); 
    $staff->setAttribute("parent_staff_id", $row["parent_staff_id"]); 

    if(is_null($row["parent_staff_id"])){ 
     $dept->setAttribute("dept_id", $row["dept_id"]); 
     $dept->setAttribute("department_name", $row["department_name"]); 
     $dept->appendChild($staff); 
    } 
    else{ 
     $qry = sprintf("//*[@staff_id = '%d']", $row["parent_staff_id"]); 
     $parent = $xpath->query($qry)->item(0); 
     if(!is_null($parent)) $parent->appendChild($staff); 
    } 
} 
$result->close(); 
$conn->close(); 

echo $xml->saveXML(); 
?> 

示例XML輸出

這是PHP腳本在我的例子產生。

<department dept_id="2" department_name="Mathematics"> 
    <staff staff_id="1" name="f00" parent_staff_id=""> 
     <staff staff_id="5" name="gamma" parent_staff_id="1"/> 
     <staff staff_id="6" name="delta" parent_staff_id="1"> 
      <staff staff_id="7" name="zeta" parent_staff_id="6"> 
       <staff staff_id="2" name="bar" parent_staff_id="7"/> 
       <staff staff_id="8" name="theta" parent_staff_id="7"/> 
      </staff> 
     </staff> 
    </staff> 
</department> 

MySQL的腳本

-- TABLES 

drop table if exists staff; 
create table staff 
(
staff_id smallint unsigned not null auto_increment primary key, 
name varchar(255) not null 
) 
engine = innodb; 

drop table if exists departments; 
create table departments 
(
dept_id tinyint unsigned not null auto_increment primary key, 
name varchar(255) unique not null 
) 
engine = innodb; 

drop table if exists department_staff; 
create table department_staff 
(
dept_id tinyint unsigned not null, 
staff_id smallint unsigned not null, 
parent_staff_id smallint unsigned null, 
primary key (dept_id, staff_id), 
key (staff_id), 
key (parent_staff_id) 
) 
engine = innodb; 

-- STORED PROCEDURES 

drop procedure if exists department_hier; 

delimiter # 

create procedure department_hier 
(
in p_dept_id tinyint unsigned, 
in p_staff_id smallint unsigned 
) 
begin 

declare v_done tinyint unsigned default 0; 
declare v_dpth smallint unsigned default 0; 

create temporary table hier(
dept_id tinyint unsigned, 
parent_staff_id smallint unsigned, 
staff_id smallint unsigned, 
depth smallint unsigned 
)engine = memory; 

insert into hier select dept_id, parent_staff_id, staff_id, v_dpth from department_staff 
    where dept_id = p_dept_id and staff_id = p_staff_id; 


/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */ 

create temporary table tmp engine=memory select * from hier; 

while not v_done do 

    if exists(select 1 from department_staff e 
      inner join hier on e.dept_id = hier.dept_id and e.parent_staff_id = hier.staff_id and hier.depth = v_dpth) then 

     insert into hier select e.dept_id, e.parent_staff_id, e.staff_id, v_dpth + 1 from department_staff e 
      inner join tmp on e.dept_id = tmp.dept_id and e.parent_staff_id = tmp.staff_id and tmp.depth = v_dpth; 

     set v_dpth = v_dpth + 1;    

     truncate table tmp; 
     insert into tmp select * from hier where depth = v_dpth; 

    else 
     set v_done = 1; 
    end if; 

end while; 

select 
hier.dept_id, 
d.name as department_name, 
s.staff_id, 
s.name, 
p.staff_id as parent_staff_id, 
p.name as parent_name, 
hier.depth 
from 
hier 
inner join departments d on hier.dept_id = d.dept_id 
inner join staff s on hier.staff_id = s.staff_id 
left outer join staff p on hier.parent_staff_id = p.staff_id; 

drop temporary table if exists hier; 
drop temporary table if exists tmp; 

end # 

delimiter ; 

-- TEST DATA 

insert into staff (name) values 
    ('f00'),('bar'),('alpha'),('beta'),('gamma'),('delta'),('zeta'),('theta'); 

insert into departments (name) values 
('Computing'),('Mathematics'),('English'),('Engineering'),('Law'),('Music'); 

insert into department_staff (dept_id, staff_id, parent_staff_id) values 
(1,1,null), 
    (1,2,1), 
    (1,3,1), 
     (1,4,3), 
      (1,7,4), 
(2,1,null), 
    (2,5,1), 
    (2,6,1), 
     (2,7,6), 
      (2,8,7), 
      (2,2,7); 

-- TESTING (call this sproc from your php) 

call department_hier(1,1); 

call department_hier(2,1); 
+0

thx dr ..我會嘗試喜歡它dr .. thx .. gr8 – vimal 2010-11-08 04:53:39