带有while循环的php echo表

带有while循环的php echo表,php,mysql,while-loop,html-table,Php,Mysql,While Loop,Html Table,我正在创建一个表,希望它以某种方式布局,并且必须从数据库中检索数据。我正在尝试将它设置为在顶部示例中有用户名的位置 Jim Chris Allen Rick 7 8 4 5 吉姆·克里斯·艾伦·里克 7 8 4 5 我的代码看起来像这样,我已经花了好几个小时来处理它,不明白为什么我不能按照我想要的方式设置它。任何帮助都将不胜感激。这是一个循环 while ($pickresu

我正在创建一个表,希望它以某种方式布局,并且必须从数据库中检索数据。我正在尝试将它设置为在顶部示例中有用户名的位置

Jim Chris Allen Rick 7 8 4 5 吉姆·克里斯·艾伦·里克 7 8 4 5 我的代码看起来像这样,我已经花了好几个小时来处理它,不明白为什么我不能按照我想要的方式设置它。任何帮助都将不胜感激。这是一个循环

   while ($pickresults= mysql_fetch_assoc($picksquery)) {
//first row                         
    echo '<th> '.$pickresults['username'].' </th> ';  
        echo ' <td> '.$pickresults['firstgame'].' </td> '; } 
while($pickresults=mysql\u fetch\u assoc($picksquery)){
//第一排
回显'.$pickresults['username'].';
回显“”。$pickresults['firstgame'].';}

首先收集表头和表体,然后输出。你当时的做法是,html是这样的,我想很容易看出html有什么问题

<th>name</th>
<td>value></td>
<th>another name</th>
<td>another value</td>
名称
价值>
另一个名字
另一个价值
您需要的是:

$td = '';
$th = '';
while ($pickresults= mysql_fetch_assoc($picksquery)) {                      
    $th .= '<th> '.$pickresults['username'].' </th> ';  
    $td .= '<td> '.$pickresults['firstgame'].' </td> ';
}
echo '<table><tr>' . $th . '</tr><tr>' . $td . '</tr>' . '</table>';
$td='';
$th='';
而($pickresults=mysql\u fetch\u assoc($picksquery)){
$th.=''.$pickresults['username'].';
$td.=''.$pickresults['firstgame'].';
}
回显“”$"$运输署""";

您的结构是先创建TH,然后创建TD,然后创建TH,然后创建TD等

这不是创建表的方式,首先需要创建四个TH,然后才能创建四个TD


编辑:Marko D提供了代码来解释我的意思。

首先,您应该学习表格的HTML代码。您的代码将一个表头(
th
)放在一个普通列项(
td
)旁边。您需要先循环遍历标题,然后再循环下一行遍历列项目,或者将字符串构建为
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>";
$headers=$col=”“;
while($pickresults=mysql\u fetch\u assoc($picksquery)){
$headers.=“{$pickresults['username']}”;
$col.=“{$pickresults['firstgame']}”;
}
回显“$headers$col”;

您需要在
-标记中写入所有用户名。 我先把它放在一个数组中,然后从数组中放入表中

while ($pickresults= mysql_fetch_assoc($picksquery)) {

    $picked[]=$pickresults;

}

echo "<table><tr>"; // create table and 1st row

foreach ($picked as $pick) {

     echo "<th>{$pick['username']}</th>"; // create 1st line headers

}

echo "</tr><tr>"; // close 1st row and open 2nd

foreach ($picked as $pick) {

    echo "<td>{$pick['firstgame']}</td>"; // create 2nd line...

}

echo "</tr></table>"; // close 2nd row and table
while($pickresults=mysql\u fetch\u assoc($picksquery)){
$picked[]=$pickresults;
}
回声“;//创建表和第一行
foreach($pick作为$pick选择){
echo“{$pick['username']}”;//创建第一行标题
}
回声“;//关闭第一排,打开第二排
foreach($pick作为$pick选择){
echo“{$pick['firstgame']}”//创建第二行。。。
}
回声“;//关闭第二行和表

别忘了在每一行的周围加上
,否则这将是无效的
。非常感谢。这很有帮助。将循环保存到一个数组就是金钱。