tree structure plugin

Preview

  • 1
    • 1.1
      • 1.1.1
      • 1.1.2
      • 1.1.3
        • 1.1.3.1
          • 1.1.3.1.1
          • 1.1.3.1.2
        • 1.1.3.1
    • 1.2
      • 1.2.1
      • 1.2.2
    • 1.3
      • 1.3.1
      • 1.3.2
      • 1.3.3

PHP and Ajax

ajax.php




include_once "db.php";
//$data = json_decode(stripslashes($_REQUEST['data']));
if ($_REQUEST['action'] == 'edit') {
    $showhideval = isset($_REQUEST['showhideval']) ? 1 : 0;
    mysqli_query($con, "UPDATE tree SET first_name='" . $_REQUEST['first_name'] . "', hide='" . $showhideval . "' WHERE id='" . $_REQUEST['id'] . "'");
} elseif ($_REQUEST['action'] == 'delete') {
    if($_REQUEST['id'] != 1) {
        mysqli_query($con, "DELETE FROM tree WHERE id in(" . $_REQUEST['id'] . ")");
    }
} elseif ($_REQUEST['action'] == 'add') {
    $showhideval = isset($_REQUEST['showhideval']) ? 1 : 0;
    mysqli_query($con, "INSERT INTO tree (first_name, parent_id, hide) VALUES ('" . $_REQUEST['first_name'] . "', " . $_REQUEST['parentid'] . ", " . $showhideval . ")");
    echo mysqli_insert_id($con);
} elseif ($_REQUEST['action'] == 'drag') {;
    mysqli_query($con, "UPDATE tree SET parent_id='" . $_REQUEST['parentid'] . "' WHERE id='" . $_REQUEST['id'] . "'");
} elseif ($_REQUEST['action'] == 'addform') {
    echo <<<EOL
    <form class="add_data" method="post" action="">
        <img class="close" loading="lazy" src="images/close.png" />
        <input type="text" class="first_name" name="first_name" placeholder="first name">
        <input type="checkbox" name="showhideval" id="hide" />
        <label for="hide">Hide Child Nodes</label>
        <input type="submit" class="submit" name="submit" value="Submit">
    </form>
EOL;
} elseif ($_REQUEST['action'] == 'editform') {
    $edit_ele_id = $_REQUEST['edit_ele_id'];
    $geteledetail = mysqli_query($con, "SELECT * FROM tree WHERE id = $edit_ele_id");
    while ($row = mysqli_fetch_array($geteledetail)) {
        $id = $row['id'];
        $first_name = $row['first_name'];
        $parent_id = $row['parent_id'];
        $hide = $row['hide'];
        $checked = $hide == 1 ? "checked" : ""; 
    }
    echo <<<EOL
    <form class="edit_data" method="post" action="">
        <img class="close" loading="lazy" src="images/close.png" />
        <input type="text" class="first_name" name="first_name" value="{$first_name}" placeholder="first name">
        <input type="checkbox" {$checked} value="{$hide}" name="showhideval" id="hide" />
        <label for="hide">Hide Child Nodes</label>
        <input type="submit" class="edit" name="submit" value="submit">    
    </form>
EOL;
}
mysqli_close($con);

index.php



        include_once 'db.php';
        $store_all_id = array();
        $id_result = mysqli_query($con, "SELECT * FROM tree");
        while ($all_id = mysqli_fetch_array($id_result)) {
            array_push($store_all_id, $all_id['parent_id']);
        }
        echo "<div class='overflow'><div>";
        in_parent(0, $store_all_id, $con);
        echo "</div></div>";

        function in_parent($in_parent, $store_all_id, $con) {
            if (in_array($in_parent, $store_all_id)) {
                $result = mysqli_query($con, "SELECT * FROM tree where parent_id = $in_parent");
                echo $in_parent == 0 ? "<ul class='tree'>" : "<ul>";
                while ($row = mysqli_fetch_array($result)) {
                    echo "<li";
                    if ($row['hide'])
                        echo " class='thide'";
                    echo "><div id=" . $row['id'] . "><span class='first_name'>" . $row['first_name'] . "</span></div>";
                    in_parent($row['id'], $store_all_id, $con);
                    echo "</li>";
                }
                echo "</ul>";
            }
        }

        mysqli_close($con);
       

DB QUERY



CREATE TABLE IF NOT EXISTS `tree` (
`id` int(10) NOT NULL,
  `first_name` varchar(30) NOT NULL,
  `parent_id` int(10) NOT NULL,
  `hide` tinyint(1) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=143294 ;

--
-- Dumping data for table `tree`
--

INSERT INTO `tree` (`id`, `first_name`, `parent_id`, `hide`) VALUES
(1, '1', 0, 0),
(143284, '1.1', 1, 0),
(143285, '1.2', 1, 0),
(143286, '1.3', 1, 0),
(143287, '1.2.1', 143285, 0),
(143288, '1.3.1', 143286, 0),
(143289, '1.1.1', 143284, 0),
(143290, '1.1.2', 143284, 0),
(143291, '1.2.1.1', 143287, 0),
(143292, '1.2.2', 143285, 0),
(143293, '1.3.2', 143286, 0);


ALTER TABLE `tree`
 ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `tree`
--
ALTER TABLE `tree`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=143294;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

No Comment
Add Comment
comment url