Sunday, 25 March 2018

How do I sort my comments/replies/replies to replies data properly

I am using Nodejs, Express, MySQL, EJS.Users are able to create posts and comment/reply to comments/reply to replies on those posts.The problem: I don't know how to sort the data into objects/arrays in a way that will allow me to render them in EJS. I know how to do it for comments and replies to the comments, but not to replies to replies. How to create an object/array(or objects/arrays) which will make it "easier/simpler" to manage the comments, replies and replies of replies of replies(...etc)?The idea: I believe the end product should look something like this:When rendering EJS, it will check the sorted data for comments, if there are comments, it will create 1 container div for that single comment(and for the replies of that single comment), each other comment will have it's own container div.In that container div, will be the main comment div which will contain only the comment.Below the comment div will be the replies to the comment div(maybe the replies to replies will be stored here too, or a div specifically for that will be created)(I don't need to do it in this way, you are allowed to do it in your own way, I just think this is not a bad way to do it)The "comments" MySQL table is: ID comment ReplyToCommentID -------------------------------------------------------- 1 | First Updated Comment | NULL 2 | Second Comment | NULL 3 | Third Comment | NULL 4 | 4th Comment | NULL 5 | This is a reply to comment ID 1 | 1 6 | Reply to Comment ID 4 | 4 7 | Testing here | NULL 8 | TriHard 7 comment id 7 | NULL The value in ReplyToCommentID means that comment is a reply to the comment with the ID of that value, the comments with ID 5 and 6 are replies to the comments with ID 1 and 4, if it's NULL, it means it's a normal comment to the post.Getting them from MySQL to Node returns them in this format: [{"id":1,"comment":"First Updated Comment","ReplyToCommentID":null},{"id":2,"comment":"Second Comment","ReplyToCommentID":null},{"id":3,"comment":"Third Comment","ReplyToCommentID":null},{"id":4,"comment":"4th Comment","ReplyToCommentID":null},{"id":5,"comment":"This is a reply to comment ID 1","ReplyToCommentID":1}, {"id":6,"comment":"Reply to Comment ID 4","ReplyToCommentID":4},{"id":7,"comment":"Testing here","ReplyToCommentID":null}, {"id":8,"comment":"TriHard 7 comment id 7","ReplyToCommentID":null}] Separating the comments and replies into their own object gives this. (Obviously) this is replies {"1":"This is a reply to comment ID 1","4":"Reply to Comment ID 4"} This is comments {"1":"First Updated Comment","2":"Second Comment","3":"Third Comment","4":"4th Comment","7":"Testing here","8":"TriHard 7 comment id 7"} I have spent a couple of days trying to figure this out, but it seems it's too much for my "student experience". (I am self-taught so I don't have someone to ask for help in real life)I would be very thankful if you help me with this.Thank you for your time.

Submitted March 25, 2018 at 04:33PM by khazha88

No comments:

Post a Comment