Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php echo table with while loop

I am creating a table and want it laid out a certain way and have to retrieve the data from the DB. I am trying to get it set up where it has the usernames across the top example...

 Jim     Chris     Allen    Rick
 7       8          4         5

my code looks like this and I have been messing around with it for hours and cant figure out why I cant get it set up how I want. Any help would be appreciated. Its a while loop.

   while ($pickresults= mysql_fetch_assoc($picksquery)) {
//first row                         
    echo '<th> '.$pickresults['username'].' </th> ';  
        echo ' <td> '.$pickresults['firstgame'].' </td> '; } 
like image 915
Ricky Avatar asked Aug 24 '26 14:08

Ricky


1 Answers

First off, you should learn the HTML code for tables. Your code is putting a Table Header (th) next to a normal column item (td). You need to loop through the headers first then next row loop through the column items or build the strings to echo out.

$headers = $col = "";
while($pickresults= mysql_fetch_assoc($picksquery)){
    $headers .= "<th> {$pickresults['username']} </th>";
    $col .= "<td> {$pickresults['firstgame']} </td>";
}

echo "<table><tr>$headers</tr><tr>$col</tr></table>";
like image 173
UnholyRanger Avatar answered Aug 26 '26 05:08

UnholyRanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!