如何在我的PHP源代码中编写递归函数

时间:2017-06-24 05:36:53

标签: php

我想在Hierarchical树结构中显示员工。我试过一个循环。但要获得输出是如此困难。我是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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

如果您只需要一个级别的层次结构,则不需要递归。但是如果你想展示下属的下属,那么递归是实现它的方法之一。

首先,我们需要对架构进行更改以使其更可靠。你可以匹配名称,但所需要的只是一个字母错误,以打破一切。使用id匹配。我已将SnoControlof重命名为更典型的idpid(父ID)。 pid只是员工经理的身份。如果员工没有人,请使用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 ?

递归有点复杂......最好在一个对象中完成,这样你就不必担心变量范围了。如果您需要深入了解层次结构,请告诉我,我会写另一个答案。它更复杂,值得一个单独的答案。