显示基于相同ID的记录但删除sql上的相似性

问题描述 投票:3回答:1

也许主题是重复的或与其他人有相似之处,但我还没有找到我正在寻找的东西。

我在sql上有一个Sales Order表,我正在使用odbc连接

id_sales_order date customer product qty 001 2017-12-12 ABC laptop 1 001 2017-12-12 ABC printer 1 001 2017-12-12 ABC mouse 2 002 2017-12-15 Hercules pc 1 002 2017-12-15 Hercules hdd 1

我有一个代码

<body>
<?php
$konek = odbc_connect("otosurabaya","mike","mike") or die("Error Connect to Database");
$sql = "SELECT id_sales_order, customer, product, date,qty FROM Kreasi_Cabang.dbo.QNota_SalesOrder where sales='" .$_SESSION['username']. "' group by id_sales_order, customer, product, date, qty" ;

$hasil = odbc_exec($konek, $sql) or die ("Error Execute [".$sql."]");
$noUrut = 1;
?>
<?php

while($result = odbc_fetch_array($hasil))
{
?>

<div class="panel panel-success">
<div class="panel-heading" >
<h3 class="panel-title" align="center"><strong> <?php echo $_SESSION['username']; ?> </strong>  </h3> 
 </div>
 <div class="panel-body">

<table width="100%" border="1">
<tr>
<th width="131" scope="row">Customer</th>
<td width="44">:</td>
<td width="800" align="left"><?php echo $result['customer'];?></td>
</tr>
<tr>
<th scope="row">SO ID</th>
<td>:</td>
<td align="left"><?php echo $result['id_sales_order'];?></td>
</tr>
<tr>
<th scope="row">Date
<td align="left"><?php echo $result['date'];?></td>
</tr>
<tr>
<th height="49" scope="row">Product</th>
<td>&nbsp;</td>
<td align="left">QTY</td>
</tr>
<tr>
<th scope="row"><?php echo $result['product'];?></th>
<td>&nbsp;</td>
<td align="left"><?php echo $result['qty'];?></td>
</tr>

<?php   }?>

</table>


</p>


</div>
<hr />
<?php
odbc_close($konek);
?>
</body>

上面的代码,它每行只显示记录,我想删除相同的值并保留其他值。

我有搜索和尝试很多语法,请你需要你的专业知识

我想显示如下记录

Customer : ABC 
Date     : 2017-12-12
ID SO    : 001
Product    QTY
Laptop      1
Printer     1
Mouse       1
php sql odbc
1个回答
2
投票

您需要将if条件放在while循环中。并且需要放置标志,以便您的客户名称和其他详细信息应该打印一次且唯一。

产品详细信息将多次打印。这是代码:

<?php

$customer_name = '';
$sale_date = '';
$sales_id = '';
$flag = true;
?>

<div class="panel panel-success">
<div class="panel-heading" >
<h3 class="panel-title" align="center"><strong> <?php echo $_SESSION['username']; ?> </strong>  </h3> 
 </div>
 <div class="panel-body">

<table width="100%" border="1">
<?php
while($result = odbc_fetch_array($hasil))
{
?>

<?php if($customer_name != $result['customer'] && $sale_date !=  $result['date'] && $sales_id != $result['id_sales_order']) { ?>
    <tr>
        <th width="131" scope="row">Customer</th>
        <td width="44">:</td>
        <td width="800" align="left"><?php echo $result['customer'];?></td>
    </tr>   
    <tr>
        <th scope="row">SO ID</th>
        <td>:</td>
        <td align="left"><?php echo $result['id_sales_order'];?></td>
    </tr>
    <tr>
        <th scope="row">Date
        <td align="left"><?php echo $result['date'];?></td>
    </tr>

<?php 
    $customer_name = $result['customer'];
    $sale_date =  $result['date'];
    $sales_id = $result['id_sales_order'];
    $flag = true;
} else { $flag = false; } 
    if($flag) {
?>  
    <tr>
        <th height="49" scope="row">Product</th>
        <td>&nbsp;</td>
        <td align="left">QTY</td>
    </tr>
    <?php } ?>
    <tr>
        <th scope="row"><?php echo $result['product'];?></th>
        <td>&nbsp;</td>
        <td align="left"><?php echo $result['qty'];?></td>
    </tr>

<?php   } ?>

如果要在任何细节不匹配的情况下打印客户详细信息,您可以使用&&更改if条件||。就像日期不同或者销售ID不同。

注意:我根据您显示的数据设置了if条件。你可以根据你的要求改变它。

© www.soinside.com 2019 - 2024. All rights reserved.