0

i got a problem, where can i place my </tr> so it shouldnt get duplicated when its in a while?

<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php 
while ($pF = mysql_fetch_array($stringVisits)) 
{ 
    $BuID= mysql_real_escape_string($pF['BuID']);
    $getByInfo = mysql_query("SELECT * FROM users_profile WHERE uID = '$BuID'") or
                die(mysql_error());
    $getByProfile = mysql_fetch_array($getByInfo);
    $getByInf = mysql_query("SELECT full_name, sex FROM users WHERE id = '$BuID'") or
                die(mysql_error());
    $getByProfil = mysql_fetch_array($getByInf);
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php 
    echo "<a href='profil.php?id=".$BuID."'>"; 
    echo "<img style='margin-right: 5px; width: 44px; height: 48px; border: 2px solid #FFF; ' src='images/profilePhoto/thumbs/";
    if (!empty($getByProfile["photo_thumb"])) 
    { 
        echo $getByProfile["photo_thumb"]; 
    } else {
        echo "noPhoto_thumb.jpg";
    }
    echo "'>";
?>
</a>
</td>

I would like to close that <tr> and start a new one, but how can i do that? If i just put in </tr> under the above^ it will duplicate the </tr> because its inside the while() If i do it after closing } the while, i can not use the while()´s $pF anymore in my next tr.

I want to make a new tr for showing the username under the profilephoto above.

Karem
  • 17,615
  • 72
  • 178
  • 278
  • 1
    Could you explain your problem in a clearer way, and get some space in your code ? – Guillaume Lebourgeois Sep 02 '10 at 14:09
  • why the and the are outside the WHILE? and why the is outside the while? – pleasedontbelong Sep 02 '10 at 14:16
  • Slightly off topic, but it looks like you're suffering from the Select N+1 problem: http://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem (You're actually doing 2N+1 select queries by the looks of it, the +1 for $stringVisits, then two ($getByInfo and $getByInf) for each result from $stringVisits) – Douglas Sep 02 '10 at 14:32
  • is that bad? and i dont have any problems with it @Douglas – Karem Sep 02 '10 at 14:36
  • I guess it just means that if you do run into performance problems, you've got something you can easily do to make it faster :-) It's probably going to be fine if your MySQL server and your PHP-enabled web server are all running on the same machine, since the select won't need to go over a network connection. – Douglas Sep 02 '10 at 14:49

3 Answers3

1

You can open and close tr inside of the while, open right when you enter the loop and close right before you exit the loop. See code below, should do the trick

<table border="0" cellspacing="0" cellpadding="0" >
    <tbody>
        <?php
        //  Check result array
        if ($pF) {
            while ($pF = mysql_fetch_array($stringVisits)) {
        ?>
                <tr>        
            <?php
                $BuID = mysql_real_escape_string($pF['BuID']);
                $getByInfo = mysql_query("SELECT * FROM users_profile WHERE uID = '$BuID'") or
                        die(mysql_error());
                $getByProfile = mysql_fetch_array($getByInfo);
                $getByInf = mysql_query("SELECT full_name, sex FROM users WHERE id = '$BuID'") or
                        die(mysql_error());
                $getByProfil = mysql_fetch_array($getByInf);
            ?>

                <td class="viewercell" style="color: #CCC; font-size: 10px;">    
                <?php
                $photo = !empty($getByProfile["photo_thumb"]) ? $getByProfile["photo_thumb"] : "noPhoto_thumb.jpg";

                //
                echo "<a href='profil.php?id=$BuID'>";
                echo "<img style='margin-right: 5px; width: 44px; height: 48px; border: 2px solid #FFF; ' src='images/profilePhoto/thumbs/$photo' />";

                ?>
            </td>
        </tr>
        <?php
            }   // End of While
        } else {
        ?>
            <tr>    
                <td>    
                    No Records!    
                </td>    
            </tr>    
        <?php
        }   // End of check result array
        ?>
    </tbody>
</table>

This is alternative solution. It is not 100% correct, you will need to tweak it to make it a correct. Need to make sure that innter loop always generates needed # of rows. This will create row and [X] amount of cells:

<?php
//  Load data
$data = array();
while ($pF = mysql_fetch_array($stringVisits)) {
    $data[] = $fF;
}

$profilesPerRow = 5;
for ($i = 0; $i < count($data); $i + $profilesPerRow) {
?>
    <tr>                                      
    <?php
    //  Inner Loop, list profiles
    for ($j = $i; $j < $profilesPerRow; $j++) {
        $pF = $data[$i + $j];
        $BuID = mysql_real_escape_string($pF['BuID']);
        $getByInfo = mysql_query("SELECT * FROM users_profile WHERE uID = '$BuID'") or
                die(mysql_error());
        $getByProfile = mysql_fetch_array($getByInfo);
        $getByInf = mysql_query("SELECT full_name, sex FROM users WHERE id = '$BuID'") or
                die(mysql_error());
        $getByProfil = mysql_fetch_array($getByInf);
        $photo = !empty($getByProfile["photo_thumb"]) ? $getByProfile["photo_thumb"] : "noPhoto_thumb.jpg";
    ?>                  

        <td class="viewercell" style="color: #CCC; font-size: 10px;">                                        
        <?php
        echo "<a href='profil.php?id=$BuID'>";
        echo "<img style='margin-right: 5px; width: 44px; height: 48px; border: 2px solid #FFF; ' src='images/profilePhoto/thumbs/$photo' />";
        ?>
    </td>
    <?php
    }   // End of inner loop
    ?>
</tr>
<?php
}   // eND OF OUTER LOOP
?>
Alex
  • 6,441
  • 2
  • 25
  • 26
  • 1
    I think he want to do a photo gallerie of like 5 images / row, so your solution would not work. – Dominique Sep 02 '10 at 14:12
  • Hi thats not how i wanted it. But OK lets continue from your code, what if you would add a new for e.g showing the user´s username under the profilephoto ? – Karem Sep 02 '10 at 14:14
  • guest I misunderstood, better to use floating divs for something like this... CSS power! `
    `, will get much better result
    – Alex Sep 02 '10 at 14:15
0

It's not at all clear what you're trying to do.

If each value of $pF is to result in more than one row, you need to put that inside the while.

egrunin
  • 24,650
  • 8
  • 50
  • 93
-1

Put inside the while :

<?php
while(){
?>
<tr>
     <td>
          <?=info?>
     </td>
</tr>
<?php
}// END While
?>
Stewie
  • 3,103
  • 2
  • 24
  • 22