Skip to main content
deleted 8 characters in body
Source Link
Alex L
  • 5.7k
  • 2
  • 25
  • 68

dynamically Dynamically load data tointo bootstrap accordion nav from databasenavigation bar

iI have made a bootstrap accordion navnavigation bar that loads data dynamically from a database with phpPHP.

here isHere are the 2two tables that iI use:

so basicallyBasically the table top_tier istop_tier has the main items of the accordion navnavigation bar. anAn entry in the top_tiertop_tier table can have multiple children from the table sub_tiersub_tier and that said entry from the sub_tiersub_tier table can have children too throughfrom the column parent_id, its kinda of a hierarchyparent_id. so when iWhen I click an item from the accordion that's from the top_tiertop_tier table, it'll showit shows its children then. Then if iI click an item from that said top_tierthe top_tier item, it'll showit shows its children items if it has any and so on...

hereHere is the code for the accordion:

hereHere is the phpPHP that fetchfetches the data from the top_tier table top_tier:

hereHere is the phpPHP that fetchfetches the data from the sub_tier table sub_tier:

hereHere is the function that controls the data from the database.:

dynamically load data to bootstrap accordion nav from database

i have made a bootstrap accordion nav that loads data dynamically from database with php

here is the 2 tables that i use:

so basically the table top_tier is the main items of the accordion nav bar. an entry in the top_tier table can have multiple children from the table sub_tier and that said entry from the sub_tier table can have children too through the column parent_id, its kinda of a hierarchy. so when i click an item from the accordion that's from the top_tier table, it'll show its children then if i click an item from that said top_tier item, it'll show its children items if it has any and so on...

here is the code for the accordion

here is the php that fetch the data from the table top_tier

here is the php that fetch the data from the table sub_tier

here is the function that controls the data from the database.

Dynamically load data into bootstrap accordion navigation bar

I have made a bootstrap accordion navigation bar that loads data dynamically from a database with PHP.

Here are the two tables that I use:

Basically the table top_tier has the main items of the accordion navigation bar. An entry in the top_tier table can have multiple children from the table sub_tier and that said entry from the sub_tier table can have children too from the column parent_id. When I click an item from the accordion that's from the top_tier table, it shows its children. Then if I click an item from the top_tier item, it shows its children items if it has any.

Here is the code for the accordion:

Here is the PHP that fetches the data from the top_tier table:

Here is the PHP that fetches the data from the sub_tier table:

Here is the function that controls the data from the database:

Source Link

dynamically load data to bootstrap accordion nav from database

i have made a bootstrap accordion nav that loads data dynamically from database with php

here is the 2 tables that i use:

CREATE TABLE IF NOT EXISTS `top_tier` (
  `id` varchar(50) NOT NULL,
  `name` varchar(255) NOT NULL,
  `date_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_by` varchar(50) NOT NULL,
  `date_edited` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `status` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `sub_tier` (
  `id` varchar(50) NOT NULL,
  `name` varchar(255) NOT NULL,
  `type` varchar(50) NOT NULL,
  `date_created` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `created_by` varchar(50) NOT NULL,
  `date_edited` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `edited_by` varchar(50) NOT NULL,
  `parent_id` varchar(50) NOT NULL,
  `status` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
)

so basically the table top_tier is the main items of the accordion nav bar. an entry in the top_tier table can have multiple children from the table sub_tier and that said entry from the sub_tier table can have children too through the column parent_id, its kinda of a hierarchy. so when i click an item from the accordion that's from the top_tier table, it'll show its children then if i click an item from that said top_tier item, it'll show its children items if it has any and so on...

here is the code for the accordion

    <div id="MainMenu">
  <div class="list-group panel">
    <?php
    $top_items =  $obj->getTopTierItems();
    foreach($top_items as $top_item) {
    extract($top_item);
    ?>

    <a href="#<?php echo $id;?>" class="strong list-group-item" data-toggle="collapse" data-parent="#MainMenu">
      <?php echo $name;?> 
      <i class="fa fa-caret-down"></i>
    </a>

    <div class="output collapse" id="<?php echo $id; ?>">
      <?php 
      $sub_items = $obj->getSubTierItems($id); 
      foreach($sub_items as $sub_item) {
        $id2 = $sub_item['id'];
        $name2 = $sub_item['name'];
        $parent_id = $sub_item['parent_id'];
      ?>
        <a href="#<?php echo $id2?>" class="strong list-group-item" data-parent="#<?php echo $parent_id; ?>">
          <?php echo $name2;?>
        </a>

        <div class="output collapse" id="<?php echo $id2; ?>">

        </div>
        <?php
        }
        ?>
    </div>

    <?php 
    }
    ?>
  </div>
</div>

here is the php that fetch the data from the table top_tier

public function getTopTierItems(){
        
        $SQL = "
            SELECT
             
            id,
            name

            FROM 
                top_tier
            WHERE
                status = 1  
        ";              
        $results = $this->db_query_listx($SQL);
        return $results;
    }

here is the php that fetch the data from the table sub_tier

public function getSubTierItems($id){
        
        $SQL = "
            SELECT
             
            id,
            name,
            parent_id

            FROM
                sub_tier
            WHERE
                status = 1
            AND
                parent_id='$id'
                        
        ";              
        $results = $this->db_query_listx($SQL);
        return $results;
    }

here is the function that controls the data from the database.

public function db_query_listx($SQL){
        global $connect;
        if ($resultset = $connect->query($SQL)) {
            if ($resultset->num_rows > 0) {
            
                $data = array();
                    while( $row = $resultset->fetch_assoc() ) {
                        $data[] = array_change_key_case($row);
                    }
                }else {
                $data = NULL;
            }
        } else {
            $data = die("<CENTER><DIV class='error'>ERROR: " . $connect->error . "<BR> ( $SQL ) <HR>
                             Please report this to <a href='#'>webmaster</a>.</DIV></CENTER>");
        }
        $resultset->close();
        return $data;
    }