分组(备注)php mysql中的值

时间:2016-05-13 08:34:18

标签: php html mysql group-by html-table

以下是产品表格:

id | mid | wgh | remark| remkok | 
1    3     1.5    r3ok      1
2    2     1.5              0 
3    2     0.6    nice      0 
4    1     1.2    okh       0
5    4     1.5    bye       0
6    4     2.4    okby      0
7    3     3.0    oknice    1

我希望在下面的 tr 下面显示评论。比如下面

    mid   wgh  
    3     1.5   
          3.0 
  remarks : r3ok, oknice
    4     1.5
          2.4
  remarks : bye, okby
    2     1.5
          0.6
  remarks :  , nice
    1     1.2
  remarks : okh

我尝试过以下内容:

$pid= null;
while($row = mysql_fetch_array($result))
{

 $rowpkts =  $row['mid'];
 echo "<tr class=\"undercl\">";
   if($rowpkts != $pid){
         echo'<td align="center" valign="top">'.$row["mid"].'</td>';
  }else{
        echo'<td align="center" valign="top"></td>';
   }

   echo'<td align="center" valign="top">'.$row["wgh"].'</td>';

  echo "</tr>";

// what i tried to build for remarks as below

$remsql = "SELECT mid as onu , GROUP_CONCAT(`remark` ORDER BY `id` ASC  SEPARATOR ', ') AS plrmks 
FROM products  WHERE 1=1 GROUP BY `mid`";
$fetchremk = mysql_query($remsql);
$rowresults =  mysql_fetch_array($fetchremk);

 if($rowresults['onu'] == $pid ){   

  echo"<tr style='border-style:underline;'>";
       echo'<td align="center" align="top">'.$rowresults["plrmks"].'</td>';               
           echo"</tr>";
        }
  }
  $pid = $rowpkts;
} 

但是 tr 下面的评论不合适......这意味着它不会显示在mid = 3或mid = 1之下。

任何其他方式,这对我有帮助。

1 个答案:

答案 0 :(得分:0)

根据需要添加适当的colspan,不要进行居中对齐。

尝试以下代码

while($row = mysql_fetch_array($result))
{

    $rowpkts =  $row['mid'];
    echo "<tr class=\"undercl\">";
    if($rowpkts != $pid){
         echo'<td align="center" valign="top">'.$row["mid"].'</td>';
    }else{
        echo'<td align="center" valign="top"></td>';
    }
    echo'<td align="center" valign="top">'.$row["wgh"].'</td>';

    echo '</tr>';
    $remsql = "SELECT mid as onu , GROUP_CONCAT(`remark` ORDER BY `id` ASC  SEPARATOR ', ') AS plrmks 
    FROM products  WHERE `remkok`= 1 GROUP BY  `mid`";
    $fetchremk = mysql_query($remsql);
    $rowresults =  mysql_fetch_array($fetchremk);

    if($rowresults['onu'] ==  $pid ){   

       echo"<tr><td colspan ='?'> Remarks : ";
       echo $rowresults["plrmks"];               
       echo "</td></tr>";
     }
    $pid = $rowpkts;
}