PHP:使用while()处理tr和td

时间:2010-10-31 22:41:17

标签: php

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php 
while ($pF = mysql_fetch_array($stringVisits)) { 
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php 
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";

if (checkStatus($BuID) == 1) {
    echo 'class="onlineBorder" ';
} else {
    echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if (!empty($getByProfile["photo_thumb"])) {
    echo $getByProfile["photo_thumb"];
} else {
    echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>

<?php } ?>
</tr>
</table>

这就是我现在所拥有的显示访问者的个人资料图像。现在我想在图像下面有他们的名字..但是我需要在这个之后创建另一个<tr>,并在每个<td>中添加他们的名字。但是我怎么能这样做,如果我有这个呢?我应该再跑一次,获取名字还是我可以做一些聪明的事情?

3 个答案:

答案 0 :(得分:0)

这可能看起来不太好但它应该有效为什么你想要2个循环?

<?php
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";
while($pF = mysql_fetch_array($stringVisits)){
echo "
<tr>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
   <a href='profil.php?id=".$BuID."'> <img /> </a>
</td>
</tr>
<tr><td> " . $getByProfile['username'] . " </td></tr>";

}
echo "</table>";
?>

答案 1 :(得分:0)

为什么不将名称与图像放在同一个单元格中?关闭图片标签后,只需回显$ getByProfile [“username”],或其他什么?

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php 
while($pF = mysql_fetch_array($stringVisits)){ 
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php 
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";

                          if(checkStatus($BuID) == 1){
                          echo 'class="onlineBorder" ';
                          } else {
                          echo 'class="image-xxsmall-border" ';
                          }
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'><br/>";
?>
</a>
<br/><?php echo $getByProfile['username']; ?>
</td>

<?php } ?>
</tr>
</table>

答案 2 :(得分:0)

这是一种方法,只需对现有代码进行最少的更改即可:

<?php
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
$names = array();  // ADDITION #1
while($pF = mysql_fetch_array($stringVisits)){ 
    $names[] = // ADDITION #2 - ADD NEW USER NAME HERE;
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php 
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";

                          if(checkStatus($BuID) == 1){
                          echo 'class="onlineBorder" ';
                          } else {
                          echo 'class="image-xxsmall-border" ';
                          }
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>

<?php }

// ADDITION #3:
echo "</tr>
<tr>";

foreach ($names as $username)
    echo "<td class=\"username\"><p>$username</p></td>";

?>

</tr>
</table>

另请注意,代码的方式,</tr>位于while循环内。