我有以下内容:我得到的结果是附加的图像。我希望用户名只列出一次然后赢得,在页面上松散。每个结果都不是一行。
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ")
or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
$week = $row["win_loose"] ;
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: '.$row_color.';">';
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
echo '<td style="width: 50" align="center"><font size="2">'.$week.'</td>';
答案 0 :(得分:2)
您必须更新代码。您正在查询但是以错误的方式获取数据。你必须尝试关联数组来解决你的问题。请尝试以下代码 -
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY 'username' asc ") or die (mysql_error());
while ($row = mysql_fetch_array($sql_events)) {
$username[$row["username"]][] = $row["win_loose"];
}
foreach($username as $key=>$val){
echo '<td style="width: 100" align="center"><font size="2">'.$username.'</td>';
foreach($val as $value){
echo '<td style="width: 50" align="center"><font size="2">'.$value.'</td>';
}
}
还有一点需要注意,在每个表列上使用内联css并不是一个好习惯。您可以使用一个类并将css获取到附加到该类的css文件。
希望它有帮助...:)
答案 1 :(得分:1)
您已经预先回应了尚未获得数据的周数。这个想法是,每周打印一个单元格,只要它指的是同一个用户:
$sql_events = mysql_query("SELECT * FROM weekpicks ORDER BY username asc ")
or die(mysql_error());
$current_username = null;
while ($row = mysql_fetch_array($sql_events)) {
$username = $row["username"];
//User changed
if ($current_username == null || $current_username != $username) {
if ($current_username != null) {
echo '</tr>'; //Had another user before so end the row
}
$row_color = ($row_count++ % 2 == 0 ? $color1 : $color2);
echo '<tr style="background-color: ' . $row_color . ';">';
echo '<td style="width: 100" align="center"><font size="2">' . $username . '</td>';
$current_username = $username;
}
$week = $row["win_loose"];
echo '<td style="width: 50" align="center"><font size="2">' . $week . '</td>';
}
echo '</tr>';