如何在 php 中使用自动完成功能在标签中显示两个数据库值中的任何一个

how to display either of the two database values in a label using autocomplete in php

这是我的以下代码:

<?php include("config.php");
include("session-user.php");

$term = trim(strip_tags($_GET['term']));//retrieve the search term that    autocomplete sends

$qstring = "SELECT product_title, product_details, product_id  FROM tbl_product WHERE product_title LIKE '".$term."%' OR product_details LIKE '".$term."' and is_delete = '0'";
$result = mysqli_query($con,$qstring);//query the database for entries containing the term

while ($row = mysqli_fetch_array($result))//loop through the retrieved values
{
    $row['label']=htmlentities($row['product_title']);
    $row['label']=htmlentities($row['product_details']);
    $row['product_id']=htmlentities($row['product_id']);
    $row_set[] = $row;//build an array
}
echo json_encode($row_set);

目前只显示其中一个labels,我想用这两个标签中的一个搜索

如何实现?

问题出在这段代码中:-

 $row['label']=htmlentities($row['product_title']);
 $row['label']=htmlentities($row['product_details']);

由于相同的索引(label)它覆盖了第一个,像这样使用:-

 $row['label']=htmlentities($row['product_title']);
 $row['label1']=htmlentities($row['product_details']); 

检查我已经将第二个索引更改为 label1

自动完成每个值一个标签,因此您可以连接它们 (label-label1) 并显示。

这可能对您有帮助:-

while ($row = mysqli_fetch_array($result))//loop through the retrieved values
{
    if($row['product_title'] !=='' && $row['product_details'] ==''){ // if product_title is present but  product_details is not present
      $row['label']=htmlentities($row['product_title']);
      $row['product_id']=htmlentities($row['product_id']);
      $row_set[] = $row;//build an array
    }else if($row['product_details'] !=='' && $row['product_title'] ==''){ // if product_details is present but  product_title is not present
      $row['label']=htmlentities($row['product_details']);
      $row['product_id']=htmlentities($row['product_id']);
      $row_set[] = $row;//build an array
    }else if($row['product_details'] !=='' && $row['product_title'] !==''){ // if both are present
      $row['label']=htmlentities($row['product_title']);
      $row['product_id']=htmlentities($row['product_id']);
      $row_set[] = $row;//build an array
    } 
}

注意:- 在上面的代码中,如果两个值(product_titleproduct_title)都存在,则 product_title 将显示为标签,否则相应的值将显示为标签。

试试这个

    $row['label1']=htmlentities($row['product_title']);
    $row['label2']=htmlentities($row['product_details']);

并将查询更改为

$qstring = "SELECT product_title, product_details, product_id  FROM tbl_product WHERE product_title LIKE '".$term."%' OR product_details LIKE '".$term."%' and is_delete = '0'";