PHP, MySQL – Recursive function to get category for N-Level in tree format

PHP, MySQL – Recursive function to get category for N-Level in tree format
- Create table tbl_categories with catid, catname and catpid (default zero)
This is tested function. Contact me if any problem

NLevelDropdown('category', '', '', '', 0, 0);

function nLevel_dropdown($name, $sel_value, $extra='', $choose_one='', $parent_id=0, $level=0){
$level++;
$sql = "select catid, catname from tbl_categories where catpid = '$parent_id'";
$result = mysql_query($sql);
if(mysql_num_rows($result)>0){
if($level==1){
$str_drop .= '{put select tag}'; //<select name="'.$name.'" id="'.$name.'" '.$extra.'>
if($choose_one != ''){
$str_drop .= '<option value="">'.$choose_one.'</option>';
}
}
while($line = mysql_fetch_array($result)){
$str_drop .= '<option value="'.$line[0].'" ';
if($sel_value == $line[0]){
$str_drop .= 'selected';
}
$str_drop .= '>';
for($i=1;$i<$level;$i++){
if($i==$level){
$str_drop .= "&nbsp;&nbsp;&raquo;";
}else{
$str_drop .= "&raquo;";
}
}
$str_drop .= $line[1].'</option>';
$str_drop .= NLevelDropdown($name, $sel_value, $extra, $choose_one='', $line[0], $level);
}
$str_drop .= $level==1?'</select>':'';
}
return $str_drop;
}

2 thoughts on “PHP, MySQL – Recursive function to get category for N-Level in tree format

  1. Pingback: My Homepage

Leave a Reply

Your email address will not be published. Required fields are marked *


three + = 7

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>