I am trying to create a simple css menu that gets the data from a mysql table.
My idea is to have menu like this
Category 1
- link 1
- link 2
- link 3
Category 2
- link 1
- link 2
- ect...
every link has a field named "category". So I want to group and display the links in the menu per category.
I have mysql grouping like
$sql = "SELECT * FROM content group by category";
$result = mysql_query($sql);
and then I have the html like this
<ul class="menu">
<li id="category1" class="files">
<a href="#category1">Category 1</a>
<ul class="sub-menu">
<li><a href="#">link 1</li>
<li><a href="#">link 2</li>
<li><a href="#">link 3</li>
</ul>
</li>
<li id="category2" class="files">
<a href="#category2">Category 2</a>
<ul class="sub-menu">
<li><a href="#">link 1</li>
<li><a href="#">link 2</li>
<li><a href="#">link 3</li>
</ul>
</li>
</ul>
The db table looks like this
CREATE TABLE IF NOT EXISTS `content` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`menu_name` text,
`menu_name_en` text,
`menu_url` varchar(255) NOT NULL DEFAULT '',
`header_name` text,
`header_name_en` enum('MEDIA','GENERAL') NOT NULL DEFAULT 'MEDIA',
`text` longtext NOT NULL,
`text_en` text,
`category` enum('Category 1', 'Category 2') NOT NULL DEFAULT 'Category 1',
`date` date NOT NULL DEFAULT '0000-00-00',
`visible` char(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
INSERT INTO content (id, menu_name, menu_name_en, menu_url, header_name, header_name_en, text, text_en, category, date, visible) VALUES (26, 'test name', '', 'test_url', 'test name', '', '<p>test text</p>', '<p>text text</p>', 'MEDIA', '2014-02-23', '1');
So, I am having troubles putting the results in the loop and creating the html by category.
I read many posts here with similar content but couldn't achieve the result I wanted.
Any help will be much appreciated. Thanks!
See Question&Answers more detail:
os