防止出现相同ID的数据

时间:2014-11-14 01:51:30

标签: php

我想阻止amaun_caj,amaun_pelbagai,amaun_penalti,amaun_tunggakan被重复,所以如果id_akaun是相同的,数据不会出现,我的代码只适用于amaun_caj,其余的,没有单个数据出现,什么&# 39;问题是什么?

<?php 

$i = 0; $id_akaun_old ="";
while($output = mysql_fetch_array($result)) {
$i++;
$id_akaun           =   $output["id_akaun"];
$lokasi             =   $output["lokasi"];
$amaun_caj          =   $output["amaun_caj"];
$amaun_tunggakan    =   $output["amaun_tunggakan"];
$amaun_penalti      =   $output["amaun_penalti"];
$amaun_pelbagai     =   $output["amaun_pelbagai"];
$jumlah_bayaran     =   $output["jumlah_bayaran"];



?>



<tr>

<td>
<?php echo $i; ?>
</td>

<td>
<?php echo $jenis; ?>
</td>

<td>
<?php echo $id_akaun;
 ?>
</td>

<td>
<?php echo $no_telefon; ?>
</td>

<td>
<?php echo $lokasi; ?>
</td>

<td align="center">
<?php 
if($id_akaun != $id_akaun_old):
    echo $amaun_caj; 
    $id_akaun_old = $id_akaun;
else: echo ''; 
endif;?>
</td>

<td align="center">
<?php 
if($id_akaun != $id_akaun_old):
    echo $amaun_pelbagai; 
    $id_akaun_old = $id_akaun;
else: echo ''; 
endif;?>
</td>

<td align="center">
<?php 
if($id_akaun != $id_akaun_old):
    echo $amaun_penalti; 
    $id_akaun_old = $id_akaun;
else: echo ''; 
endif;?>
</td>

<td align="center">
<?php 
if($id_akaun != $id_akaun_old):
    echo $amaun_tunggakan; 
    $id_akaun_old = $id_akaun;
else: echo ''; 
endif;?>
</td>


<td align="center">
<?php 
if($id_akaun != $id_akaun_old):
    echo $jumlah_bayaran; 
    $id_akaun_old = $id_akaun;
else: echo ''; 
endif;?>
</td>



<?php
}
?>

2 个答案:

答案 0 :(得分:1)

使用临时变量$ old_id_akaun可能不是最佳做法。如果您仍然希望这样做,我建议您在SQL语法中使用ORDER BY id_akaun。

在我看来,你可能会摆脱临时变量并按照这些步骤进行操作,

 1. Create empty array outside your while loop. For the sake of easy 
 understanding, let's called it $list_id_akaun.
 2. Inside your while loop, after you get $id_akaun, check whether $id_akaun 
 is inside $list_id_akaun
 3. If not exists, insert it to $list_id_akaun and continue echoing your 
 table row
 4. If exists, skip to the next row.

答案 1 :(得分:0)

我不知道为什么你不想在这种情况下使用select distinct子句,但只需将它们放入容器中然后检查循环内部:

<?php 
$temp = array();
$i = 1;
while($output = mysql_fetch_array($result)): 
    $id_akaun           =   $output["id_akaun"];
    $lokasi             =   $output["lokasi"];
    $amaun_caj          =   $output["amaun_caj"];
    $amaun_tunggakan    =   $output["amaun_tunggakan"];
    $amaun_penalti      =   $output["amaun_penalti"];
    $amaun_pelbagai     =   $output["amaun_pelbagai"];
    $jumlah_bayaran     =   $output["jumlah_bayaran"];
?>
<tr>
    <td><?php echo $i; $i++; ?></td>
    <td><?php echo $jenis; ?></td>
    <td><?php echo $id_akaun; ?></td>
    <td><?php echo $no_telefon; ?></td>
    <td><?php echo $lokasi; ?></td>
    <?php if(!in_array($id_akaun, $temp)): ?>
        <td><?php echo $amaun_penalti; ?></td>
        <td><?php echo $amaun_tunggakan; ?></td>
        <td><?php echo $jumlah_bayaran; ?></td>
        <?php $temp[] = $id_akaun; ?>
    <?php else : ?>
        <td></td><td></td><td></td>
    <?php endif; ?>
</tr>
<?php endhile; ?>