-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
61 lines (57 loc) · 1.56 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
ini_set('display_errors', '1');
// database connection
$host = "localhost";
$db_user_name = ""; //enter your database user name
$db_user_password = ""; //enter your database user password
$db_name = "n_level_dropdown";
$conn = mysqli_connect($host,$db_user_name,$db_user_password,$db_name);
if(!$conn){
echo "database connection error";
die;
}
// insert category data in database
if(isset($_POST['submit'])){
$category = $_POST['category'];
$name = $_POST['name'];
$qry = "insert into categories (parent_id,name) values ('".$category."','".$name."')";
$res = mysqli_query($conn,$qry);
if($res){
echo "Category added!";
}
}
// creating recursion function to create a tree of category
function categoryTree($parent_id=0,$prefix=""){
global $conn;
$qry2 = "select * from categories where parent_id='".$parent_id."' order by name";
$res2 = mysqli_query($conn,$qry2);
if(mysqli_num_rows($res2) > 0){
while($row = mysqli_fetch_array($res2)){
echo $options = "<option value='".$row['id']."'>".$prefix.$row['name']."</option>";
categoryTree($row['id'],$prefix.' ');
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>N Level Dynamic Dropdown</title>
</head>
<body>
<form method="post">
Category :
<select name="category">
<option value="0">-- Select --</option>
<?php categoryTree() ?>
</select>
<br>
name :
<input type="text" name="name">
<br>
<input type="submit" name="submit">
</form>
</body>
</html>