HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Dynamically load data into bootstrap accordion navigation bar

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
bardynamicallybootstrapintonavigationaccordionloaddata

Problem

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:

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`)
)


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:


  
    getTopTierItems();
    foreach($top_items as $top_item) {
    extract($top_item);
    ?>

    " class="strong list-group-item" data-toggle="collapse" data-parent="#MainMenu">
       
      
    

    ">
      getSubTierItems($id); 
      foreach($sub_items as $sub_item) {
        $id2 = $sub_item['id'];
        $name2 = $sub_item['name'];
        $parent_id = $sub_item['parent_id'];
      ?>
        " class="strong list-group-item" data-parent="#">
          
        

        ">

        
        
    

    
  


Here is the PHP that fetches the data from the

Solution

Timestamp

Why do you insert an invalid value '0000-00-00 00:00:00' into your TIMESTAMP columns by DEFAULT? I think you would be much better served by using something relevant, like CURRENT_TIMESTAMP...

`date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,


And...

`date_edited` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,


Identifiers

Is there any particular reason your identifiers are varchar? Seems odd. Example:

`id` varchar(50) NOT NULL,


To me should be:

`id` INT NOT NULL AUTO_INCREMENT,


PDO

This function could use a MySQL procedure (and PHP PDO):

public function getTopTierItems(){


And this one especially:

public function getSubTierItems($id){

Code Snippets

`date_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_edited` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`id` varchar(50) NOT NULL,
`id` INT NOT NULL AUTO_INCREMENT,
public function getTopTierItems(){

Context

StackExchange Code Review Q#55410, answer score: 3

Revisions (0)

No revisions yet.