Syn
Syn

Reputation: 39

Displaying data in tables depending on group

I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.

What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png

<div class="col-lg-6">
                <h3>Team Name Goes Here </h3>
   <?php              

echo "<table class='table table-striped'>";
echo "  <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    ";
while($row = mysqli_fetch_array($result)) {
    $teamName = $row['teamName'];
    $fName = $row['firstName'];
    $surName = $row['surName'];
    echo "
        <tbody>
      <tr>
        <td>$teamName</td>
        <td>$fName</td>
        <td>$surName</td>
      </tr>       
    </tbody>

     ";

} 

echo "</table>";
?>
            </div>

with my query :

$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID  ";

I know the idea I need to do but cannot get it done, so any help would be appreciated.

Upvotes: 2

Views: 93

Answers (3)

AnkiiG
AnkiiG

Reputation: 3488

Try as below (Please replace table column name as yours and mysql to mysqli):

    <?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
    <?php              
        echo "<table>";

        $teamName = "";
        $i=0;
        while($row = mysql_fetch_array($result)) 
        {
            if($teamName == "" || $teamName != $row['team'])
            {
                if($i!=0)
                    echo "</table>";

                echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
                $teamName = $row['team'];
                $i=0;
            }
            $fName = $row['fname'];
            $surName = $row['lname'];
            $email = $row['email'];

            if($i==0)
            {
                echo "<table class='table table-striped'><tr>
                <th>Firstname</th>
                <th>Lastname</th>
                <th>Email</th>
                </tr>";
            }

            echo "<tr>
                <td>$fName</td>
                <td>$surName</td>
                <td>$email</td>
                  </tr>";

            $i++;
        } 
        echo "</table>";
    ?>
</div></body></html>

Upvotes: 0

KTAnj
KTAnj

Reputation: 1356

Try this code

    <?php $teemid=array();
    while($row = mysqli_fetch_array($result)) {
          if(!in_array($row['teamID'],$teemid)){
                 array_push($teemid,$row['teamID']);  
             if(!empty($teemid)){ ?>
                   </tbody>
                  </table>       
                </div>
           <?php } 

     ?>
       <div class="col-lg-6">
           <h3><?php echo $row['teamName']; ?></h3>
           <table class='table table-striped'>
              <thead>
                 <tr>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Email</th>
                 </tr>

              </thead>
              <tbody>
             <?php } ?>
              <tr>
                  <td><?php echo $row['teamName']; ?></td>
                  <td><?php echo $row['firstName']; ?></td>
                  <td><?php echo $row['surName']; ?></td>
             </tr> 


    <?php } ?>
             </tbody>
           </table>       
       </div>

SQL Query Change as below

$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID  ORDER BY u.teamID";

Upvotes: 1

Faiz Rasool
Faiz Rasool

Reputation: 1379

You can do this logic

$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
    $teamid = $row['teamid'];
    $teamname = $row['teamname'];

    $teammemberquery = "select all member in the  where team = $teamid sql query";
    echo "<table>";
    while ($r = mysqli_fetch_array($teammemberquery)) {
        $teamName = $r['teamName'];
        $fName = $r['firstName'];
        $surName = $r['surName'];
        echo "
        <tbody>
      <tr>
        <td>$teamName</td>
        <td>$fName</td>
        <td>$surName</td>
      </tr>       
    </tbody>
     ";
    }
    echo "</table>";
}

Upvotes: 0

Related Questions