简体   繁体   中英

MySQL query for hierarchical table structure

I am trying to build a comment system below my articles where users can reply on the comment as well. Comment or its reply can be posted by only logged-in users.

There I have created 4 tables in MySQL.

User Table

 <style type="text/css"> table.tableizer-table { border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #ccc; } .tableizer-table th { background-color: #104E8B; color: #FFF; font-weight: bold; } </style> 
 <table class="tableizer-table"> <tr class="tableizer-firstrow"><th>id</th><th>email</th></tr> <tr><td>1</td><td>abc@gmail.com</td></tr> <tr><td>2</td><td>xyz@gmail.com</td></tr> <tr><td>3</td><td>pqr@gmail.com</td></tr> <tr><td>4</td><td>abc@yahoo.com</td></tr> <tr><td>5</td><td>xyx@yahoo.com</td></tr> </table> 

Profile Table

 <style type="text/css"> table.tableizer-table { border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #ccc; } .tableizer-table th { background-color: #104E8B; color: #FFF; font-weight: bold; } </style> 
 <table class="tableizer-table"> <tr class="tableizer-firstrow"><th>id</th><th>user_id</th><th>fname</th><th>lname</th></tr> <tr><td>1</td><td>3</td><td>PQR</td><td>Gmail</td></tr> <tr><td>2</td><td>2</td><td>XYZ</td><td>Gmail</td></tr> <tr><td>3</td><td>1</td><td>ABC</td><td>Gmail</td></tr> <tr><td>4</td><td>5</td><td>XYZ</td><td>Yahoo</td></tr> <tr><td>5</td><td>4</td><td>ABC</td><td>Yahoo</td></tr> </table> 

Article Table

 <style type="text/css"> table.tableizer-table { border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #ccc; } .tableizer-table th { background-color: #104E8B; color: #FFF; font-weight: bold; } </style> 
 <table class="tableizer-table"> <tr class="tableizer-firstrow"><th>id</th><th>article_title</th><th>article_content</th></tr> <tr><td>1</td><td>Helooooooo</td><td>Hi, How are you?</td></tr> <tr><td>2</td><td>Hiiiiiiiiiiii</td><td>Hey, How are you?</td></tr> <tr><td>3</td><td>Heeeeeey</td><td>Hello, How are you?</td></tr> </table> 

Comment Table

 <style type="text/css"> table.tableizer-table { border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif; font-size: 12px; } .tableizer-table td { padding: 4px; margin: 3px; border: 1px solid #ccc; } .tableizer-table th { background-color: #104E8B; color: #FFF; font-weight: bold; } </style> 
 <table class="tableizer-table"> <tr class="tableizer-firstrow"><th>id</th><th>article_id</th><th>user_id</th><th>parent_comment_id</th><th>Comment</th></tr> <tr><td>1</td><td>2</td><td>5</td><td>0</td><td>Great Article</td></tr> <tr><td>2</td><td>2</td><td>2</td><td>0</td><td>Nice Article</td></tr> <tr><td>3</td><td>2</td><td>4</td><td>1</td><td>I agree Great Article</td></tr> <tr><td>4</td><td>2</td><td>1</td><td>1</td><td>I also agree Great Article</td></tr> <tr><td>5</td><td>2</td><td>3</td><td>0</td><td>Bad Article</td></tr> <tr><td>6</td><td>2</td><td>4</td><td>2</td><td>OK Artilce</td></tr> </table> 

I want my output to display as:

Name: XYZ Yahoo (Comment)
Great Article   

Name: ABC Yahoo (Reply on 1st Comment)
I agree Great Article

Name: ABC Gmail (Reply on 1st Comment)
I also agree Great Article

Name: XYZ Gmail (Comment)
Nice Article    

Name: ABC Yahoo (Reply on 2nd Comment)
OK Artilce

Name: PQR Gmail (Comment)
Bad Article 

In the above comment system i am using hierarchical table structure to store comment and its replys.

Hope i have been able to explain my problem.

Can someone help me with MySQL statement and php code

I would tweak the order by clause a little bit so you would get all posts in the right order on php side, so you can simply loop through the resultset.

$sql = 'SELECT  c.id, parent_comment_id,`comment`, p.fname, p.lname FROM comment c INNER JOIN profile p on c.user_id=p.user_id INNER JOIN WHERE article_id='.$article_id . ' ORDER BY IF(parent_comment_id=0,id,parent_comment_id) ASC, id ASC;'

While looping through the resulset in php I would build an array to hold the ids of each record to determine their rank within the comments. Although, I should note, that on forums you can rarely see the reference that a comment is a response to the first comment, you can usually see that a comment is a response to a comment made by sy at such and such time. Anyway, the helper array can hold user names and timestamps of the records as well.

//outside the loop
//initialise rowcount variable to 0
$rowcount=0
...

//within the loop
//rowcount variable
$rowcount++;
$helper[$row['id']]=$rowcount;
...
//if a comment has a parent comment, get the parent's rownumber
echo 'Reply to ' . $helper[$row['parent_comment_id']] . '. comment';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM