PHP read treeview database

PHOTO EMBED

Mon Nov 08 2021 23:50:49 GMT+0000 (Coordinated Universal Time)

Saved by @nilreis #php

            /* Database connection start */
            $servername = "localhost";
            $username = "root";
            $password = "";
            $dbname = "test";
            $conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

            function get_categories($parent = 0, $conn)
            {
                $html = '<ul>';
                //$str1 = "SELECT * FROM test.tree_struct WHERE pid=" . $parent;

                $str1 = "SELECT test.tree_data.id, nm AS 'text', pid  FROM test.tree_data
                        INNER JOIN test.tree_struct
                            ON test.tree_data.id = test.tree_struct.id WHERE pid=" . $parent;


                $query = mysqli_query($conn, $str1);

                while ($row = mysqli_fetch_assoc($query)) {
                    $current_id = $row['id'];
                    $html .= '<li>' . $row['text'];
                    $has_sub = NULL;
                    $str = "SELECT COUNT('pid') FROM test.tree_struct WHERE pid=" . $parent;

                    $has_sub = mysqli_num_rows(mysqli_query($conn, $str));
                    if ($has_sub) {
                        $html .= get_categories($current_id, $conn);
                    }
                    $html .= '</li>';
                }
                $html .= '</ul>';

                return $html;
            }

            print get_categories(0, $conn);
content_copyCOPY