Jump to content

[SOLVED] Displaying results of a query in two columns


jakeoh

Recommended Posts

Hi,

 

I have a section on my page where I want to display the avatar and name of users currently logged on. The following code works perfect, but I want to format it a certain way and do not know how; in this section I would like to display each entry (avatar + username) in a 2 column table:

 

User #1 User #2
User #3 User #4

 

Here's the code I have right now, which will display the users one after the other in a single column table:

elseif($num_rows > 0){
   /* Display active users, with link to their info */
   echo "<table width=\"213\" align=\"left\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n";
   echo "<tr><td><font size=\"2\">\n";
   for($i=0; $i<$num_rows; $i++){
     $uname = mysql_result($result,$i,"username");
 $avatarQuery = "SELECT avatarID, last_name, first_name FROM ".TBL_USERS." WHERE username = '$uname'";
 $resultAvatar = $database->query($avatarQuery);
 $uAvatar = mysql_result($resultAvatar, 0);
 $first_name = mysql_result($resultAvatar, 0, "first_name");
 $last_name = mysql_result($resultAvatar, 0, "last_name");
 $initial = $last_name[0];
echo '<img src="get_avatar.php?pid='.$uAvatar.'"width="60" height="60">';
echo "<br>";
    echo "<a href=\"userinfo.php?user=$uname\">$first_name $initial</a>";
echo "<br>";
echo "<br>";
   }
   echo "</font></td></tr></table><br>\n";
}

 

How can I format the info in two columns?

Here is another way to do it.. This is a Karaoke Song List that is displayed in 2 colums.

 

<?php
$result = mysql_query("SELECT * FROM tracks ORDER BY artist,track LIMIT $offset, $setlimit "); 
while($row = mysql_fetch_object($result))
{ 
    $track[]='$row->artist .' -- '. $row->track; 
// I set each result in an array called $track[]

}; 
?>

<table width="90%" align="center" cellpadding="5" cellspacing="0">
<tr><td colspan="2" align="right" class="smaller">Showing Page <?=$page ?> of   <?=$pages ?></td>
</tr>
<?
$x=0;
$row_count = "0";  
while($x < count($track))
{ 
    $row_color = ($row_count % 2) ? $color1 : $color2 ; 
?>
<tr bgcolor="<?=$row_color ?>" align="left" valign="middle">

<td height="20" >
<?=$track[$x++] ?><!-- First Column -->
</td>
<td height="20" ><?=$track[$x++] ?><!--Second Column --></td>
</tr>
<?
$row_count++; 
}
?>
</table>

 

To me this is easier, Loop through the results and turn it into an array with all the results, then do another loop to display it in columns.

 

Nate

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.