using implode function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thatsme
Forum Commoner
Posts: 87
Joined: Sat Apr 07, 2007 2:18 am

using implode function

Post 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
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: using implode function

Post 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
Post Reply