Sp0T
Sp0T

Reputation: 284

tree structure in php

I'm trying to get the below structure

  1. Books

    a.Harry Potter

  2. Clothes

    a. Shirts

like this while fetching all data from database. I did this using php and iquery but instead I'm getting the following structure-

  1. Books

    a. Harry Potter

    b. Shirts

  2. Clothes

    a. Harry Potter

    b. Shirts

So can anyone tell me where i'm going wrong.My code is

<?php include 'dbconnect.php' ?>
<?php include 'header.php' ?>
<div id="wrapper">
    <div id="content">
        <div id="rightnow">
            <h3 class="reallynow"></h3>
            <br/>
            <script src="jquery.js" type="text/javascript"></script>
            <script src="jquery-ui.custom.js" type="text/javascript"></script>
            <script src="jquery.cookie.js" type="text/javascript"></script>
            <!-- Include the basic stylesheet: -->
            <script src="jquery.dynatree.js" type="text/javascript"></script>
            <script type="text/javascript">
                $(function () {
                    $("#tree").dynatree({
                        title: "Sample Theming",
                        // Image folder used for data.icon attribute.
                        imagePath: "skin-custom/",
                        onActivate: function (node) {
                            return true;
                        }
                    });
                });
            </script>
            <?php
            $x1 = array();
            $x2 = array();
            $sql_1 = mysqli_query($con, "SELECT * FROM jsrao_db4");
            while ($row_1 = mysqli_fetch_array($sql_1)) {
                array_push($x1, $row_1["cat_name"]);
            }
            $sql_2 = mysqli_query($con, "
                SELECT * FROM jsrao_db5 WHERE pro_id = $i
            ");
            while ($row_2 = mysqli_fetch_array($sql_2)) {
                array_push($x2, $row_2["pro_name"]);
            }
            ?>
            <div id="tree">
                <ul>
                    <?php foreach ($x1 as $p) {
                        echo "<li class='folder'>$p"; ?>
                        <ul>
                        <?php foreach ($x2 as $q) {
                            echo "<li>$q";
                        } ?>
                        </ul><?php } ?>
                </ul>
            </div>
        </div>
    </div>
</div> 

Upvotes: -2

Views: 951

Answers (4)

Chetan hada
Chetan hada

Reputation: 1

instead of using li you can try like this thank you

<div id="tree">
    <?php foreach ($x1 as $p) {
        echo "<li class='folder'>$p</li>"; ?>
        <ul>
            <li>
            <?php foreach ($x2 as $q) {
                echo "<li>$q</li>";
            } ?>
            </li>
        </ul>
    <?php } ?>
</div>

Upvotes: 0

Chetan hada
Chetan hada

Reputation: 1

Use structure like this thank you

<?php
$select_maincat = "SELECT * FROM wp_categories";
$qry_maincat = mysql_query($select_maincat);
while ($data_maincat = mysql_fetch_assoc($qry_maincat)) { ?>
    <ul>
        <li> <?php echo $data_maincat['cat_name']; ?>
            <ul>
            <?php
            $select_subcat = "select * from wp_subcategories where cat_id=" . $data_maincat['id'];
            $qry_subcat = mysql_query($select_subcat);
            while ($data_subcat = mysql_fetch_assoc($qry_subcat)) { ?>
                <li><?php echo $data_subcat['subcat_name']; ?></li>
            <?php } ?>
            </ul>
    </ul>
<?php } ?> 

Upvotes: 0

Muthamizhchelvan. V
Muthamizhchelvan. V

Reputation: 1261

instead of using array push you can try like this

$x1    = array();
$query = $this->db->query("SELECT * FROM  jsrao_db4 ");
$rows  = $query->result();

foreach ($rows as $row) {
    array_push($x1, $row->cat_name);
    $query = $this->db->query("SELECT * FROM  jsrao_db5 WHERE id=$row->id");
    $rs = $query->result();
    foreach ($rs as $r) {
        $x1[$row->cat_name] = array();
        array_push($x1[$row->cat_name], $r->pro_name);
    }
}
?>
<div id="tree">
    <ul>
        <?php foreach ($x1 as $p) {
            echo "<li class='folder'>$p"; ?>
            <ul>
            <?php foreach ($x1[$p] as $q) {
                echo "<li>$q";
            } ?>
            </ul><?php } ?>
    </ul>
</div>
</div>
</div>

Upvotes: 0

twain
twain

Reputation: 1325

Have you tried to close the "li" element?

 <div id="tree">
        <ul>
            <?php foreach($x1 as $p){  echo "<li class='folder'>$p</li>" ;?>
        <ul>
                <?php foreach($x2 as $q) { echo"<li>$q</li>"; } ?>
        </ul><?php } ?>

        </ul>
        </div>
</div>

Upvotes: 0

Related Questions