Doing live search in codeigniter via ajax , mongodb












0














This is my ajax code which fetches all data from data and display in the table, but when I input an alphabet in search-box, it again fetches complete data inside the table



<script>
$(document).ready(function(){

load_data();

function load_data(query)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "<?php echo base_url() ?>Appconfig/get_masteradmin_data?query="+query,true);
xhttp.onload= function()
{
if (xhttp.status >=200 && xhttp.status <400)
{
var data= JSON.parse(xhttp.responseText);
var html = '';
var i;
for(i=0; i<data.length; i++){

html +='<tr>'+
'<td>'+data[i].full_name+'</td>'+
'<td>'+data[i].username+'</td>'+
'<td>'+data[i].designation+'</td>'+
'<td>'+data[i].department+'</td>'+
'<td>'+data[i].official_mobile_no+'</td>'+
'<td>'+data[i].official_email_id+'</td>'+
'<td>'+data[i].select_user_type+'</td>'+
'<td>'+data[i].permission+'</td>'+
'</tr>';
}
showdata.insertAdjacentHTML('beforeend',html);
}
else
{
console.log("Try again after some time");
}

};
xhttp.send();
}


$('#search').keyup(function(){
var search = $(this).val();
//console.log(search);
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>


This is my model for fetching data from my mongodb collection.



public function get_masteradmin_data($query)
{
$mongo = new MongoDBDriverManager('mongodb://localhost:27017');
$query= '';
//$filter = ['full_name' => 'www'];
$regex = new MongoDBBSONRegex ($query);
$filter = ['full_name'=>$regex,];
$options =[
'projection' => [
'_id' => 0,
'full_name' => 1,
'username' => 1,
'designation'=> 1,
'department'=> 1,
'official_mobile_no'=> 1,
'official_email_id'=> 1,
'select_user_type'=> 1,
'permission'=> 1,
],
'sort' => [
'_id' => -1
],

];

$query = new MongoDBDriverQuery($filter, $options);
//$readPreference = new MongoDBDriverReadPreference(MongoDBDriverReadPreference::RP_PRIMARY);
$result = $mongo->executeQuery('justrack_db.master_admin', $query);


$res = array();
foreach($result as $r)
{
$res = $r;
}
return json_encode($res,true);
//return $res;
}


This is my controller for displaying data. I am not sure, but I think there is some issue in my controller, as I tried to echo $query but it shows nothing. I am not able to understand how to fix this.



public function get_masteradmin_data()
{
$query = '';
$this->load->model('AppconfigModel');
$this->master_admin();
if($this->input->post('query'))
{
$query = $this->input->post('query');
}
$result= $this->AppconfigModel->get_masteradmin_data($query);
echo ($result);

}









share|improve this question
























  • The line $query= ''; is overwriting the $query input of the function. So it's always creating an empty regular expression. You probably added that when testing things out. Remove the line.
    – Neil Lunn
    Nov 12 '18 at 11:07












  • @NeilLunn if i remove $query=' '; It does not show any data
    – Alisha
    Nov 12 '18 at 11:25










  • you can use JQuery Filters
    – Vijay Makwana
    Nov 12 '18 at 11:37










  • @VijayMakwana as my data is large , i want to fetch only that data that was necessary
    – Alisha
    Nov 14 '18 at 5:15
















0














This is my ajax code which fetches all data from data and display in the table, but when I input an alphabet in search-box, it again fetches complete data inside the table



<script>
$(document).ready(function(){

load_data();

function load_data(query)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "<?php echo base_url() ?>Appconfig/get_masteradmin_data?query="+query,true);
xhttp.onload= function()
{
if (xhttp.status >=200 && xhttp.status <400)
{
var data= JSON.parse(xhttp.responseText);
var html = '';
var i;
for(i=0; i<data.length; i++){

html +='<tr>'+
'<td>'+data[i].full_name+'</td>'+
'<td>'+data[i].username+'</td>'+
'<td>'+data[i].designation+'</td>'+
'<td>'+data[i].department+'</td>'+
'<td>'+data[i].official_mobile_no+'</td>'+
'<td>'+data[i].official_email_id+'</td>'+
'<td>'+data[i].select_user_type+'</td>'+
'<td>'+data[i].permission+'</td>'+
'</tr>';
}
showdata.insertAdjacentHTML('beforeend',html);
}
else
{
console.log("Try again after some time");
}

};
xhttp.send();
}


$('#search').keyup(function(){
var search = $(this).val();
//console.log(search);
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>


This is my model for fetching data from my mongodb collection.



public function get_masteradmin_data($query)
{
$mongo = new MongoDBDriverManager('mongodb://localhost:27017');
$query= '';
//$filter = ['full_name' => 'www'];
$regex = new MongoDBBSONRegex ($query);
$filter = ['full_name'=>$regex,];
$options =[
'projection' => [
'_id' => 0,
'full_name' => 1,
'username' => 1,
'designation'=> 1,
'department'=> 1,
'official_mobile_no'=> 1,
'official_email_id'=> 1,
'select_user_type'=> 1,
'permission'=> 1,
],
'sort' => [
'_id' => -1
],

];

$query = new MongoDBDriverQuery($filter, $options);
//$readPreference = new MongoDBDriverReadPreference(MongoDBDriverReadPreference::RP_PRIMARY);
$result = $mongo->executeQuery('justrack_db.master_admin', $query);


$res = array();
foreach($result as $r)
{
$res = $r;
}
return json_encode($res,true);
//return $res;
}


This is my controller for displaying data. I am not sure, but I think there is some issue in my controller, as I tried to echo $query but it shows nothing. I am not able to understand how to fix this.



public function get_masteradmin_data()
{
$query = '';
$this->load->model('AppconfigModel');
$this->master_admin();
if($this->input->post('query'))
{
$query = $this->input->post('query');
}
$result= $this->AppconfigModel->get_masteradmin_data($query);
echo ($result);

}









share|improve this question
























  • The line $query= ''; is overwriting the $query input of the function. So it's always creating an empty regular expression. You probably added that when testing things out. Remove the line.
    – Neil Lunn
    Nov 12 '18 at 11:07












  • @NeilLunn if i remove $query=' '; It does not show any data
    – Alisha
    Nov 12 '18 at 11:25










  • you can use JQuery Filters
    – Vijay Makwana
    Nov 12 '18 at 11:37










  • @VijayMakwana as my data is large , i want to fetch only that data that was necessary
    – Alisha
    Nov 14 '18 at 5:15














0












0








0







This is my ajax code which fetches all data from data and display in the table, but when I input an alphabet in search-box, it again fetches complete data inside the table



<script>
$(document).ready(function(){

load_data();

function load_data(query)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "<?php echo base_url() ?>Appconfig/get_masteradmin_data?query="+query,true);
xhttp.onload= function()
{
if (xhttp.status >=200 && xhttp.status <400)
{
var data= JSON.parse(xhttp.responseText);
var html = '';
var i;
for(i=0; i<data.length; i++){

html +='<tr>'+
'<td>'+data[i].full_name+'</td>'+
'<td>'+data[i].username+'</td>'+
'<td>'+data[i].designation+'</td>'+
'<td>'+data[i].department+'</td>'+
'<td>'+data[i].official_mobile_no+'</td>'+
'<td>'+data[i].official_email_id+'</td>'+
'<td>'+data[i].select_user_type+'</td>'+
'<td>'+data[i].permission+'</td>'+
'</tr>';
}
showdata.insertAdjacentHTML('beforeend',html);
}
else
{
console.log("Try again after some time");
}

};
xhttp.send();
}


$('#search').keyup(function(){
var search = $(this).val();
//console.log(search);
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>


This is my model for fetching data from my mongodb collection.



public function get_masteradmin_data($query)
{
$mongo = new MongoDBDriverManager('mongodb://localhost:27017');
$query= '';
//$filter = ['full_name' => 'www'];
$regex = new MongoDBBSONRegex ($query);
$filter = ['full_name'=>$regex,];
$options =[
'projection' => [
'_id' => 0,
'full_name' => 1,
'username' => 1,
'designation'=> 1,
'department'=> 1,
'official_mobile_no'=> 1,
'official_email_id'=> 1,
'select_user_type'=> 1,
'permission'=> 1,
],
'sort' => [
'_id' => -1
],

];

$query = new MongoDBDriverQuery($filter, $options);
//$readPreference = new MongoDBDriverReadPreference(MongoDBDriverReadPreference::RP_PRIMARY);
$result = $mongo->executeQuery('justrack_db.master_admin', $query);


$res = array();
foreach($result as $r)
{
$res = $r;
}
return json_encode($res,true);
//return $res;
}


This is my controller for displaying data. I am not sure, but I think there is some issue in my controller, as I tried to echo $query but it shows nothing. I am not able to understand how to fix this.



public function get_masteradmin_data()
{
$query = '';
$this->load->model('AppconfigModel');
$this->master_admin();
if($this->input->post('query'))
{
$query = $this->input->post('query');
}
$result= $this->AppconfigModel->get_masteradmin_data($query);
echo ($result);

}









share|improve this question















This is my ajax code which fetches all data from data and display in the table, but when I input an alphabet in search-box, it again fetches complete data inside the table



<script>
$(document).ready(function(){

load_data();

function load_data(query)
{
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "<?php echo base_url() ?>Appconfig/get_masteradmin_data?query="+query,true);
xhttp.onload= function()
{
if (xhttp.status >=200 && xhttp.status <400)
{
var data= JSON.parse(xhttp.responseText);
var html = '';
var i;
for(i=0; i<data.length; i++){

html +='<tr>'+
'<td>'+data[i].full_name+'</td>'+
'<td>'+data[i].username+'</td>'+
'<td>'+data[i].designation+'</td>'+
'<td>'+data[i].department+'</td>'+
'<td>'+data[i].official_mobile_no+'</td>'+
'<td>'+data[i].official_email_id+'</td>'+
'<td>'+data[i].select_user_type+'</td>'+
'<td>'+data[i].permission+'</td>'+
'</tr>';
}
showdata.insertAdjacentHTML('beforeend',html);
}
else
{
console.log("Try again after some time");
}

};
xhttp.send();
}


$('#search').keyup(function(){
var search = $(this).val();
//console.log(search);
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>


This is my model for fetching data from my mongodb collection.



public function get_masteradmin_data($query)
{
$mongo = new MongoDBDriverManager('mongodb://localhost:27017');
$query= '';
//$filter = ['full_name' => 'www'];
$regex = new MongoDBBSONRegex ($query);
$filter = ['full_name'=>$regex,];
$options =[
'projection' => [
'_id' => 0,
'full_name' => 1,
'username' => 1,
'designation'=> 1,
'department'=> 1,
'official_mobile_no'=> 1,
'official_email_id'=> 1,
'select_user_type'=> 1,
'permission'=> 1,
],
'sort' => [
'_id' => -1
],

];

$query = new MongoDBDriverQuery($filter, $options);
//$readPreference = new MongoDBDriverReadPreference(MongoDBDriverReadPreference::RP_PRIMARY);
$result = $mongo->executeQuery('justrack_db.master_admin', $query);


$res = array();
foreach($result as $r)
{
$res = $r;
}
return json_encode($res,true);
//return $res;
}


This is my controller for displaying data. I am not sure, but I think there is some issue in my controller, as I tried to echo $query but it shows nothing. I am not able to understand how to fix this.



public function get_masteradmin_data()
{
$query = '';
$this->load->model('AppconfigModel');
$this->master_admin();
if($this->input->post('query'))
{
$query = $this->input->post('query');
}
$result= $this->AppconfigModel->get_masteradmin_data($query);
echo ($result);

}






ajax mongodb codeigniter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 12:16









Mukyuu

7091619




7091619










asked Nov 12 '18 at 11:04









AlishaAlisha

43




43












  • The line $query= ''; is overwriting the $query input of the function. So it's always creating an empty regular expression. You probably added that when testing things out. Remove the line.
    – Neil Lunn
    Nov 12 '18 at 11:07












  • @NeilLunn if i remove $query=' '; It does not show any data
    – Alisha
    Nov 12 '18 at 11:25










  • you can use JQuery Filters
    – Vijay Makwana
    Nov 12 '18 at 11:37










  • @VijayMakwana as my data is large , i want to fetch only that data that was necessary
    – Alisha
    Nov 14 '18 at 5:15


















  • The line $query= ''; is overwriting the $query input of the function. So it's always creating an empty regular expression. You probably added that when testing things out. Remove the line.
    – Neil Lunn
    Nov 12 '18 at 11:07












  • @NeilLunn if i remove $query=' '; It does not show any data
    – Alisha
    Nov 12 '18 at 11:25










  • you can use JQuery Filters
    – Vijay Makwana
    Nov 12 '18 at 11:37










  • @VijayMakwana as my data is large , i want to fetch only that data that was necessary
    – Alisha
    Nov 14 '18 at 5:15
















The line $query= ''; is overwriting the $query input of the function. So it's always creating an empty regular expression. You probably added that when testing things out. Remove the line.
– Neil Lunn
Nov 12 '18 at 11:07






The line $query= ''; is overwriting the $query input of the function. So it's always creating an empty regular expression. You probably added that when testing things out. Remove the line.
– Neil Lunn
Nov 12 '18 at 11:07














@NeilLunn if i remove $query=' '; It does not show any data
– Alisha
Nov 12 '18 at 11:25




@NeilLunn if i remove $query=' '; It does not show any data
– Alisha
Nov 12 '18 at 11:25












you can use JQuery Filters
– Vijay Makwana
Nov 12 '18 at 11:37




you can use JQuery Filters
– Vijay Makwana
Nov 12 '18 at 11:37












@VijayMakwana as my data is large , i want to fetch only that data that was necessary
– Alisha
Nov 14 '18 at 5:15




@VijayMakwana as my data is large , i want to fetch only that data that was necessary
– Alisha
Nov 14 '18 at 5:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260826%2fdoing-live-search-in-codeigniter-via-ajax-mongodb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53260826%2fdoing-live-search-in-codeigniter-via-ajax-mongodb%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation