ตัวอย่างโค้ด php ต่อไปนี้เป็นแนวทางในการเลือกไม่แสดงข้อมูลซ้ำ กรณีตาราง
ข้อมูลมีการเรียงข้อมูลแบบจัดกลุ่ม
ตัวอย่างตารางข้อมูลที่มีการแรียงข้อมูล
| Top | Sub |
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
สิ่งที่ต้องการ
| Top | Sub |
| 1 | 1 |
| 2 | |
| 3 | |
| 2 | 1 |
| 2 | |
| 3 | |
| 3 | 1 |
| 2 | |
| 3 |
โค้ดตัวอย่าง
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
</head>
<body>
<div style="margin:auto;width:500px;">
<table class="table table-condensed table-hover" width="300">
<tr>
<td align="center">Top</td>
<td align="center">Sub</td>
</tr>
<?php
for($i=1;$i<=3;$i++){
$temp_data1=null;
$temp_data2=null;
$data_show=1; // 1 แสดง 0 ไม่แสดง
for($v=1;$v<=3;$v++){
$temp_data1=$i;
if($temp_data2==null){
$temp_data2=$temp_data1;
$data_show=1;
}else{
if($temp_data1==$temp_data2){
$data_show=0;
$temp_data2=$temp_data1;
}
}
?>
<tr>
<td align="center">
<?php if($data_show==1){?>
<?=$i?>
<?php } ?>
</td>
<td align="center"><?=$v?></td>
</tr>
<?php } } ?>
</table>
</div>
</body>
</html>
ตัวอย่างการประยุกต์กับฐานข้อมูล
<table width="300" border="0" cellspacing="00" cellpadding="0">
<tr>
<td align="center">Top</td>
<td align="center">Sub</td>
</tr>
<?php
.... ...
... ..
$temp_data1=null;
$temp_data2=null;
$data_show=1; // 1 แสดง 0 ไม่แสดง
while($rs=mysql_fetch_array($qr)){
$temp_data1=$rs['ฟิลด์เทียบ'];
if($temp_data2==null){
$temp_data2=$temp_data1;
$data_show=1;
}else{
if($temp_data1==$temp_data2){
$data_show=0;
$temp_data2=$temp_data1;
}else{
$temp_data2=$temp_data1;
$data_show=1;
}
}
?>
<tr>
<td align="center">
<?php if($data_show==1){?>
<?=$rs['ฟิลด์เทียบ']?>
<?php } ?>
</td>
<td align="center"><?=$v?></td>
</tr>
<?php } ?>
</table>