php阻止特定的重复行

I have a sample PHP code

  <?php 
   for ($x=0; $x<=3; $x++) {
    echo '<tr><td>Column $x</td>';
    $result = mysql_query(" SELECT id, `Nickname`,`hours` FROM `employee` ");

    echo '<table border=1>';
    while ($row = mysql_fetch_array($result)) {
      echo '<tr>';
      echo "<td>Nickname:".$row{'Nickname'}."</td><td>Hours: ".$row{'hours'}."</td>";
      echo '</tr>';
    }
    echo '</table>';    
    echo '</tr>';
   } 
?>

and the result is something like this.

                Column 1                    Column 2                   Column 3   
Nickname: John  Hours: 1  |  Nickname: John Hours:  1 | Nickname: John Hours: 1
Nickname: Foo   Hours: 2  |  Nickname: Foo  Hours:  2 | Nickname: Foo  Hours: 2 

I need to eliminate specific repeating rows. I dont need to repeat the Nickname Row.

the output should be like this Column 1 Column 2 Column 3 Nickname: John Hours: 1 | Hours: 1 | Hours: 1 Nickname: Foo Hours: 2 | Hours: 2 | Hours: 2

My suggestion is that you solve this easily in PHP.

<?php 
for ($x=0; $x<=3; $x++) {

$result = mysql_query(" SELECT id, `Nickname`,`hours` FROM `employee` ORDER BY `Nickname`");

echo '<table border=1>';
$nick = "";
while ($row = mysql_fetch_array($result)) {
  if($nick != $row{'Nickname'} {
       if($nick != "") { echo '</tr>'; }
       echo '<tr>';
       echo "<td>Nickname:".$row{'Nickname'}."</td>";
  }
  echo "<td>Hours: ".$row{'hours'}."</td>";
  $nick = $row{'Nickname'};
}
echo '</table>'; 
?>

In order for this to work as intended, you have to have all nicknames next to eachother, which you easily do with ORDER BY Nickname.

You should remove the For loop part. Plus your code should be something like this:

<?php 

    $result = mysql_query("SELECT id, `Nickname`,`hours` FROM `employee`");

    echo '<table border=1>';
    while ($row = mysql_fetch_array($result)) {
      echo '<tr>';
      echo "<td>Nickname:".$row{'Nickname'}."</td><td>Hours: ".$row{'hours'}."</td>";
      echo '</tr>';
    }
    echo '</table>'; 
  ?>

Try it out ...

Try this

    $result = mysql_query(" SELECT id, `Nickname`,`hours` FROM `employee` ");

    echo '<table border=1>';
    while ($row = mysql_fetch_array($result)) {
      echo '<tr>';
      echo "<td>Nickname:".$row['Nickname']."</td><td>Hours: ".$row['hours']."</td>";
      echo '</tr>';
    }
    echo '</table>'; 
  ?>

simply remove the for loop

Just change your sql query

SELECT id, `Nickname`,`hours` FROM `employee`

to

SELECT max(id) as id, `Nickname`, sum(`hours`) as hours FROM `employee` GROUP BY `Nickname`

Also, pay attention that mysql_query (http://php.net//manual/en/function.mysql-query.php) function is deprecated and you should use mysqli_query:

http://php.net/manual/en/mysqli.query.php

i think order by and lastName variable can solve better

<?php 
for ($x=0; $x<=3; $x++) {

$result = mysql_query(" SELECT id, `Nickname`,`hours` FROM `employee` order by Nickname");

echo '<table border=1>';
$lastName='';
while ($row = mysql_fetch_array($result)) {
  echo '<tr>';
  if($row['Nickname']==$lastName)
  {
      echo "<td></td>";
  }
  else
  {
      echo "<td>Nickname:".$row{'Nickname'}."</td>";
      $lastName=$row['Nickname'];
  }
  echo "<td>Hours: ".$row{'hours'}."</td>";
  echo '</tr>';
}
echo '</table>'; 
?>