Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...