من از این استفاده میکنم..البته این کد رو توی نت پیدا کردم و با کلی دردسر تونستم مشکلاتش رو برطرف کنم اما الان راحت میتونید ازش استفاده کنید..
<?php
mysql_connect("localhost",'root','usbw');
mysql_select_db('test');
$refs = array();
$list = array();
$query = mysql_query("SELECT * FROM table");
while($data = mysql_fetch_array($query)) {
$thisref = &$refs[ $data['id'] ];
$thisref['parent_id'] = $data['parent_id'];
$thisref['id'] = $data['id'];
$thisref['title'] = $data['title'];
if ($data['parent_id'] == 0) {
$list[ $data['id'] ] = &$thisref;
} else {
$refs[ $data['parent_id'] ]['children'][ $data['id'] ] = &$thisref;
}
}
function space($val){
return str_repeat('┘---> ',$val);
}
function cat($arr,$sp=0){
$html = '';
foreach ($arr as $v){
if($v['parent_id']==0){
$html .= '<option style="font-weight:bold;" value="'.$v['id'].'">'.$v['title'].'</option>'."\r\n";
$sp=0;
}else{
$html .= '<option value="'.$v['id'].'">'.space($sp).$v['title'].'</option>'."\r\n";
}
if (array_key_exists('children', $v)){
$html .= cat($v['children'], $sp+1);
}
}
return $html;
}
// build the list and output it
echo "<select style='width: 400px;' name=\"category\">";
echo cat($list);
echo '</select>';
?>