duanchi3109 2019-01-08 07:22
浏览 43
已采纳

如何通过仅从表中获取唯一数据来仅在文本字段中显示div的数据?

I'm trying to display some data on a div with 3 fields by getting only the unique values from the table. The query used is working fine in pgAdmin3, but when trying to display it on the page, multiple divs are getting displayed based on an data scanned in the previous page.

Since it is an array, I'm using the foreach to display and that is the reason multiple divs are getting generated, and I don't know how to display data from an array without using loop.

Getting data scanned in the previous page

$strings = explode(PHP_EOL,trim($_SESSION['grid']));

Displaying data in the divs using foreach based on the number barcodes scanned in the previous page

foreach ($strings as $d){ 
$sql = "select distinct size from items where main_group IN(select distinct 
main_group from items where addl_item_code_barcode IN ('$d')) ORDER BY 
size";
$result = pg_query($db, $sql);
$res = pg_fetch_assoc($result);
?> 
<div class="col-sm show-hide">
<input type="text" value="<?php echo $res['size']; ?> " readonly 
style="background-color: #F5F5F5;" class="form-control"><br> 
<input class="form-control" type="text" name="n2" id="n2"/><br>
<select class="form-control">
<option>25%</option>
<option>50%</option>
<option>100%</option>
</select><br>
</div>
<?php }?>

I scanned 2 barcodes in the previous page which has the same value '36' in the field 'size'. Since 2 data was scanned, the loop ran twice and got displayed twice. How do I display it only once.

Here's how the page looks like now. https://ibb.co/qDh425j

  • 写回答

1条回答 默认 最新

  • doudouchan5830 2019-01-08 08:12
    关注

    Each barcode is linked to an item which has size '36'.

    What you need to check is whether there is a data entry error. if each barcode is supposed to uniquely match to certain size, then you need to fix the data in your db.

    Otherwise, you need to decide what to do with barcodes which are mapping to an item of the same size.

    Lastly you can probably combine the query so you don't need the loop for each barcode something like this

    $sa = [];
    foreach ($strings as $s) {
        //add single quotes
        array_push($sa, "'".$s."'");
    }   
    //recreate string with coma
    $d=implode(',', $sa);
    
    $sql = "select distinct size from items where main_group IN(select distinct 
    main_group from items where addl_item_code_barcode IN ('$d')) ORDER BY 
    size";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测