Thursday, 5 September 2013

N-Level Category Listing

N-Level Category Listing

I'm Creating a N level Category Listing using select tag in php
A
--A1
-->>A1-1
B
--B1
My Database table is
id category pid
1 A 0
2 A1 1
3 B 0
4 A1-1 2
5 B1 3
<?php
include('get_list.php');
echo '<select name="catid" id="catid">';
echo '<option value="0">-- Select -- </option>';
$get_options = get_cat_selectlist(0, 0);
echo count($get_options);
$options= "";
if (count($get_options) > 0)
{
foreach ($get_options as $key => $value)
{
$options .="<option value=\"$key\"";
$options .=">$value</option>\n";
}
}
echo $options;
echo '</select>';
?>
</code></pre>
This Is My Recursive Function
$sql = "SELECT id, category from cat where pid ='.$current_cat_id .'";
$get_options = mysqli_query($con,$sql)or die(mysql_error());
$num_options = mysqli_num_rows($get_options);
$indent_flag='';
if ($num_options > 0)
{
while (list($id, $category) = mysqli_fetch_row($get_options))
{
if ($current_cat_id!=0)
{
$indent_flag .= '--';
for($x=2; $x<=$count; $x++)
{
$indent_flag .= ' >';
}
}
$category = $indent_flag.$category;
$option_results[$id] = $category;
get_cat_selectlist($id, $count);
}
}
return $this->$option_results;
}
?>
A
B
Output is not in a way as I wanted

No comments:

Post a Comment