html表并用空行填充php

时间:2015-07-02 17:56:12

标签: php html

我正在尝试用数据填充表格。 我想要实现看起来像enter image description here

的东西

但是,我的结果是:

enter image description here

我想这可能与php IF语句有关。

这是我的代码:

<table class="tg">
    <tr>
        <th class="tg-s6z2" colspan="2" rowspan="4">OPPONENT</th>
        <th class="tg-s6z2" colspan="5">DIVISION</th>
    </tr>
    <tr>
        <td class="tg-s6z2" colspan="5">TEAM</td>
    </tr>
    <tr>
        <?php  
        foreach($rows as $row) {
            echo "<td class='tg-031e' colspan='2'>";
            echo $row["Date"];
            echo "</td>";
        }           
        ?>
    </tr>
    <tr>
    </tr>
    <?php  
        foreach($rows as $row) {
            echo"<tr>";
            echo "<td class='tg-031e' colspan='2'>";
            echo $row["teamName"];
            echo "</td>";

            if(!empty($row["Score"])){
                echo"<td>";
                echo$row["Score"];  
                echo "</td>";
            }else{
                echo "<td>&nbsp;</td>";
            }
            echo"</tr>";
        }           
    ?>  
</table>

$results的输出

Array ( 
    [0] => Array ( 
        [Date] => 2015-04-22 
        [0] => 2015-04-22 
        [Score] => 1:4 
        [1] => 1:4 
        [divisionID] => 2 
        [2] => 2 
        [3] => 2 
        [teamName] => TEAM YXZ 
        [4] => TEAM XYZ ) 
    [1] => Array ( 
        [Date] => 2015-04-15 
        [0] => 2015-04-15 
        [Score] => 2.5:2.5 
        [1] => 2.5:2.5 
        [divisionID] => 2 
        [2] => 2 
        [3] => 2 
        [teamName] =>  TEAM XYZ 'B' 
        [4] => TEAM XYZ 'B' 
    ) 
)

3 个答案:

答案 0 :(得分:0)

您的条件语句检查$row['score']表示添加带有分数的TD或添加TD一无所有。您仍然需要添加TD,无论它是否为空白,否则会破坏表格布局。

此外,您获得的数据是否足以填充整个表格的循环数据?在我看来,随着条件语句一起返回数据会发生一些事情。

答案 1 :(得分:0)

您需要遍历列标题的日期,检查$rows的当前元素是否与相应的日期匹配。首先在您创建标题时列出所有日期:

    $dates = array();
    foreach($rows as $row) {
        echo "<td class='tg-031e' colspan='2'>";
        echo $row["Date"];
        $dates[] = $row['Date'];
        echo "</td>";
    }           

然后当你编写行时,循环遍历日期。匹配时,写下分数,否则将<td>留空(不需要写&nbsp;)。

    foreach($rows as $row) {
        echo"<tr>";
        echo "<td class='tg-031e' colspan='2'>";
        echo $row["teamName"];
        echo "</td>";
        foreach ($dates as $date) {
            echo "<td>";
            if ($row['Date'] == $date && !empty($row['Score'])) {
                echo $row["Score"];
            }
            echo "</td>";
        }
        echo"</tr>";
    }  

答案 2 :(得分:0)

您提供的示例图片与$rows所拥有的数据不对应。 2015-04-22上的匹配应该得分为1:4吗?

我将逻辑与HTML分开了。

在我看来,构建$rows的方式通常是糟糕的设计。

考虑到这一点;这是我想出来的;

<?php

$dates = array();
foreach ($rows as $row):
    $dates[] = $row['Date'];
endforeach;

$matches = array();
foreach ($rows as $row):
    $scores = array();
    foreach ($dates as $date):
        $score = '';
        if (!empty($row['Score']) && $row['Date'] === $date):
            $score = $row['Score'];
        endif;
        $scores[] = $score;
    endforeach;
    $matches[] = array('teamName' => $row['teamName'], 'Scores' => $scores);
endforeach;
?>

<table class="tg">
    <tr>
        <th class="tg-s6z2" colspan="2" rowspan="4">OPPONENT</th>
        <th class="tg-s6z2" colspan="5">DIVISION</th>
    </tr>
    <tr>
        <td class="tg-s6z2" colspan="5">TEAM</td>
    </tr>
    <tr>
        <?php foreach ($dates as $date): ?>
            <td>
                <?php echo $date; ?>
            </td>
        <?php endforeach; ?>
    </tr>
    <tr>
    </tr>
    <?php foreach ($matches as $match): ?>
        <tr>
            <td class="tg-031e" colspan="2">
                <?php echo $match['teamName']; ?>
            </td>
            <?php foreach ($match['Scores'] as $score): ?>
                <td><?php echo $score; ?></td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>