首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP源代码中编写递归函数

问如何在PHP源代码中编写递归函数
EN

Stack Overflow用户
提问于 2017-06-24 13:36:53
回答 2查看 196关注 0票数 1

我想以分层树结构显示员工。我已经尝试了一个循环。但是要得到输出是非常困难的。我是PHP新手,可以帮我解决这个问题,即如何在源代码中编写递归函数。

代码语言:javascript
复制
 <?php
 include_once('config.php');
 $Empname="Prakash";
 $Designation="HR";
 ?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" language="javascript"></script>
</head>
<body>
<div align="right">
<a href="logout.php?Empid=<?php echo $Empid;?>">Logout</a>
</div>
<div id="div">
<h4>EmpName:<?php echo $Empname ;?></h4>
<h4>Designation:<?php echo $Designation ;?></h4>
</div>
<form>
<?php
$selectAF=array();
$i=0;
while($getbyHR=mysql_fetch_array($selectbyHR))
{
$selectAF[$i]=$getbyHR['Empname'];
?>
<table>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<tbody>
<tr>
<td><?php echo $getbyHR['Empname']?></td>
<td><?php echo $getbyHR['Designation']?></td>
<td><?php echo $getbyHR['Controlof'];?></td>
<td><?php echo $getbyHR['Projectstatus']?></td>
</tr>
</tbody>
</table>
<table>
<h2>Project managers</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedPM=array();
$x=0;
for($p=0;$p<count($selectAF);$p++)
{
$PMname=$selectAF[$p];
$selectpm=mysql_query("select * from reports where Controlof='$PMname'");
while($getPM=mysql_fetch_array($selectpm))
{
$selectedPM[$x] =$getPM['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getPM['Empname']; ?></td>
<td><?php echo $getPM['Designation'];?></td>
<td><?php echo $getPM['Controlof'];?></td>
<td><?php echo $getPM['Projectstatus'];?></td>
</tr>
<?php
$x++;
}
}
?>
</tbody>
</table>
<table>
<h2>TeamLeaders</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php

$selectedTL=array();
$q=0;
for($t=0;$t<count($selectedPM);$t++)
{
$TLname=$selectedPM[$t];
$selectedteamleader=mysql_query("select * from reports where Controlof='$TLname'");
while($getTL=mysql_fetch_array($selectedteamleader))
{
$selectedTL[$q] =$getTL['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getTL['Empname']; ?></td>
<td><?php echo $getTL['Designation'];?></td>
<td><?php echo $getTL['Controlof'];?></td>
<td><?php echo $getTL['Projectstatus'];?></td>
</tr>
<?php
$q++;
}
}
?>
</tbody>
</table>
<table>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<h2>SeniorEngineer</h2>
<?php
$selectedSE=array();
$w=0;
for($s=0;$s<count($selectedTL);$s++)
{
$SEname=$selectedTL[$s];
$selectedseniorengineer=mysql_query("select * from reports where Controlof='$SEname'");
while($getSE=mysql_fetch_array($selectedseniorengineer))
{
$selectedSE[$w] =$getSE['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getSE['Empname']; ?></td>
<td><?php echo $getSE['Designation'];?></td>
<td><?php echo $getSE['Controlof'];?></td>
<td><?php echo $getSE['Projectstatus'];?></td>
</tr>
<?php
$w++;
}
}
?>
</tbody>
</table>
<table>
<h2>AssistantEngineer</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedAE=array();
$b=0;
for($a=0;$a<count($selectedSE);$a++)
{
$AEname=$selectedSE[$a];
$selectedassistantengineer=mysql_query("select * from reports where Controlof='$AEname'");
while($getAE=mysql_fetch_array($selectedassistantengineer))
{
$selectedAE[$b] =$getAE['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getAE['Empname']; ?></td>
<td><?php echo $getAE['Designation'];?></td>
<td><?php echo $getAE['Controlof'];?></td>
<td><?php echo $getAE['Projectstatus'];?></td>
</tr>
<?php
$b++;
}
}
?>
</tbody>
</table>
<table>
<h2>Execuetive Engineer</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedEE=array();
$y=0;
for($e=0;$e<count($selectedAE);$e++)
{
$Engineername=$selectedAE[$e];
$selectedengineer=mysql_query("select * from reports where Controlof='$Engineername'");
while($getEE=mysql_fetch_array($selectedengineer))
{
$selectedEE[$y] =$getEE['Empname'];
?>
<tbody>
<tr>
<td><?php echo $getEE['Empname']; ?></td>
<td><?php echo $getEE['Designation'];?></td>
<td><?php echo $getEE['Controlof'];?></td>
<td><?php echo $getEE['Projectstatus'];?></td>
</tr>
<?php
$y++;
}
}
?>
</tbody>
</table>
<table>
<h2>Trainee</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<?php
$selectedtrainee=array();
$g=0;
for($r=0;$r<count($selectedEE);$r++)
{
$EEname=$selectedEE[$r];
$selectedTrainee=mysql_query("select * from reports where Controlof='$EEname'");
while($gettrainee=mysql_fetch_array($selectedTrainee))
{
$selectedtrainee[$g] =$gettrainee['Empname'];
?>
<tbody>
<tr>
<td><?php echo $gettrainee['Empname']; ?></td>
<td><?php echo $gettrainee['Designation'];?></td>
<td><?php echo $gettrainee['Controlof'];?></td>
<td><?php echo $gettrainee['Projectstatus'];?></td>
</tr>
<?php
$g++;
}
}
?>
</tbody>
</table>
<table>
<h2>Ap</h2>
<thead>
<th>Empname</th>
<th>Designation</th>
<th>Controlof</th>
<th>Projectstatus</th>
</thead>
<tbody>
<?php
$selectedAp=array();

for($v=0;$v<count($selectedtrainee);$v++)
{
$Trainee=$selectedtrainee[$v];
$selectedAp=mysql_query("select * from reports where Controlof='$Trainee'");
while($getAp=mysql_fetch_array($selectedAp))
{
?>
<tr>
<td><?php echo $getAp['Empname']; ?></td>
<td><?php echo $getAp['Designation'];?></td>
<td><?php echo $getAp['Controlof'];?></td>
<td><?php echo $getAp['Projectstatus'];?></td>
</tr>
<?php
}
}
?>
<?php
// while loop of AF
$i++;
}
?>
</tbody>
</table>
</form>
</body>
</html>


        Table
  Sno|Empname|Designation|Controlof|
   1 |Prakash|HR         |Ram      |
   2 |Sasi   |AF         |prakash  |
   3 |venki  |AF         |prakash  |
   4 |Tom    |PM         |Sasi     |
   5 |orton  |PM         |venki    |
   6 |Ram    |TL         |Tom      |
   7 |David  |SE         |orton    |
If prakash logged into his profile the dashboard has to look like this
    sno |Empname|Designation|controlof
     1   Sasi      AF        prakash
     2   venki     AF        Prakash

Project Managers
   sno |Empname|Designation|Control of    sno |Empname|Designation|Controlof
    1    Tom      PM          Sasi         1    orton   PM           Venki
EN

回答 2

Stack Overflow用户

发布于 2017-06-24 14:53:21

减少代码的最简单起始点是删除函数中的公共代码……

代码语言:javascript
复制
function displayEmployees ( $employee ) {
    echo '<tbody>';
    $selected=array();
    $select=mysql_query("select * from reports where Controlof='$employee'");
    while($get=mysql_fetch_array($select))
    {
        $selected[] =$get['Empname'];
        echo '<tr>';
        echo '<td>'.$get['Empname'].'</td>';
        echo '<td>'.$get['Designation'].'</td>';
        echo '<td>'.$get['Controlof']'.</td>';
        echo '<td>'.$get['Projectstatus'].'</td>';
        echo '</tr>';
    }
    echo '</tbody>';
    return $selected;
}

然后,您可以在需要显示数据的每个点调用它,例如...

代码语言:javascript
复制
<?php
$selectedPM=displayEmployees($selectAF);
?>
票数 0
EN

Stack Overflow用户

发布于 2017-06-24 22:54:19

如果您只需要一个层次结构级别,则不需要递归。但是如果你想显示下级的下级,那么递归就是其中一种方式。

首先,我们需要对模式进行更改,使其更加可靠。您可以匹配名称,但只需输入一个错误的字母即可破坏所有内容。使用要匹配的id。我已经将Sno和Controlof重命名为更典型的id和pid (父id)。pid只是员工经理的id。如果员工没有人管理他们,请使用0或null。

代码语言:javascript
复制
      table: Employees
id |pid | employeeName | designation
 1 | 6  | Prakash      | HR
 2 | 1  | Sasi         | AF
 3 | 1  | Venki        | AF
 4 | 2  | Tom          | PM
 5 | 3  | Orton        | PM
 6 | 4  | Ram          | TL
 7 | 5  | David        | SE

如果您只需要深入一层,那么就不需要递归;您只需要一个好的sql查询,假设您知道登录的员工的id:

代码语言:javascript
复制
SELECT * FROM Employees WHERE pid=?

然后将登录员工的id绑定到占位符。

注意:了解如何使用准备好的语句进行查询。mysql_query("select * from reports where Controlof='$PMname'");容易受到SQL注入的攻击。

如果由于某些原因你不知道员工的id,你甚至可以通过搜索员工的名字来找到:

代码语言:javascript
复制
SELECT a.* from Employees as a, Employees as b WHERE a.pid=b.id AND a.employeeName LIKE ?

递归有点复杂...最好在对象中完成,这样您就不必担心变量作用域。如果你需要更深入的层次结构,让我知道,我会写另一个答案。它更复杂,应该有一个单独的答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44733156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档