Jump to content

php-mysql


dannybrazil

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.