PHP的回声表与while循环 [英] 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> '; }
推荐答案
首先,您应该学习表的HTML代码.您的代码将表标题(th
)放在普通列项目(td
)旁边.您需要先循环浏览标题,然后再在下一行循环浏览列项目,或构建字符串以echo
输出.
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>";
这篇关于PHP的回声表与while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!