php - Modify MySQL query to also show child records sorted under parent records in a menu -
i need build mysql database driven link menu shows links user has permission view.
the link menu have parent
, child
links. child links under parent links (like directory structure).
i have 2 database tables shown below. intranet_links
has link records , user_link_permissions
has permissions determine if user can view link or not.
here current sql query returns links user has permission view...
also here working demo see results http://sqlfiddle.com/#!9/8aa75/2
select intranet_links.id, intranet_links.title, intranet_links.url, intranet_links.description, intranet_links.sort_order, intranet_links.parent, user_link_permissions.user_id user_link_permissions join intranet_links on user_link_permissions.link_id=intranet_links.id , user_link_permissions.permission=1 user_link_permissions.permission=1 , user_link_permissions.user_id=1 order title
i need results sort in way let child links ordered under parent links.
i using php doesn't have sql. desird end result menu this...
database structure , data...
create table if not exists `intranet_links` ( `id` int(11) not null auto_increment, `title` varchar(255) default null, `description` text not null, `url` varchar(255) default null, `active` int(2) not null default '1', `sort_order` int(11) default null, `parent` int(10) not null default '1', primary key (`id`), unique key `id` (`id`) ) engine=innodb default charset=latin1 auto_increment=8 ; -- -- dumping data table `intranet_links` -- insert `intranet_links` (`id`, `title`, `description`, `url`, `active`, `sort_order`, `parent`) values ('1', 'lnk parent 1', '', 'google.com', 1, null, 0), ('2', 'ink 2', '', 'google.com', 1, null, 0), ('3', 'link 3', '', 'google.com', 1, null, 0), ('4', 'lnk parent 2', '', 'google.com', 1, null, 0), ('5', 'ink 5', '', 'google.com', 1, null, 2), ('6', 'link 6', '', 'google.com', 1, null, 2), ('7', 'ink 7', '', 'google.com', 1, null, 2), ('8', 'link 8', '', 'google.com', 1, null, 2); create table if not exists `user_link_permissions` ( `id` int(100) not null auto_increment, `user_id` int(30) not null, `link_id` int(30) not null, `permission` int(2) not null default '0', key `id` (`id`) ) engine=innodb default charset=latin1 auto_increment=8 ; -- -- dumping data table `user_link_permissions` -- insert `user_link_permissions` (`id`, `user_id`, `link_id`, `permission`) values (1, 1, 1, 1), (2, 1, 2, 1), (3, 1, 3, 0), (4, 1, 4, 1), (5, 1, 5, 1), (6, 1, 6, 0), (7, 1, 7, 1), (8, 1, 8, 1), (9, 2, 1, 1), (10, 2, 2, 1), (11, 2, 3, 1), (12, 2, 4, 1), (13, 2, 5, 1), (14, 2, 6, 1), (15, 2, 7, 1), (16, 2, 8, 1);
i think best best break 2 queries. first query of link_ids user has permission see.
select link_id user_link_permissions user_id=:user , permission=1
then result of query, second query links , order them want
select * intranet_links id in (123, 456, 789) order title
you subquery, prefer , have had great success breaking sql smaller pieces.
Comments
Post a Comment