php - 如何在循环中排除一行代码

时间:2016-08-10 17:48:51

标签: php loops

我在表格中有HTML代码。我希望循环只是忽略它们

<?php
$sel_admin = "query  ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{      
    echo "<th>". $row['a'].  "</th>";


    </thead> // This two line of code
    <tbody>  // is the one I want to exclude in the while loop


    $sel_admin2 = "query2  ";
    $rs_admin2 = mysql_query($sel_admin2);
    while($row2 = mysql_fetch_array($rs_admin2))
    {
        echo" <tr class='gradeX'> ";
        echo "<td>" . $row2['sched3_time'].  "</td>";
        echo"</tr>";
    }
}
?>

这甚至可能吗?

4 个答案:

答案 0 :(得分:1)

你需要结束你的第一个循环,吐出html,然后再次启动循环,没有经过测试,但我认为现在应该可以使用了。

 <?php
$sel_admin = "query  ";
$rs_admin  = mysql_query($sel_admin);
while ($row = mysql_fetch_array($rs_admin)) {
    echo "<th>" . $row['a'] . "</th>";
}
?>
</thead> 
<tbody>
<?php
$sel_admin2 = "query2  ";
$rs_admin2  = mysql_query($sel_admin2);
while ($row2 = mysql_fetch_array($rs_admin2)) {
    echo " <tr class='gradeX'> ";
    echo "<td>" . $row2['sched3_time'] . "</td>";
    echo "</tr>";
}
?> 

答案 1 :(得分:0)

请使用mysqli而不是mysql。看看:MySQL vs MySQLi when using PHP + 你的问题也是答案。

<?php
     $sel_admin = "query  ";
     $rs_admin = mysqli_query($connection,$sel_admin);
     while($row = mysqli_fetch_array($rs_admin))
     {      
        echo "<th>". $row['a'].  "</th>";

        ?>
        </thead>
        <tbody>
        <?php

        $sel_admin2 = "query2  ";
        $rs_admin2 = mysqli_query($connection, $sel_admin2);
        while($row2 = mysqli_fetch_array($rs_admin2))
        {      
           echo" <tr class='gradeX'> ";
           echo "<td>" . $row2['sched3_time'].  "</td>";
           echo"</tr>";
        }
    }
?> 

答案 2 :(得分:0)

我猜你想要打印一次这些线条,而不是在循环之外。您可以使用变量来跟踪它:

$linesNeeded = true;
while (...) {
  ...
  if ($linesNeeded) {
    echo $line1;
    echo $line2;
    $linesNeeded = false;
  }
  ...
}

答案 3 :(得分:0)

老实说,这只是猜测,但根据您提供的代码,您实际上需要在删除不需要的代码后添加更多代码:

<?php
$sel_admin = "query  ";
$rs_admin = mysql_query($sel_admin);
while($row = mysql_fetch_array($rs_admin))
{      
    echo "<tr><th>". $row['a'].  "</th></tr>"; // Notice the <tr></tr>

    $sel_admin2 = "query2  ";
    $rs_admin2 = mysql_query($sel_admin2);
    while($row2 = mysql_fetch_array($rs_admin2))
    {
        echo" <tr class='gradeX'> ";
        echo "<td>" . $row2['sched3_time'].  "</td>";
        echo"</tr>";
    }
}
?>