PHP - 在循环中添加分隔符

时间:2013-09-17 10:17:25

标签: php mysql

昨天我问过这个问题,但是在我试图获得进一步的帮助后没有人回复。我一直试图找出我在previous post中给出的代码中的问题,但它没有给我我想要的结果。它现在做的是它在每一行之后写一个分隔符,但我真正想要的是如果前一行的管理器名称不同,那么写一个分隔符并继续循环。

while ($row = mysqli_fetch_array($results)) {
    $team = $row[0]['Manager'];
    foreach($row as $rows){
        if($rows['Manager'] !== $team){
            echo "<tr><td colspan=\"3\">Separator</td></tr>";
        }
        echo "<tr>";
            echo "<td>".$row['Manager']."</td>";
            echo "<td>".$row['Employee_ID']."</td>";
            echo "<td>".$row['Name']."</td>";
        echo "</tr>";
    }
}

5 个答案:

答案 0 :(得分:1)

您正在混合$row$rows,您可以这样做:

$team = "";
while ($row = mysqli_fetch_array($results)) {
    if($team && ($row['Manager'] != $team)){ // skip first separator
       echo "<tr><td colspan=\"3\">Separator</td></tr>";
    }
    $team = $row['Manager'];
    echo "<tr>";
        echo "<td>".$row['Manager']."</td>";
        echo "<td>".$row['Employee_ID']."</td>";
        echo "<td>".$row['Name']."</td>";
    echo "</tr>";
}

答案 1 :(得分:0)

while ($row = mysqli_fetch_array($results)) {

    $team = $row[0]['Manager'];

    foreach($row as $rows){
        if($rows['Manager'] !== $team) {
            $team = $row['Manager'];
            echo "<tr><td colspan=\"3\">Separator</td></tr>";
        }
        echo "<tr>";
        echo "<td>".$row['Manager']."</td>";
        echo "<td>".$row['Employee_ID']."</td>";
        echo "<td>".$row['Name']."</td>";
        echo "</tr>";
    }      
}

答案 2 :(得分:0)

我是否遗漏了某些内容,或者您​​只是简单地显示数据,然后在循环中进一步将其分配给变量?

while ($row = mysqli_fetch_array($results)) {
    $team = $row[0]['Manager'];
    foreach($row as $rows){
    if($row['Manager'] !== $team){
        echo "<tr><td colspan=\"3\">".$team."</td></tr>";
    }
    echo "<tr>";
        echo "<td>".$row['Manager']."</td>";
        echo "<td>".$row['Employee_ID']."</td>";
        echo "<td>".$row['Name']."</td>";
    echo "</tr>";
    $team = $row[0]['Manager'];
    }
}

答案 3 :(得分:0)

什么是$rowS['Manager']$rowS未知。使用$row['Manager']之类的:

    if($row['Manager'] !== $team){
        echo "<tr><td colspan=\"3\">Separator</td></tr>";
    }

答案 4 :(得分:0)

只需在if条件中移动值分配$team = $rows['Manager'];即可。

while ($row = mysqli_fetch_array($results)) {
    $team = "";
    foreach($row as $rows){
        if($rows['Manager'] != $team){
            echo "<tr><td colspan=\"3\">Separator</td></tr>";
            $team = $rows['Manager'];
        }
        echo "<tr>";
            echo "<td>".$rows['Manager']."</td>";
            echo "<td>".$rows['Employee_ID']."</td>";
            echo "<td>".$rows['Name']."</td>";
        echo "</tr>";
    }
}