0

我想在页面顶部的一个表格中显示团队成员列表及其信息,然后根据各自行上一个单元格内的值将它们分成团队。

“成员”表如下所示:

+------+------------------------+------------------+
| name | interests              | email            |
+------+------------------------+------------------+
| John | Golf, Hockey, Baseball | John@example.com |
| Fred | Hockey                 | Fred@example.com |
+------+------------------------+------------------+

这是我的代码的简化版本:

$result = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($result)){ 
echo "$row['name'] . $row['interests'] . $row['email']"
}

while($row = mysql_fetch_array($result)){
$sport = array($row['interests']);
if (in_array("Golf", $sport)) {
echo "$row['name'] . $row['email']"
}
if (in_array("Hockey", $sport)) {
echo "$row['name'] . $row['email']"
}
if (in_array("Baseball", $sport)) {
echo "$row['name'] . $row['email']"
}

如何让页面看起来像这样?:

所有成员

姓名:约翰
兴趣:高尔夫、曲棍球、棒球
电子邮件:john@example.com

姓名:弗雷德
兴趣:曲棍球
电子邮件:fred@example.com

团队

高尔夫球手:
约翰 - john@example.com

曲棍球运动员:
约翰 - john@example.com
弗雷德 - fred@example.com

棒球运动员:
约翰 - john@example.com

4

4 回答 4

0

这是未经测试的,但您可以尝试做一些事情,例如查询所有成员以获得兴趣,然后使用 foreach 循环遍历每个具有该兴趣的成员。

$result = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($result)){ 

$interests =  $row['interests'];

foreach($interests as $interest){
     echo $interest." Players";


      $result = mysql_query("SELECT * FROM members WHERE interests='$interest'");

       while($row = mysql_fetch_array($result)){ 
         $sport = array($row['interests']);
          if (in_array($interest, $sport)) {
             echo "$row['name'] . $row['email']"
          }
       }


   }
}
于 2013-01-30T11:25:44.900 回答
0
// Fetch data
$db = new mysqli( 'localhost', 'root', '', 'test' );
$query = "SELECT * FROM members";
$result = $db->query( $query );

// Pre-create sorted arrays
$members = array();
$teams = array();
while ( $row = mysqli_fetch_row( $result ) ) {
    $members[] = $row;
    $tm = explode( ',', $row[1] );
    foreach( $tm as $game ) {
        $teams[trim($game)][] = $row[0] . ' - ' . $row[2];
    }
};

// Echo the result
echo '<h2>All members</h2>';
foreach( $members as $member ) {
    echo 'Name: '.$member[0].'<br/>';
    echo 'Interests: '.$member[1].'<br/>';
    echo 'E-mail: '.$member[2].'<br/><br/>';
}

echo '<h2>Teams</h2>';
foreach( $teams as $sport => $team ) {
    echo '<b>'.$sport.'</b></br>';
    foreach( $team as $player ) {
        echo $player . '</br>';
    }
    echo '<br />';
}

结果:

All members
Name: John
Interests: Golf, Hockey, Baseball
E-mail: John@example.com

Name: Fred
Interests: Hockey
E-mail: Fred@example.com

Teams
Golf
John - John@example.com

Hockey
John - John@example.com
Fred - Fred@example.com

Baseball
John - John@example.com
于 2013-01-30T11:28:46.627 回答
0
<?php

$result = mysql_query("SELECT * FROM members");
while($row = mysql_fetch_array($result)){
    $members[] = $row; //bulid members array
    $member_teams = explode(',',$row['interests']); //get array of interests for row
    foreach ($member_teams as $team) { //cast name and emial to team list
       $teams[$team]['title'] = $team;
       $teams[$team]['members'][] = array('name'=>$row['name'],'email'=>$row['email']);
    }    
}



//bulid members list
if (!empty($members)) {
    echo 'All Members<br />';

    foreach ($members as $member) {
        echo 'Name: '. $member['name'];
        echo '<br /> Interests:'. $member['interests'];
        echo '<br /> Email: '. $member['email'];
    }

    echo '<hr>';
    echo '<br />Teams<br />';

    foreach ($teams as $team) {
        echo '<br />'.$team['title'];
        foreach ($team['members'] as $member) { //bulid list of team members
            echo '<br />'.$member['name']. ' - ' . $member['email'];
        }
    }

    // these are lists so i preffer <ul><li>
    // but <br /> is no problem for me


?>
于 2013-01-30T11:29:01.860 回答
0

此代码显示所有成员和团队

 $result = mysql_query("SELECT * FROM members");
 $members=array();
 while($row = mysql_fetch_array($result)){ 
     $members[]=$row;
 }
 echo "All Members:<br>\n";
 foreach($members as $row){
    echo "Name: ".$row['name'] ."<br>Interests: ". $row['interests']."<br>Email:". $row['email']."<br>\n";
 }
 echo "Teams:<br>\n";
 echo "Hockey Players:<br>\n";
 foreach($members as $row){
    if($row['interests']=="Hockey"){
        echo $row['name'] ."-". $row['email']."<br>\n";        
    }
 }
 echo "Golfers Players:<br>\n";
 foreach($members as $row){
    if($row['interests']=="Golfers"){
        echo $row['name'] ."-". $row['email']."<br>\n";        
    }
 }
 echo "Baseball Players:<br>\n";
 foreach($members as $row){
    if($row['interests']=="Baseball"){
        echo $row['name'] ."-". $row['email']."<br>\n";        
    }
 }
于 2013-01-30T11:29:02.480 回答