Jump to content

MySQL Left Join Help


sportsreport2

Recommended Posts

First off i have three tables in a database. One is called "student" which has fields student_id, firstname and lastname. The next table is called "class" which has class_id, title, date. The last table is called "att" which has a att_id, class_id, student_id.

 

I am trying to display every students first and last names and show whether or not they have attended a class but only show attendance for one class per page.

 

Example:

 

Page 1 (Title1, date1)

Firstname Lastname, attended

Firstname Lastname, did not attend

Firstname Lastname, did not attend

Firstname Lastname, attended

Firstname Lastname, attended

 

But i can't seem to figure out how to have every students name displayed when they haven't attended the class.

 

I tried to use a Left Join to join student and att tables then display firstname and lastname but when i put WHERE title_id it changes and only shows the students who have attended the class.

 

Help Please

Link to comment
Share on other sites

    <table width="20%" border="1" cellspacing="0" cellpadding="0" align="center">
    <tr>
    <td><strong>Name</strong></td>
    <td><strong>Attendance</strong></td>
    </tr>
  <? 
$result = mysql_query("
SELECT *
FROM student
LEFT JOIN att
ON student.student_id=att.student_id
WHERE class_id=1
");
while($row = mysql_fetch_array($result))
  {

?> 
   <tr>      
        <td align="center"><? echo $row['firstname']; ?> <? echo $row['lastname']; ?></td>
        <td align="center">(NOT SURE WHAT TO PUT HERE i would like it to say attended or not attended  based on if they did or not)</td>
      </tr>
  <? } ?>
    </table>

 

Link to comment
Share on other sites

- Don't use short tags () use <?php.

- Your actual JOIN looks fine.

 

I tried to use a Left Join to join student and att tables then display firstname and lastname but when i put WHERE title_id it changes and only shows the students who have attended the class.

I don't see any condition with "title_id" in it.

 

 

Link to comment
Share on other sites

I meant to say class_id not title_id.

 

For clarification this is what my output needs to be like...

So I have a page of links that have the class title and date printed, when the teacher clicks on the title/date it would go to a separate page that lists every students name and whether they attended the class or not, by saying "Attended" or "Not Attended." Then the teacher wants to be able to click on the "Attended" or "Not Attended" and be able to switch it to the other one. For example if Student A did not attend the teacher would click on "Not Attended" and it would automatically change it to "Attended"

 

So im not sure if my database is set up wrong or what but i can't seem to figure it out.

 

thanks for your help

 

PS whats wrong with <? ?> why do you need the php if it all works the same?

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.