Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

php - More efficient hierarchy system

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?

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

My table structure for categories is

id, name, parent_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.

All help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...