更加有效的层次结构系统

更加有效的层次结构系统

本文介绍了更加有效的层次结构系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写层次结构检测脚本,我已经编写了该脚本,但只能分为4级.有没有一种方法可以将其压缩为几行,这些行可以在无数个关卡中起作用?

I am trying to code a hierarchy detection script, I've already written the script, but can only go down 4 levels. Is there a way to condense this to a few lines that will work with an infinite amount of levels?

此脚本基本上是相同的代码副本,并粘贴了4次

This script is basically the same code copy and pasted 4 times

<?php
function listCategories($name, $disable_status = 0, $show_nums = 0) {
    echo "<select name='".$name."'>";
    $result = mysql_query("SELECT * FROM categories") or die(mysql_error());
    while($row = mysql_fetch_array($result)) {
        if($row['parent_id']==0) {
            $result2 = mysql_query("SELECT * FROM categories WHERE parent_id=".$row['id']) or die(mysql_error());
            echo "<option value='".$row['id']."'";

            if($disable_status==1&&isParent($row['id'])){
                echo " disabled='disabled'";
            }
            echo ">".$row['name']."</option>";

            while($row2 = mysql_fetch_array($result2)) {
                $result3 = mysql_query("SELECT * FROM categories WHERE parent_id=".$row2['id']) or die(mysql_error());
                echo "<option value='".$row2['id']."'";
                if($disable_status==1&&isParent($row2['id'])){
                    echo " disabled='disabled'";
                }
                echo ">- ".$row2['name']."</option>";

                while($row3 = mysql_fetch_array($result3)) {
                    $result4 = mysql_query("SELECT * FROM categories WHERE parent_id=".$row3['id']) or die(mysql_error());
                    echo "<option value='".$row3['id']."'";
                    if($disable_status==1&&isParent($row3['id'])){
                        echo " disabled='disabled'";
                    }
                    echo ">-- ".$row3['name']."</option>";

                    while($row4 = mysql_fetch_array($result4)) {
                        echo "<option value='".$row4['id']."'>[".$row4['id']."] --- ".$row4['name']."</option>";
                    }
                }
            }
        }
    }
    echo "</select>";
}

function isParent($cat_ID) {
    $result = mysql_query("SELECT * FROM categories WHERE parent_id=".$cat_ID) or die(mysql_error());
    if(mysql_num_rows($result)==0) {
        return FALSE;
    } else {
        return TRUE;
    }
}

我的categories的表结构是

id, name, parent_id

如果类别没有父类别,则parent_id将是0,否则将是其父类别的id.

If the category has no parent, the parent_id will be 0, or else, it would be the id of the category of it's parent.

感谢所有帮助.

推荐答案

我想应该遵循以下原则(未经测试,必须适应您的需求):

I guess something along these lines should do (untested, must be adapted to your needs):

$q = mysql_query("SELECT id, parent_id, name FROM categories");
while ($r = mysql_fetch_row($q)) {
  $names[$r[0]] = $r[2];
  $children[$r[0]][] = $r[1];
}

function render_select($root=0, $level=-1) {
  global $names, $children;
  if ($root != 0)
    echo '<option>' . strrep(' ', $level) . $names[$root] . '</option>';
  foreach ($children[$root] as $child)
    render_select($child, $level+1);
}

echo '<select>';
render_select();
echo '</select>';

执行此操作的一种更时髦的方法是使用SQL存储过程,但是在这种情况下,这可能会显得矫kill过正...

an even funkier way of doing this is by using SQL stored procedures, but it may be way overkill in this case...

这篇关于更加有效的层次结构系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:04