开发者

PHP: working with tr and td with while()

<table border="0" cellspacing="0" cel开发者_StackOverflowlpadding="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>

This is what i have right now it displays the visiter´s profileimage. Now i would like to have their name under the image too.. but then i need to create another <tr> after this one, and add their name in each <td>. But how can i do that, if i have this while? Should i run another while, to get the names or can i do something smart with this?


Why not just stick the name in the same cell as the image? After you close the image tag, just echo $getByProfile["username"], or whatever?

<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>


Here's a way to do it with minimal changes to your existing code:

<?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>

Also note that the way you had your code, the </tr> was inside the while loop.


this might not look nice but it should work why do you want 2 loops?

<?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>";
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜