I would like to set up a multi level comments that will look like that :
1 this is the first comment (with 2 replies)
1.1 first reply (with 2 replies)
1.1.1 first reply of first
1.1.2 second reply of first
1.2 second reply (with 1 reply)
1.2.1 first reply of second
2 this is the second comment (with 0 reply)
3 this is the third comment (with 0 reply)
I just discovered the drupal method which is pretty cool to sort the datas in the query but i don't know how to count the number of replies a comment has. I don't know if it's possible to do that in 1 sql query.
Here's my table structure :
CREATE TABLE IF NOT EXISTS `mb_post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`topic_id` int(11) NOT NULL,
`user` int(11) NOT NULL,
`message` text COLLATE utf8_unicode_ci NOT NULL,
`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`level` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`parent_post_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=0 ;
--
-- Dumping data for table `mb_post`
--
INSERT INTO `mb_post` (`id`, `topic_id`, `user`, `message`, `date`, `level`, `parent_post_id`) VALUES
(1, 99, 10, 'this is the first comment', '2013-10-18 11:00:00', '1', 0),
(2, 99, 20, 'this is the second comment', '2013-10-18 11:10:00', '2', 0),
(3, 99, 30, 'this is the third comment', '2013-10-18 11:20:00', '3', 0),
(4, 99, 20, 'first reply', '2013-10-18 14:30:00', '1.1', 1),
(5, 99, 50, 'first reply of first', '2013-10-18 15:00:00', '1.1.1', 4),
(6, 99, 70, 'second reply of first', '2013-10-18 15:30:00', '1.1.2', 4),
(7, 99, 80, 'second reply', '2013-10-18 14:45:00', '2.1', 2),
(8, 99, 90, 'first reply of second', '2013-10-18 15:15:00', '2.1.1', 7);
As you can see i have the level
column in varchar (i couldn't find other way to use multi decimal).
Is there a way to count the number of reply a comment has ?
Thank you very much!
EDIT : Wanted result :
id | topic_id | user | message | date | level | parent_post_id | count
1 | 99 | 10 | this is the first comment | 2013-10-18 11:00:00 | 1 | 0 | 2
4 | 99 | 20 | first reply | 2013-10-18 14:30:00 | 1.1 | 1 | 2
5 | 99 | 50 | first reply of first | 2013-10-18 15:00:00 | 1.1.1 | 4 | 0
6 | 99 | 70 | second reply of first | 2013-10-18 15:30:00 | 1.1.2 | 4 | 0
2 | 99 | 20 | this is the second comment| 2013-10-18 11:10:00 | 2 | 0 | 1
7 | 99 | 80 | second reply | 2013-10-18 14:45:00 | 2.1 | 2 | 1
8 | 99 | 90 | first reply of second | 2013-10-18 15:15:00 | 2.1.1 | 7 | 0
3 | 99 | 30 | this is the third comment | 2013-10-18 11:20:00 | 3 | 0 | 0
The datas should be sorted by level and the count column should be the result of the number of its direct child. For ie: the comment 1
should count all the comments with level 1.1, 1.2, 1.3
but not 1.1.1