Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8507981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:01:37+00:00 2026-06-11T03:01:37+00:00

Let me try ask this again with a code snippet with what I have

  • 0

Let me try ask this again with a code snippet with what I have tried

I’m trying to format a Jquery autocomplete to include a heading for each of the data sources and to highlight the term. I’m using Codeigniter and thought maybe the easiest would be to format it before i send it back:

JS:

$( ".auto-search" ).autocomplete({
        source: '/json/autocomplete_search',
        minLength: 2,

    });

PHP (Codeigniter)

public function autocomplete_search()
{
    $term = $this->input->get('term');

    //load model and get results
    $this->load->model("mymodel");
    $results1= $this->mymodel->search_one($term);
    $results2= $this->mymodel->search_two($term);

    //Start JSON string
    $json='[';

    //first source
    if ($result1->num_rows() > 0)
    {
        $json .= '{"value":"<h3>Heading One<h3>"}';
        foreach ($results1->result_array() as $r1)
        {
            $result = str_replace($term,"<strong>".$term."</strong>",$r1['title']);
            $json .= ',{"value":"'.$result.'"}';
        }
    }

    //second source
    if ($result2->num_rows() > 0)
    {
        if ($result1->num_rows() > 0){$json .=  ',';}
        $json .= '{"value":"<h3>Heading Two<h3>"}';

        foreach ($results2->result_array() as $r2)
        {
            $result = str_replace($term,"<strong>".$term."</strong>",$r2['location']);
            $json .= ',{"value":"'.$result.'"}';
        }
    }

    //Close JSON string
    $json .= ']';
    echo  $json;
}`

Unfortunately I’m not getting a formatted output, instead, it actually adds the words < h1> and < strong> to the output. Here is sample output:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:01:39+00:00Added an answer on June 11, 2026 at 3:01 am

    Okay so I’ve found a way to do it. Here is how I did it:

    Javascript:

        $( ".auto-search" ).autocomplete({
            source: '/hotpepper/json/autocomplete_search2',
            minLength: 2,
            open: function(event, ui) {
                $(".ui-autocomplete .ui-menu-item a").each(function (i) {
                    var row = $(this).html();
                    row=row.replace(/&lt;/g,"<");
                    row=row.replace(/&gt;/g,">");
                    $(this).html(row);
                  });   
            },
    
        });
    

    PHP(Codeigniter):

    public function autocomplete_search2()
    {
        $term = $this->input->get('term');
    
        //load model and get results
        $this->load->model("establishment_model");
        $results1= $this->establishment_model->search_autocomplete_est($term);
        $results2= $this->establishment_model->search_autocomplete_loc($term);
    
        //Start JSON string
        $json='[';
    
        //first source
        if ($results1->num_rows() > 0)
        {
            $header= "<h3 style='font-weight:bold'>Accommodation:</h3>";
            $json .= '{"value":"'.$header.'"}';
            foreach ($results1->result_array() as $r1)
            {
                $result = str_replace($term,"<strong style='color:#C00'>".$term."</strong>",$r1['establishment_name']);
                $json .= ',{"value":"'.$result.'"}'; 
            }
        }
    
        //second source
        if ($results2->num_rows() > 0)
        {
            if ($results1->num_rows() > 0){$json .=  ',';}
            $header= "<h3 style='font-weight:bold'>Destinations:</h3>";
            $json .= '{"value":"'.$header.'"}';
    
            foreach ($results2->result_array() as $r2)
            {
                $result = str_replace($term,"<strong style='color:#C00'>".$term."</strong>",$r2['establishment_location']);
                $json .= ',{"value":"'.$result.'"}';
            }
        }
    
        //Close JSON string
        $json .= ']';
        echo  $json;
    }
    

    Since auto-complete escapes my html that I send through, I just unescape it by replacing &lt; and &gt; with <> when i open the auto complete box.

    EDIT:
    Also had to add the following event to format the result back:

    close: function(event, ui) {
                    var result = $(this).val();
                    if (result.search("</h3>") ==-1)
                    {
                        result=result.replace(/<strong style='color:#C00'>/g,"");
                        result=result.replace(/<\/strong>/g,"");
                    }
                else
                {
                    result="";
                }
                $(this).val(result);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me try to ask this question from a different angle. I noticed that
Ok so let me try to explain this the best way that i can.
Alright so I have a task, that I have to let the client try
Let me post some code before I ask question. public Object returnSomeResult() { Object
This is the second time I ask this question because in my first try
I am going to try to ask this question without supplying too much source
let me try to explain my problem. I'm currently trying to develop a small
I have a problem that I tried to ask about previously, but didn't get
Let me try to explain by example. Say website is hosted at example.com (NOT
Let me try to explain it. Please tell me if I am wrong. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.