adambedford Posted March 13, 2010 Share Posted March 13, 2010 I've written some simple code that shows a list of music files that are in a database. I'm now trying to add a comments system so that users can add a comment to a specific track. I've got two tables, 'tracks' and 'comments' and the 'comments' table has a field called c_TrackID which relates to the field 't_ID' in the tracks table - all standard stuff. The problem I have is displaying the comments. My first thought was to use a LEFT JOIN query to return a recordset of the two but that hasn't worked out quite as I'd hoped. The query itself is fine but it doesn't repeat the comments within each track, it repeats each combination of track & comment. I'm looking for a solution to repeat all the tracks and within that list repeat all the comments for that particular track. I thought about querying the comments table within the while loop for the tracks but that will be quite slow and cumbersome and I'm sure there is a more efficient and easier way. I'd really appreciate any suggestions! Quote Link to comment Share on other sites More sharing options...
Instigate Posted March 14, 2010 Share Posted March 14, 2010 Im not quite sure what you are trying to achieve, do you simply want it to display the comments for each track? Quote Link to comment Share on other sites More sharing options...
adambedford Posted March 14, 2010 Author Share Posted March 14, 2010 Yeah I do, but at the moment it is displaying each track and a single comment and then repeating each track with a different comment. I want the comments to be displayed underneath each track and grouped together so that the tracks only display once. So yeah just to display the comments for each track. Quote Link to comment Share on other sites More sharing options...
Instigate Posted March 14, 2010 Share Posted March 14, 2010 The querys would have to be something like: <?php $tracks = mysql_query("SELECT * FROM tracks"); while($tracks_result=mysql_fetch_array($tracks)) { $comments = mysql_query("SELECT * FROM comments WHERE c_TrackID={$tracks_result['t_ID']}"); while($comment_result=mysql_fetch_array($comments)) { ?> /* HTML HERE /* <?php }} ?> If that is what you are doing just make sure there is no LIMIT, if you still have a problem after that. Show us the HTML you are echoing the comments into. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.