Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+7 votes
7.7k views
in Q2A Core by
by
This will be very useful feature. we can suggest Gideon for 1.5 version.

2 Answers

+1 vote
by

Actually, it doesn't seem to be all that hard.  I copied the /qa-include/qa-page-search.php file and then used the code pasted below.

require_once QA_INCLUDE_DIR.'qa-app-users.php';

 

require_once QA_INCLUDE_DIR.'qa-app-cookies.php';
require_once QA_INCLUDE_DIR.'qa-app-format.php';
 
// Perform the search if appropriate
 
if (strlen(qa_get('q'))) {
require_once QA_INCLUDE_DIR.'qa-db-selects.php';
require_once QA_INCLUDE_DIR.'qa-util-string.php';
 
$inquery=trim(qa_get('q'));
$words=qa_string_to_words($inquery);
$retrieve=2*QA_DB_RETRIEVE_QS_AS+1; // get enough results to be able to give some idea of how many pages of search results there are
 
//ADDED BY NATHAN
$qa_login_userid=qa_get_logged_in_userid();
$qa_start=min(max(0, (int)qa_get('start')), QA_MAX_LIMIT_START);
$qa_cookieid=qa_cookie_get();
//END ADDED BY NATHAN
 
 
$questions=qa_db_select_with_pending(
qa_db_search_posts_selectspec($qa_login_userid, $words, $words, $words, $words, $inquery, $qa_start, false, $retrieve)
);
 
$pagesize=qa_opt('page_size_search');
$gotcount=count($questions);
$questions=array_slice($questions, 0, $pagesize);
 
$usershtml=qa_userids_handles_html($questions);
 
qa_report_event('ajax_search', $qa_login_userid, qa_get_logged_in_handle(), $qa_cookieid, array(
'query' => $inquery,
'start' => $qa_start,
));
 
 
echo json_encode($questions);
}
 
I also had to add a "search" case to the switch statement in qa-include/qa-ajax.php
 
case 'search':
require QA_INCLUDE_DIR.'qa-ajax-search.php';
break;
 
To allow browser-based testing I also allowed a few variables to be read from the $_REQUEST array instead of the $_POST array.
 
With those changes done, you can point your browser to 

 

YOURDOMAIN/qa/index.php?qa=ajax&qa_operation=search&q=searchQuery

And you'll get JSON-encoded search results

+1 vote
by

I also wanted to make add search autocomplete.
I found out that autocomplete option is already built in the code, and is used when someone is asking (not searching) a question on the ask page.

I added a search box to my theme's hearder (you may alrady have your search box somewhere)

<input type="text" placeholder="Find your answer..." name="q" onkeyup="qa_title_change_search(this.value);" autocomplete="off" required>

Then I added the a span which will display the suggested questions just below the search box:

<span id="similar_search"></span>

 

Now I went to qa-ask.js and based on this function "qa_title_change"(which is used for the autocomplete while asking a question) I careated the function: "qa_title_change_search"

function qa_title_change_search(value) {
    qa_ajax_post('asktitle', { title: value }, function (lines) {
        if (lines[0] == '1') {
            if (lines[1].length) {
                qa_tags_examples = lines[1];
                qa_tag_hints(true);
            }
 
            if (lines.length > 2) {
                var simelem = document.getElementById('similar_search');
                if (simelem)
                    simelem.innerHTML = lines.slice(2).join('\n');
            }
 
        } else if (lines[0] == '0')
            alert(lines[1]);
        else
            qa_ajax_error();
    });
 
    qa_show_waiting_after(document.getElementById('similar_search'), true);
}
 
If you want to change the line displayed before the suggested questions, you can look for "ask_same_q" in qa-lan-question.php (be default it is: "Before proceeding, please check your question was not asked already:")
by
Hi Alex,
Was wondering if you have implemented it somewhere so we can see, live demo I mean
by
Nice! I am using google powered search which also searches the body of questions, but ofcourse not as cool as an auto search one like your. btw i know a friend who produces a lot of those 3D anaglyph glasses you are also using in your logo
...