Joe Bills Joe Bills - 6 months ago 9
SQL Question

How to make usernames display in rows and columns in a Table

I have this table im trying to display users, being 2 users per 2 columns, then list down. Here is what i have so far:

<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'");
while($row = mysql_fetch_array($result)) { echo
" <table>
<tr>
<td width='85' align='left'><br><center>". $row['username'] . "</center>
</td>
<td align='right'><center></center>
</td>
</tr>
<td width='85' align='left'><center></center>
</td>
<td align='right'><center></center>
</td>

</table>";


} ?>


This just displays the members as rows going down, and missing out the other column completely. I was hoping that it would display the username in each of the table fields. I also did try putting
". $row['username'] ."
in the other fields too, but that just duplicated it.

EDIT:
So iv'e changed it to this, I can't see going down as I only have 2 members, Would this work:

<?php $result = mysql_query("SELECT * from users WHERE adminlevel='5'"); ?>

<table>
<tr>
<?php while($row = mysql_fetch_array($result)) { echo
"<td width='85' align='left'><font size='1px'><center>". $row['username'] . "</font></center></td>
<td align='right'><center></center></td>";
} ?>
</tr>
</table>


example:

enter image description here

Jah Jah
Answer

try something like this

        <table>
            <tr>
                <th>name</th>
                <th>name</th>
            </tr>
<?php
$result = mysql_query("SELECT * from users WHERE adminlevel='5'");

$i = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    if ($i == '0') echo "<tr>";
    echo "<td>{$row['username']}</td>";
    if ($i == '1') echo "</tr>";  

    $i++;
    if($i =='2')$i='0';
}
?>
        </table>
Comments