jQuery UI accordion menu from MySQL data

jQuery UI accordion menu from MySQL data

jquery ui accordion menu
This tutorial shows you how to create a cool jQuery UI accordion menu with the data from a MySQL table using PHP. Also you will find out some classes to override the default style with custom css.

Creating MySQL Table

Create a simple table called menu with 4 columns for this tutorial.
Insert some values to the table like in the sample below.
jquery-ui-accordion-menu-table
Rows with NULL parent id are parent menus

Basic HTML structure

This is the HTML structure we are aiming to create with the data from MySQL table.

Inserting basic jQuery UI accordion

First of all include jquery and jquery ui libraries and stylesheet. Then initialize accordion for the id #accordion like below.

PHP code

Basically we have two while blocks. Outer block is for parent menu and the inner while block is for sub menus.
In outer while loop, we are fetching the ids of the rows with NULL parent id. These are the parent menus.
In inner while loop, we are fetching the rows with the parent id corresponding to the value fetched from outer loop.

Custom CSS

Finally we can change the default jquery ui accordion menu to something cool and flat using custom css.
Change the above styles according to your taste. And we’re done.

0 comments :