-1

This code is used to show a list of stuff...

$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 "";

It ends up showing

Week 1
asdfasdf1

Week 1
asdfasdf2

Week 2
asdfasdf3

Week 2
asdfasdf4

But I would like to have instead...

Week 1
asdfasdf1

asdfasdf2

Week 2
asdfasdf3

asdfasdf4

Any suggestions would be helpful. I'm looking for a solution that will be written into the code (a php/mysql file) itself. Bear in mind I've solved this problem with the following code in a more roundabout way previously...

sed 's/<br>Week 1\b//2g; s/<br>Week 2//2g;'
Mike
  • 3
  • 2
  • **Warning:** `mysql_*` extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0. Instead, either the [mysqli](https://www.php.net/manual/en/book.mysqli.php) or [PDO_MySQL](https://www.php.net/manual/en/book.pdo.php) extension should be used. See also the [MySQL API Overview](https://www.php.net/manual/en/mysqlinfo.api.choosing.php) for further help while choosing a MySQL API. – Dharman May 24 '21 at 09:55

1 Answers1

1

Try This

$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']; 
}
Mohanasundar
  • 157
  • 1
  • 3
  • 13