I have an array include name of the home team and away team . But i don't know how to display it in the same row . My array like this :
[8] => Array
(
[comm_match_team] => localteam
[player_name] => Fraizer Campbell
)
[9] => Array
(
[comm_match_team] => localteam
[player_name] => Jason Puncheon
)
[24] => Array
(
[comm_match_team] => visitorteam
[player_name] => Eden Hazard
)
[25] => Array
(
[comm_match_team] => visitorteam
[player_name] => Nemanja Matic
)
And my code
<tbody>
<?php foreach($teams as $team): ?>
<tr>
<?php if($team['comm_match_team'] == 'localteam'): ?>
<td class="home"><?php echo $team['player_name'] ?></td>
<?php else: ?>
<td class="away"><?php echo $team['player_name'] ?></td>
<?php endif ?>
</tr>
Everytime loop , it add tr tag too , but i want it :
<tr>
<td class="home">Fraizer Campbell</td>
<td class="away">Eden Hazard</td>
</tr>
<tr>
<td class="home">Jason Puncheon</td>
<td class="away">Nemanja Matic</td>
</tr>
Can anyone give me a solution for this problem ? Thanks
<?php
$c1 = [];
$c2 = [];
foreach($teams as $team) {
if($team['comm_match_team'] == 'localteam') {
$c1[] = $team['player_name'];
}
else
{
$c2[] = $team['player_name'];
}
}
$count = count($c1);
?>
<?php for($i = 0; $i < $count; $i++) { ?>
<tr>
<td class="home"><?php echo $c1[$i]; ?></td>
<td class="away"><?php echo $c2[$i]; ?></td>
</tr>
<?php } ?>
<tbody>
<tr>
<?php foreach($teams as $team): ?>
<?php if($team['comm_match_team'] == 'localteam'): ?>
<td class="home"><?php echo $team['player_name'] ?></td>
<?php else: ?>
<td class="away"><?php echo $team['player_name'] ?></td>
<?php endif ?>
//stop foreach here
//then close </tr>
</tr>
You should get your <tr>
out of your foreach loop. Right now everytime you code goes trough the loop it adds an <tr>
to your table