Retros
Retros

Reputation: 49

Foreach loop creates empty table rows in html output

I´ve the following table created out of a foreach loop.

echo '<table id="myTable" class="tablesorter">';
echo '<tbody>';
echo '<thead>';
echo '<tr>';
echo '<th>';
echo "Charaktername";
echo '</th>';
echo '<th>';
echo "Server";
echo '</th>';
echo '<th>';
echo "Level";
echo '</th>';
echo '<th data-sorter="false" data-filter="false" search="false">';
echo "";
echo '</th>';
echo '</tr>';
echo '</thead>';
    
foreach($data2['wow_accounts']['0']['characters'] as $character) {
    $found = true;
    foreach ($names as $name) {
        if ($character['name'] == $name['firstname'] && $character['realm']['name'] == $name['lastname']) {
            $found = false;
            break;
        }
    }
    
    $class_string = $found ?  '' : ' class="my-additional-class"';
    echo '<tr' . $class_string . '>';
    
    if($character['level'] > 20) {
        echo '<td align="middle"' . $class_string . '>';
        echo $character['name'];
        echo '</td>';
    } else {
    
    }
    
    if($character['level'] > 20) {
        echo '<td align="middle"' . $class_string . '>';
        echo $character['realm']['name'];
        echo '</td>';
    } else {
    
    }
    
    if($character['level'] > 20) {
        echo '<td align="middle"' . $class_string . '>';
        echo $character['level'];
        echo '</td>';
    } else {
    
    }
    
    if($character['level'] > 20) {
        echo '<td align="middle"' . $class_string . '>';
        echo '<button class="btnSelect" data-target="xxx">Select</button>';
        echo '</td>';
    } else {
    
    }
    
}
echo '</tbody>';
echo '</table>';

Problem: if the condition if($character['level'] > 20 doesn´t fit the foreach loop creates an empty table row. This causes that there are spaces between some of the rows.

How can I make it that there aren´t any rows created if this condition doesn´t fit?

Upvotes: 0

Views: 478

Answers (1)

Barmar
Barmar

Reputation: 781058

Put the condition around processing the entire array element and creating the row, instead of around each <td>.

foreach($data2['wow_accounts']['0']['characters'] as $character) {
    if($character['level'] > 20) {

        $found = true;
        foreach ($names as $name) {
            if ($character['name'] == $name['firstname'] && $character['realm']['name'] == $name['lastname']) {
                $found = false;
                break;
            }
        }
    
        $class_string = $found ?  '' : ' class="my-additional-class"';
        echo '<tr' . $class_string . '>';
    
        echo '<td align="middle"' . $class_string . '>';
        echo $character['name'];
        echo '</td>';
    
        echo '<td align="middle"' . $class_string . '>';
        echo $character['realm']['name'];
        echo '</td>';
    
        echo '<td align="middle"' . $class_string . '>';
        echo $character['level'];
        echo '</td>';
    
        echo '<td align="middle"' . $class_string . '>';
        echo '<button class="btnSelect" data-target="xxx">Select</button>';
        echo '</td>';
    
        echo '</tr>';
    }
}

Upvotes: 1

Related Questions