943,545 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1292
  • PHP RSS
Dec 11th, 2008
0

How to join two table with same cells names

Expand Post »
Hello every one
I have a forum written in php language. What I want is to display all the comments made by one user on a certain topic.
The database had two tables "forum" and "comment".

Forum table is structured as follows:
id cat_id title date_time userid name post allow usesig iconid uploadfile reader c_comment sticky close lastuserid timestamp edit_by ip_address rating_total ratings

Comment table is structured as follows:
id modType thread_id userid name title comment news_c_allow cat_id usesig uploadfile timestamp allow

All comments are linked back to the original thread by the "thread_id". And of course the cell "userid" refer to the id of the user in the users table in the Database.

I want to retrieve all the comments by one user, however I want to display the title of the thread and not the title of the comment (Even if it occurs more than one time).

I use this code to retrieve the information:
PHP Syntax (Toggle Plain Text)
  1. $result = $apt->query("SELECT DISTINCT forum.*, comment.*
  2. FROM forum JOIN comment
  3. ON forum.userid=comment.userid
  4. WHERE comment.userid = $userid
  5. ORDER BY forum.timestamp DESC
  6. LIMIT $start,$perpagelist");
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
khr2003 is offline Offline
64 posts
since Dec 2008
Dec 12th, 2008
0

Re: How to join two table with same cells names

Hi...
What you are asking is not clear...
Let me try..

you want to display the thread title and not the comment title in the query.... If this is the case.. then join the table threads also and join it on comment.thread_id = threads.thread_id

I hope you are having threads table..
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Dec 12th, 2008
0

Re: How to join two table with same cells names

yes that is excactly what I wanted. Actually forum table is the threads table (I know the wrong name) but thanks I will try it and see
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
khr2003 is offline Offline
64 posts
since Dec 2008
Dec 14th, 2008
0

Re: How to join two table with same cells names

Thank you very much. it worked.
I have one more question though. Since both tables have the same cell name for the title, how to do I view the two titles? I mean forum.title and comment.title
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
khr2003 is offline Offline
64 posts
since Dec 2008
Dec 14th, 2008
0

Re: How to join two table with same cells names

Hi...
See for this thing, although you can join the two tables with the help of outer join
(search on google for more join types, use outer join as outer join will be a better idea because you have usually one thread/forum but have many comments associated with that)

but i will preferably recommend you only one thing, that use two different queries for this purpose...

for viewing the forum title..
select * from forums where forumid=1;
for the comments:
select * from omments where threadid=5 and forumid=1;
Last edited by peter_budo; Dec 15th, 2008 at 4:00 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Dec 24th, 2008
0

Re: How to join two table with same cells names

Thanks for answering
But would not this add pressure on the server, if I had twenty raw per page then I would have forty queries. isn't there an easier way?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
khr2003 is offline Offline
64 posts
since Dec 2008
Dec 24th, 2008
0

Re: How to join two table with same cells names

Click to Expand / Collapse  Quote originally posted by khr2003 ...
Hello every one
I have a forum written in php language. What I want is to display all the comments made by one user on a certain topic.
The database had two tables "forum" and "comment".

Forum table is structured as follows:
id cat_id title date_time userid name post allow usesig iconid uploadfile reader c_comment sticky close lastuserid timestamp edit_by ip_address rating_total ratings

Comment table is structured as follows:
id modType thread_id userid name title comment news_c_allow cat_id usesig uploadfile timestamp allow

All comments are linked back to the original thread by the "thread_id". And of course the cell "userid" refer to the id of the user in the users table in the Database.

I want to retrieve all the comments by one user, however I want to display the title of the thread and not the title of the comment (Even if it occurs more than one time).

I use this code to retrieve the information:
PHP Syntax (Toggle Plain Text)
  1. $result = $apt->query("SELECT DISTINCT forum.*, comment.*
  2. FROM forum JOIN comment
  3. ON forum.userid=comment.userid
  4. WHERE comment.userid = $userid
  5. ORDER BY forum.timestamp DESC
  6. LIMIT $start,$perpagelist");


Hi,
What exactly you want is, just retrieve all comments from one user from comment table?
OK!

You have to write first;
php Syntax (Toggle Plain Text)
  1. // made your database connections here
  2. $q1=mysql_query("select * from forum");
  3.  
  4. while($row=mysql_fetch_array($q1))
  5. {
  6. $userid=$row['userid'];
  7.  
  8. $q2=mysql_query("select * from comments where comments.userid='$userid'");
  9. while($row1=mysql_fetch_array($q2))
  10. {
  11. echo $row1['comment'];
  12. echo $row1['userid'];//display whatever you want
  13. }
  14. }
  15. mysql_close($con);
Last edited by peter_budo; Dec 29th, 2008 at 5:33 am. Reason: Keep It Organized - Use [bbcode] only when it is necessary to the comprehension of your post.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ashafaaiz is offline Offline
20 posts
since Dec 2008
Dec 24th, 2008
0

Re: How to join two table with same cells names

thanks for the reply.

Actually, i am aware of what the code you posted but it not 100% what i want. I want to display the title of the thread that the comment of the user is linked to (and not to display the comment itself), I hope that i am making my self clear
Last edited by khr2003; Dec 24th, 2008 at 6:16 am.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
khr2003 is offline Offline
64 posts
since Dec 2008
Dec 24th, 2008
0

Re: How to join two table with same cells names

sql Syntax (Toggle Plain Text)
  1. SELECT
  2. forum.title,
  3. comments.comment
  4. FROM
  5. forum
  6. LEFT JOIN comments ON ( forum.id = comments.thread_id )
  7. WHERE
  8. comments.userid = $iUserID AND
  9. forum.id = $iForumID

this would yield something like this:
+---------------+------------------------+
| title | comment |
+---------------+------------------------+
| Forum Title | Comment text #1 |
| Forum Title | Comment text #2 |
| Forum Title | Comment text #3 |
| Forum Title | Comment text #4 |
+---------------+------------------------+
Sponsor
Reputation Points: 265
Solved Threads: 126
Practically a Master Poster
mschroeder is offline Offline
624 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP and Flash Form help
Next Thread in PHP Forum Timeline: Using PHP/MySql with IIS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC