For some method, please kindly advise the error. This post was last edited by ymkacscc20 from 2012-12-1302: 53: 59 & lt ;? Phpfunction & nbsp; get_employees_by_hierarchy (& nbsp; $ _ employee_ I)
This post was last edited by ymkacscc20 at 02:53:59 on
Function get_employees_by_hierarchy ($ _ employee_id = 0, $ _ depth = 0, $ _ org_array = array ()){
If ($ this-> org_depth <$ _ depth ){
$ This-> org_depth =$ _ depth;
}
$ _ Depth ++;
$ _ Query = "SELECT * FROM employees WHERE ";
If (! $ _ Employee_id ){
$ _ Query. = "maid is null or maid = 0 ";
}
Else {
$ _ Query. = "maid =". $ this-> dbh-> quoteSmart ($ _ employee_id );
}
$ _ Result = $ this-> query ($ _ query );
While ($ _ row =$ _ result-> fetchRow ()){
$ _ Row ['dest'] = $ _ depth;
Array_push ($ _ org_array, $ _ row );
$ _ Org_array = $ this-> get_employees_by_hierarchy (
$ _ Row ['employee _ manager_id '],
$ _ Depth,
$ _ Org_array
);
}
Return $ _ org_array;
}
?>
How can I optimize this code? What are 3-5 actions important? 17. what is important? The younger brother just started learning php. please kindly advise me ~
------ Solution --------------------
The only thing that can be said about this code is $ this-> dbh-> quoteSmart ($ _ employee_id)
However, quoteSmart provides only an escape function. If it is just an escape, it seems redundant. Because numbers do not need to be escaped. If it is a string, it is not enclosed in quotation marks in the constructed query string.
If $ this-> dbh-> quoteSmart ($ _ employee_id) returns the original value of $ _ employee_id
Then "maid =". $ this-> dbh-> quoteSmart ($ _ employee_id) will return
All records of employee_manager_id = $ _ employee_id
In the following recursive call, $ _ employee_id = $ _ row ['employee _ manager_id ']
Because $ _ employee_id is not changed, it will be in an endless loop.
Therefore, $ this-> dbh-> quoteSmart ($ _ employee_id) should return a value different from $ _ employee_id.
So he is crucial.