dannybrazil Posted June 22, 2011 Share Posted June 22, 2011 Hello people, I have a question, and I hope someone can SAVE me here. I have a DB table with these columns: teacher_name Student_name Comment date Now I have for example 1000 students but every day a teacher writes a comment for a student. NOT ALL STUDENTS have comments. what I want to do: when a teacher enters his backoffice page, he would be able to get from the DB ONLY the students he wrote comments to. But the thing is, he wrote, let's say, 100 comments, TO 20 students. Is there a way to have a code that: Gets the TOTAL comments of this teacher separate it to STUDENT names and give me an output with the names that I could print the results only for these 20 students and NOT all Hope you got it Quote Link to comment Share on other sites More sharing options...
AMcHarg Posted June 22, 2011 Share Posted June 22, 2011 Oh it was moved, okay. Quote Link to comment Share on other sites More sharing options...
AMcHarg Posted June 22, 2011 Share Posted June 22, 2011 This is fairly simple to achieve. Just add the appropriate filtering into your queries to extract where teacher = x and/or student = y. Quote Link to comment Share on other sites More sharing options...
AMcHarg Posted June 22, 2011 Share Posted June 22, 2011 Can you give an example of the output you want? After re-reading I think you mean that you want... Comments by TeacherX Comments to Student1 Comment 1 Comment 2 Comment 3 Comment 4 Comments to Student2 Comment 1 Comment 2 etc... is that correct? Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted June 22, 2011 Author Share Posted June 22, 2011 Exactly! But I don't/cannot know the STUDENT'S name BEFORE. I need the code to get ALL the names and then check for similarity or smt Quote Link to comment Share on other sites More sharing options...
AMcHarg Posted June 22, 2011 Share Posted June 22, 2011 With a carefully positioned IF statement and counter in a WHILE loop you can establish the student name and follow it with the comments to that student... $query = mysql_query("SELECT * FROM table WHERE teacher_name = 'Mrs Smith' ORDER by Student_name ASC, comment_date ASC"); $x=0; $previous_student = ''; while ($row = mysql_fetch_array($query)) { $current_student = $row['Student_name']; $comment = $row['comment']; $comment_date = $row['comment_date']; if (($x == 0) OR ($current_student != $previous_student)) { echo"Comments for $current_student<br>"; } echo"$comment_date $comment<br>"; $previous_student = $current_student; $x++; } I haven't tested the above so please forgive any syntax errors. Quote Link to comment Share on other sites More sharing options...
dannybrazil Posted June 22, 2011 Author Share Posted June 22, 2011 WOW...that was FAST... I will check it soon and let you know Thanks A LOT !!! 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.