搜索建议将搜索词放在建议的前面

时间:2017-11-23 12:54:21

标签: ajax wordpress search

目前,我正在开展一项我继承的项目"来自其他人并且无法解决某些问题。

就在我认为我完成这个项目的时候,我发现,如果我搜索例子" apple"并且搜索的建议是" foo apple bar",它会扔掉" apple foo bar"

以下是代码:

function select_data()
{
global $wpdb;
$product_name=$_POST['product_name'];

if(!empty($product_name))
{
$unique_id = 'prodct_search';
$query ="SELECT * FROM `wp_posts` WHERE `post_title` LIKE '%".$product_name."%' AND `post_type` = 'product'";

$locar = $wpdb->get_results($query);

if($locar)
{
echo '<ul>';
foreach($locar as $loca)
{
echo '<li id="'.$loca->ID.'" onClick="filladsearch_loc('.this.',\''.addslashes($loca->post_title).'\',\''.$unique_id.'\')">'.$loca->post_title.'</li>';
}
echo '</ul>';
}
else
{
  echo '<ul><li>No products were found matching your search.</li></ul>';
}
}
die();
}

function select_data1()
{
global $wpdb;
$product_name=$_POST['product_name'];

if(!empty($product_name))
{
$unique_id = 'prodct_search';
$query ="SELECT * FROM `wp_posts` WHERE `post_title` LIKE '%".$product_name."%' AND `post_type` = 'product'";

$locar = $wpdb->get_results($query);

if($locar)
{
echo '<ul>';
foreach($locar as $loca)
{
    $titcapital=strtoupper($loca->post_title);
    $proupper=strtoupper($product_name);
    $stringsearched=str_replace($proupper,"",$titcapital);
    //$createdarr=explode($product_name,$loca->post_title);
    $finalstring='<span class="pro_top">'.strtoupper($product_name).'</span>'.strtoupper($stringsearched);
    //print_r($createdarr);

echo '<li id="'.$loca->ID.'" onClick="filladsearch_loc('.this.',\''.addslashes($titcapital).'\',\''.$unique_id.'\')">'.$finalstring.'</li>';
}
echo '</ul>';
}
else
{
  echo '<ul><li>No products were found matching your search.</li></ul>';
}
}
die();
}

这就是它的样子:

enter image description here

0 个答案:

没有答案
相关问题