How to create unlimited sub category in drop down list
Would you please guide me on how to create unlimited category and sub
category in a drop down list using php and mysql?
I was the sub category to have --- in front of them. For example, the
first sub category --- and the second level ----- and so on: I want to
achieve some thing like this:-
<select name="category">
<option value="1">Root</option>
<option value="3">- Sub </option>
<option value="4">- - Sub</option>
<option value="2">Root</option>
<option value="3">- Sub </option>
<option value="4">- - Sub</option>
</select>
Here is the code I got so far
function get_category($parentID = 0){
global $mysqli;
$html = '';
// Prepare the query
$stmt = $mysqli->prepare("SELECT CategoryID, Title
FROM category
WHERE ParentID =?");
// Execute the query
$stmt->bind_param('i', $parentID);
$stmt->execute();
// Store the result
$stmt->store_result();
// Bind the result
$stmt->bind_result($categoryID, $title);
while($stmt->fetch()){
$html .= "<option
value=\"$categoryID\">$title</option>";
$child = $mysqli->prepare("SELECT CategoryID
FROM category
WHERE ParentID =?");
// Execute the query
$child->bind_param('i', $categoryID);
$child->execute();
// Store the result
$child->store_result();
// Bind the result
$has_child = NULL;
$has_child = $child->num_rows;
if($has_child){
$html .= get_category($categoryID);
}
}
return $html;
}
echo '<select name="category">';
print(get_category());
echo '</select>';
Any help is appreciated
No comments:
Post a Comment