简体   繁体   中英

Display data from two tables by grouping in PHP

I am new in php field. I have created 2 tables in Mysql 1.Group 2. Event. goup_id is primary key in group and 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";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM