Page 1 of 1

Many-to-Many confusion...

Posted: Wed Jan 06, 2010 9:41 am
by MiniMonty
Hi all,

newb here learning as I go.

I've got a many-to-many db in which students, courses, course description and tutor
have relationships.

I'm trying to learn how to display various queries (and not doing very well).

My query looks like this:

Code: Select all

 
$id = mysql_real_escape_string($id);
$id = eregi_replace("`", "", $id);
$query = sprintf("SELECT c.* FROM students_to_courses stc INNER JOIN courses c ON stc.course_fk = c.course_id WHERE stc.student_fk = '$id'");
$result = mysql_query($query);
 
while ($row = mysql_fetch_assoc($result)) {
    $tutor = $row['tutor_fk'];
    $course_name = $row['course_name'];
    $course_descriptoin = $row['course_description'];
}
 
I want to display a list of courses that the member has signed up to.
So in the body of the html I tried this:

Code: Select all

 
<td colspan="4"><span class="orangeText"><?php print_r ("$course_name"); ?></span></td>
 
but it only lists one course that the member has signed up (and I know the member is signed up to three).

Any ideas ?

Best wishes
Monty

Re: Many-to-Many confusion...

Posted: Wed Jan 06, 2010 10:53 am
by AbraCadaver
Each time through the while loop it is fetching one row from the $result, so you need the HTML inside the while loop, or put the while loop in the HTML:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
    $tutor = $row['tutor_fk'];
    $course_name = $row['course_name'];
    $course_descriptoin = $row['course_description'];
    echo '<td colspan="4"><span class="orangeText">' . $course_name . '</span></td>';
}

Re: Many-to-Many confusion...

Posted: Wed Jan 06, 2010 3:48 pm
by MiniMonty
Thanks mate - that works sweetly.