Page 1 of 1

using implode function

Posted: Sun Jul 13, 2008 1:13 pm
by thatsme
Hello,

I have 2 tables
tbl1:- id, stud_name, class_id

tbl2:-class_id, class_name

I have to display results in the below format,
class_name
student1, student2,....

The code i wrote is,

Code: Select all

 
$q = mysql_query("SELECT * FROM tbl1, tbl2 WHERE tbl1.class_id=tbl2.class_id");
while($r = mysql_fetch_assoc($q))
{
  $stud_name = $r['stud_name'];
  $class_name= $r['class_name'];
 
  $stud_class[$class_name][]=$stud_name;
} 
 
Can any one please tell me how to iterate the above array to get the results in above pattern?

Thanks

Re: using implode function

Posted: Sun Jul 13, 2008 2:00 pm
by jaoudestudios
You can do it all in the sql. This will be more efficient as sql is designed for data sorting and there will be less loops php loops.

I will try and dig out some similar examples