Hi
Suppose this situiation,
The relationship between stuents and subjects they are studing in a school is descriped as M:M
(Many to Many).
So, To handle it, it must be handled in three tables, let them as follows,
student table which will handle student entity
subject table which will handle subject entity
student_subject which will handle the studied subjects entity by a student
That's True?
Ok...
Let we go further,
For the purpose of my post I will make things as simple as possible, Now I will set a simple
design for each table as follows:
Student Table
ID | PK |
Student_Name | |
Birth_Date | |
Subject Table
ID | PK |
Subject_Name | |
Top_Degree | |
Student_Subject Table
stu_id | PK |
Sub_id | PK |
Now Let we populate them with DATA, they may appear as follows:
Student Table:
ID | Student_Name | Birth_Date |
1 John Doe 1980-01-26
2 Michel Jackson 1977-05-01
3 Said Bakr 1977-11-19
4 Nancy Chan 1981-02-23
5 Ghandi Amitab 1978-10-01
Subject Table
ID | Subject_Name | Top_degree |
1 PHP 100
2 DHTML 80
3 MySQL 100
4 HTML 50
Student_Subject Table
stu_id | sub_id |
1 2
1 4
3 2
Ok...
To retrive information from the data above, the following SQL query may be done:
SELECT student.Student_Name, subject.Subject_Name FROM
student, subject, student_subject WHERE
student.ID = student_subject.stu_id AND
student_subject.sub_id = subject.ID;
This will produce result as shown,
Student_Name | Subject_Name
John Doe | DHTML
John Doe | HTML
Said Bakr | DHTML
My Question is:
What's your sugestions to code a PHP code inwhich these information may be displayed as follows,
Student John Doe studied DHTML, HTML
Student Said Bakr studied DHTML,
You should notice the real world application will deal with more data and the performance issue
must be regarded.
The critical point here, is the record set will contain the same name twice (John Doe),
So on using a function such as mysql_fetch_array(), the returned array will includes two elements
with the same key value, so on looping throgh it (John Doe) will appears twice!?
Is there any way to make the result produced from the SQL query to be as follows:
Student_Name | Subject_Name
John Doe | DHTML
| HTML
Said Bakr | DHTML
So the repeated fields will have only one value.