递归中的PHP单选按钮

时间:2013-07-29 06:56:56

标签: php

如何在递归中添加单选按钮,以便我可以选择并删除它们?

数据库:

ID  |  NAME          |  PARENT
1   |  General       |    0  
2   | Enteprise Dept |    0
3   | Sales Dept     |    0
4   | Collection     |    3
5   | Company        |    2
6   | Collection     |    5

功能:

function list_project_category($name,$parent,$parent_name,$default,$rec=0)
{
$temp="";
if($rec==0)
{
    //just_testing_code
}
$sql = "select id,name from system_categories where parent=$parent";
$result = mysql_query($sql) or die("error:list_project_category");
while($row=mysql_fetch_array($result))
{
    $ids=$row['id'];
    $names=$row['name'];

    $sql3 = mysql_query("SELECT COUNT(category) as cat FROM system_tables WHERE     category='$ids'");
    while($row1=mysql_fetch_assoc($sql3))
    {
        $cat_count=$row1['cat'];

        $def=($default==$row[0])?"":"";
        $par=($parent_name=="")?"":"$parent_name >> ";
        $temp.="</br>"."$def $par".$row[1]." >> $cat_count";
        $temp.=list_project_category($name,$row[0],$par.$row[1],$default,$rec+1);
    }
}
if($rec==0)
{
     //just_testing_code
}
return ($temp);
}

致电:

echo list_project_category("category",0,"",$parent,0);

结果:(该数字实际上是部门下的项目总数)

General >> 0
Enterprise Dept >> 0
Enterprise Dept >> company >> 1
Enterprise Dept >> company >> collection >> 0
Sales Dept >> 0
Sales Dept >> collection >> 1 

我想为他们添加按钮。

1 个答案:

答案 0 :(得分:0)

假设1:

如果我理解正确,在删除父级时,您希望删除所有子级,以免它们成为孤儿。

假设2:

您想使用复选框来选择和删除多个项目。

解决方案:

  1. 定义提交到deletecategories.php脚本的表单。
  2. 在表单中打印您的类别标签以及一个ID为您的类别ID的复选框
  3. 在提交时,在你的deletecategories.php中,捕获表单提交的id并使用sql,递归删除id及其所有后代的类别。
  4. 注意:您可以直接在递归函数内部打印HTML,或者为了使事情更清晰,您的递归函数返回一个“label”=&gt;“id”对的平面数组,然后在您的html内部迭代模板。