autocomplete textbox are not working in ci - codeigniter

controller: Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Fetch_data');
}
public function index()
{
$searchTerm = $_GET['term'];
$data['search'] = $this->Fetch_data->autocomplete($searchTerm);
$this->load->view('index',$data);
}
}
view: index.php
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input type="text" name="colleges" id="colleges" class="form-control" />
<script>
$(function() {
$( "#colleges" ).autocomplete({
source: '<?php echo base_url("index.php"); ?>/test/index';
});
});
</script>
model: Fetch_data.php
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function autocomplete($searchTerm)
{
$this->db->select('college_name,field');
$this->db->from('all_colleges');
$where = "short_name like '%".$searchTerm."%' or college_name like '%".$searchTerm."%'";
$this->db->where($where);
$this->db->order_by('college_name');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
I am new in ci. Using this code I want to create a autocomplete suggestion box in codeigniter But this code is not work for me. So, how can I create autocomplete suggestion box in ci and please tell me what wrong in my code can I use this code for autocompletion.
Thank You

UPDATED You can try this:
in you Model Fetch_data.php
<?php
class Fetch_data extends CI_Model
{
function __construct()
{
parent::__construct();
}
public function autocomplete()
{
$this->db->select('college_name,field');
$this->db->from('all_colleges');
$this->db->order_by('college_name');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
}
controller: Test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Fetch_data');
}
public function index()
{
$all_data = array();
$result = $this->Fetch_data->autocomplete(); //collect all college name
foreach($result as $key=>$value){
array_push($all_data, $value["college_name"]);
}
$data['search'] = $all_data;
$this->load->view('index',$data);
}
}
view: index.php
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input type="text" name="colleges" id="colleges" class="form-control" />
<input type='hidden' id='mydata' value='<?php echo json_encode($search);?>' />
<script>
$( function() {
var oData = $("#mydata").val();
oData = JSON.parse(oData);
$( "#colleges" ).autocomplete({
source: oData;
});
});
</script>
You can get all the college_names and store it with a variable then when you enter a letter you can no longer use ajax because it is already stored with a variable.
Hope it will work.

Related

Writing to Database when executing controller's index method - error

I call the index() method of the collaborating controller, it automatically inserts 6 records into the database, and the action of logging into the database is not in the cadastrarquestao() method of the class, and not in the index method . I'm using a template library.
Controller Colaborador:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Colaborador extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->helper('funcoes_helper');
$this->load->helper('file');
$this->load->helper('cookie');
$this->load->library('session');
$this->load->library('controle_acesso');
$this->load->model('resumoapp_model', 'resumo');
$this->load->model('homeapp_model', 'homeapp');
$this->load->model('simulado_model', 'simulado');
$this->controle_acesso->acesso();
$this->output->enable_profiler(TRUE);
}
public function index() {
$dados['teste'] = 1;
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function logout() {
session_unset();
redirect ('/entrar');
}
public function cadastrarquestao() {
$this->simulado->envioquestao(1);
$dados['mensagem'] = "dados cadastrados com sucesso!";
$this->template->load("template/template_app",'app/enviar-questao', $dados);
}
public function materia() {
}
}
part of the code
public function envioquestao($dados) {
$data = [
'Id_usuario' => $this->session->userdata('id_usuario'),
];
$this->db->insert('teste',$data);
}
librarie Template:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Template {
var $template_data = array();
function set($name, $value)
{
$this->template_data[$name] = $value;
}
function load($template = '', $view = '' , $view_data = array(), $return = FALSE)
{
$this->CI =& get_instance();
$this->set('contents', $this->CI->load->view($view, $view_data, TRUE));
return $this->CI->load->view($template, $this->template_data, $return);
}
}
View template:
<html lang="pt-br">
<?php
$this->load->view('app/header');
?>
<body>
<div class="wrapper">
<?php
$this->load->view('app/topo');
?>
<?php
$this->load->view('app/menu');
?>
<?php echo $contents ?>
<footer class="footer-container"><span>© </span></footer>
</div>
<?php
$this->load->view('app/footer');
?>
</body>
</html>
the correct one would be to insert it in the database after clicking on 'register' which is in the cadastrarquestao() method

Codeigniter AutoComplete Stop working when i put the code under login conditions

The Code Runs Fine when i run the code without login Conditions, When i put code under login condition it gets stop. if a login into application even that time code is not under login conditions but the search gets stop until i restart my computer
My Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class autocomplete extends CI_Controller {
public function __construct(){
parent::__construct();
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
}
else
{
redirect('login', 'refresh');
}
}
public function index(){
$this->load->view('view_demo');
}
public function start()
{
$json = [];
$this->load->database();
if(!empty($this->input->get("q"))){
$this->db->like('name', $this->input->get("q"));
$query = $this->db->select('drugs_id as id,name as text')
->limit(10)
->get("drugs");
$json = $query->result();
}
echo json_encode($json);
}
}
Here is My View
<div style="width:520px;margin:0px auto;margin-top:30px;height:500px;">
<h2>Select Box with Search Option Jquery Select2.js</h2>
<select class="itemName form-control" style="width:500px" name="itemName">
</select>
</div>
Here is Script
$('.itemName').select2({
placeholder: '--- Select Item ---',
ajax: {
url: 'http://localhost/dcms/autocomplete/start',
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});

Error displayed when trying to insert data from db into dropdown list within CodeIgniter

The following error is displayed:.............................................
Severity: Notice
Message: Undefined variable: groups
Filename: views/content_view.php
Line Number: 10
please can you assist me...below is the code that i have tried
below is my controller
**MY CONTROLLER**
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('model_get');
}
public function delivery()
{
$data['groups'] = $this->model_get->getAllGroups();
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view('login');
$this->load->view('content_view');
$this->load->view("content_video");
$this->load->view("site_footer");
}
}
?>
below is my view
**MY VIEW**
<div id="content" class="col-md-6">
<h1> Comparison</h1>
<select class="form-control">
<?php
if (is_array($groups))
{
foreach($groups as $row)
{
echo '<option value="'.$row->page.'">'.$row->page.'</option>';
}
}
?>
</select>
</div>
below is the relevant information from the model
MY MODEL
class Model_get extends CI_Model{
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT page FROM pageData');
return $query->result();
}
}
Try:
$this->load->view('content_view', $data);

Codeigniter Ajax dropdown

controller(user.php)
<?php
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('country_model');
}
public function index() {
$this->load->view('register');
}
public function register() {
$data['countries'] = $this->country_model->get_countries();
$this->load->view('post_view', $data);
}
public function get_cities($country) {
$this->load->model('city_model');
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->city_model->get_cities($country)));
}
}
City_model
<?php
class City_model extends CI_Model {
public function __construct() {
$this->load->database();
}
function get_cities($country = null) {
$this->db->select('id, city_name');
if ($country != NULL) {
$this->db->where('country_id', $country);
}
$query = $this->db->get('cities');
$cities = array();
if ($query->result()) {
foreach ($query->result() as $city) {
$cities[$city->id] = $city->city_name;
}
return $cities;
} else {
return FALSE;
}
}
}
?>
country_model
<?php
class Country_model extends CI_Model {
public function __construct() {
$this->load->database();
}
function get_countries() {
$this->db->select('id, country_name');
$query = $this->db->get('countries');
echo "countries";
$countries = array();
if ($query->result()) {
foreach ($query->result() as $country) {
$countries[$country->id] = $country->country_name;
}
return $countries;
} else {
return FALSE;
}
}
}
?>
view file
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
$('#country').change(function() { //any select change on the dropdown with id country trigger this code
$("#cities > option").remove(); //first of all clear select items
var country_id = $('#country').val(); // here we are taking country id of the selected one.
$.ajax({
type: "POST",
url: "http://localhost/task/user/get_cities/" + country_id, //here we are calling our user controller and get_cities method with the country_id
success: function(cities) //we're calling the response json array 'cities'
{
$.each(cities, function(id, city) //here we're doing a foeach loop round each city with id as the key and city as the value
{
var opt = $('<option />'); // here we're creating a new select option with for each city
opt.val(id);
opt.text(city);
$('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
});
}
});
});
});
// ]]>
</script>
</head>
<body>
<?php $countries['#'] = 'Please Select'; ?>
<label for="country">Country: </label><?php echo form_dropdown('country_id', $countries, '#', 'id="country"'); ?><br />
<?php $cities['#'] = 'Please Select'; ?>
<label for="city">City: </label><?php echo form_dropdown('city_id', $cities, '#', 'id="cities"'); ?><br />
</body>
</html>
I am trying to populate the data from database using codeigniter ajax dropdown. I couldnt able to get the value from database. When i select a country name from the dropdown list the city name should be triggered out.I dont know where i did mistake. Any help would appreciatable.
Try this:
public function get_cities($country)
{
$this->load->model('city_model');
$result = $this->city_model->get_cities($country);
$this->output->set_output(json_encode($result));
}
}
public function get_cities($country) {
$this->load->model('city_model');
$this->output->set_content_type('application/json');
$this->output->set_status_header(200);
$this->output->set_output(
json_encode(
$this->city_model->get_cities($country)
)
);
}
use this code for json reply

why is this error showing

I am using CI for my web-site. While programming CMS for the same I faced no problem but when I am programming the same for user-end I am getting error as:
"404 Page Not Found
The page you requested was not found."
What am i doing wrong?? Any help/suggestions is warmly welcome.Thank you.
In Controller(model.phpl):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('user_model');
$this->load->model('common');
$this->load->model('home_model');
$this->load->model('page_model');
}
function _remap($method , $params = array())
{
$this->menu = $this->common->createMenuArr();
$this->index();
}
function index()
{
$data['sliderArr'] = $this->user_model->sliders();
$this->load->view('index', $data);
}
}
In Model(user_model.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model{
function __construct()
{
parent::__construct();
//$this->load->database();
}
function sliders()
{
$query = $this->db->query("SELECT slider FROM tbl_sliders ORDER BY slider_id DESC")->result_array();
return $query;
}
}
and finally in view(index.php):
<div id="slideContainer">
<div id="slideShim">
<?php
if(!empty($sliderArr))
{
foreach($sliderArr as $slider)
{ ?>
<img src="<?php echo base_url();?>images/sliders/<?php echo $slider['slider'];?>"
<?php }
}
?>
</div>
</div>
Go to Application -> Config -> Routes.
And Set $route['default_controller'] = "index"; //whatever your controller name is on which you want your application to route by default
Hope this helps.

Resources