查询格式帮助 (PHP, MySQL) 遗留代码

Query formatting help (PHP, MySQL) Legacy code

此代码用于显示内容列表...

$result= mysql_query("
SELECT DISTINCT 
m.username, m.asdf_txt, m.week_txt,  
   FROM table m  
      WHERE m.username='$user' ORDER BY m.week_txt ASC") 
or die(mysql_error($link));

while($row = mysql_fetch_array($result)) { 
    echo "<br>" . $row['week_txt'] . ""; 
    echo "<br>" . $row['asdf_txt'] . ""; 
} 
echo "<br>"; echo "";

它最终显示

第 1 周
asdfasdf1

第 1 周
asdfasdf2

第 2 周
asdfasdf3

第 2 周
asdfasdf4

但我想改为...

第 1 周
asdfasdf1

asdfasdf2

第 2 周
asdfasdf3

asdfasdf4

任何建议都会有所帮助。我正在寻找一种将写入代码(php/mysql 文件)本身的解决方案。请记住,我之前已经使用以下代码以更迂回的方式解决了这个问题...

sed 's/<br>Week 1\b//2g; s/<br>Week 2//2g;'

试试这个

$prev = "";
while($row = mysql_fetch_array($result)) {
    if (strcmp($row['week_text'],$prev)){
       echo "<br>" . $row['asdf_txt'] . "";
    } else {
       echo "<br>" . $row['week_txt'] . ""; 
       echo "<br>" . $row['asdf_txt'] . "";
    }
    $prev = $row['week_text']; 
}