4

Structure table Comments:

id (int 11)
NewsIdn (varchar 10)
CommentsIdn (varchar 10) //
ForCommentsIdn (varchar 10) //have CommentsIdn comments(answers) reply to which users 
Text (varchar 100)
DateCreate (datetime)

I would like output comments and comments(answers) for comments, ie. structure output data on page:

-- comment 1
-----answer on comment 1
--------answer on answer on comment 1 (№1)
--------answer on answer on comment 1 (№2)
--------answer on answer on comment 1 (№3)
----------answer on answer №3 on answer on comment 1
-------------
---------------N
                       ***
                       ***
                       ***
-- comment 2
-----answer on comment 2
--------answer on answer on comment 2 (№1)
--------answer on answer on comment 2 (№2)
--------answer on answer on comment 2 (№3)
----------answer on answer №3 on answer on comment 2
-------------
---------------N

                       ***
                       ***
                       ***
-- comment N
-----answer on comment N
--------answer on answer on comment N (№1)
--------answer on answer on comment N (№2)
--------answer on answer on comment N (№3)
----------answer on answer №3 on answer on comment N

AND IE.

I get first comment:

SELECT * FROM COMMENTS WHERE NewsIdn='1122121' // value NewsIdn as example

But How output comments on comments in cycle ?

Tell me please how make it?

Raptor
  • 53,206
  • 45
  • 230
  • 366

1 Answers1

1

Yea you must need normalization... I create some tables for you.

Users Table

id(PK) name  

News table

id (int 11)
Text
DateCreate (datetime)
user_id(FK with user table)

Comments table

id (int 11)
Text
DateCreate (datetime)
news_id(int 11)(FK with news table)
user_id

Sub comments for a comment table

id (int 11)
comments_id(FK with comments table)
Text
user_id(int)(FK with user table)
DateCreate (datetime)

And now write this following query for each comment

For cycling just use a a loop inside comments table. then you get all answer for each comments.

$query=query("select id from news");
while($q=mysql_fetch_assoc($query)){
//Here is each news
$query1=query("SELECT comments_id,comments FROM comments c WHERE c.news_id=$q['id']");
//Here is each comment
   while($q1=mysql_fetch_assoc($query1)){
   $query2=query("
                  SELECT sub.text,u.user_name FROM sub_comments AS sub
                  LEFT JOIN comments AS c
                  ON c.id=sub.comments_id
                  INNER JOIN user AS u
                  ON u.id=sub.user_id
                  WHERE c.id=$q1['comments_id']
                 ");
                 while($q2=mysql_fetch_assoc($query2)){
                    //Here are sub comments for each comments  
                    print $q2['text'];
                 }
    }
}
Imran
  • 3,031
  • 4
  • 25
  • 41
  • How exacly is this an answer to the question? – jeroen Dec 20 '13 at 02:10
  • I am writing query for this structue. may be OP can understand "How output comments on comments in cycle?" I will edit my answer soon – Imran Dec 20 '13 at 02:13
  • your answer get answers on comments level 1, but in question i ask how get all answers on comments and i show structure answers on comments. me need cycle which can help output all answers with y structure answers. –  Dec 20 '13 at 02:26
  • why you dont use php loop for cycle on above query? – Imran Dec 20 '13 at 02:30
  • Please see my updates – Imran Dec 20 '13 at 02:51
  • @imran your answer get answers on comments level 1, but in question i ask how get all answers on all comments(comments level2, level3,...levelN). with your answers we can get answers comments only on comments level 1. –  Dec 20 '13 at 03:02
  • wow, you must need news_id if you want unlimited comments and answer cycle and you need loop for specific news. ok I update my answer – Imran Dec 20 '13 at 03:11
  • please see updates. it should work. First loop retrieve each news and second loop retrieve each comment and third loop retrieve each comment answers. may you want this – Imran Dec 20 '13 at 03:31
  • @imran for your understand write please answer for all answers on all comments unto level 20 –  Dec 20 '13 at 03:42
  • please at first make sure my technique is work. if it works (shoul work) you can edit your level – Imran Dec 20 '13 at 03:48
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/43561/discussion-between-imran-and-learner) – Imran Dec 20 '13 at 03:51