开发者

Display data from two tables by grouping in PHP

开发者 https://www.devze.com 2023-02-22 08:42 出处:网络
I am new in php field. I have created 2 tables in Mysql 1.Group 2. Event. goup_id is primary key in group an开发者_开发问答d its foreign key in event.

I am new in php field. I have created 2 tables in Mysql 1.Group 2. Event. goup_id is primary key in group an开发者_开发问答d its foreign key in event.

I want to display events which is in specific group. Example : Group1 Event 1 Event 2

      Group2 
      Event 3 
      Event 4 etc 

In this way, first group title and then list of events in that group will come. I want to select data from 2 different table.

Please help!! Thanks


Something like this?:

$sql = "SELECT * FROM Event as e LEFT JOIN Group as g on g.group_id=e.group_id ORDER BY e.group_id";
$result = mysql_query($sql);
if($result){
    $currGroup = -1;
    while($row=mysql_fetch_array($result)){
       if($row['groupName']!=$currGroup){
         $currGroup = $row['groupName'];
         echo $currGroup."\n";
       }
       echo $row['eventName']."\n";
    }
}

0

精彩评论

暂无评论...
验证码 换一张
取 消