如何在我的PHP源代码中编写递归功能

  • 本文关键字:递归 功能 源代码 PHP php
  • 更新时间 :
  • 英文 :


我想在层次树结构中显示员工。我尝试了一个循环。但是很难获得输出。我是PHP的新手,请帮助我,也就是说,如何在我的源代码中编写递归功能。

 <?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

减少代码的最简单起点是将通用代码删除到函数...

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;
}

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

<?php
$selectedPM=displayEmployees($selectAF);
?>

如果您只需要一个层次结构,则无需递归。但是,如果您想显示下属的下属,那么递归就是这样做的方法之一。

首先,我们需要更改模式以使其更可靠。您 can 与名称匹配,但只需要一个字母误以为破坏所有内容。使用ID与之匹配。我已将SnoControlof重命名为更典型的idpid(父ID(。pid只是员工经理的ID。如果员工没有任何人,请使用0null

      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:

SELECT * FROM Employees WHERE pid=?

,然后将员工ID中的已记录的占位符绑定到占位符。

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

如果由于某种原因您不知道员工ID,即使搜索员工的名字也可以到达那里:

SELECT a.* from Employees as a, Employees as b WHERE a.pid=b.id AND a.employeeName LIKE ?

递归更为复杂……最好在对象中完成,因此您不必担心可变范围。如果您需要更深入地进入层次结构,请告诉我,我会写另一个答案。它更复杂,值得一个单独的答案。

最新更新