Codeigniter Ajax dropdown - ajax

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

Related

Getting remarks of teacher and principal in view

I'm trying to get the comments of teacher and principal to show up in view, all to no avail.
This is a school web app where parents get to view the results of their respective children. Just below the scores is the teacher's and principal comments.
Now, these comments show up in students' view but I'm having a hard time making it show up on the parents' view.
This is the model:
Comment_model.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Comment_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->current_session = $this->setting_model->getCurrentSession();
$this->current_session_name = $this->setting_model->getCurrentSessionName();
$this->start_month = $this->setting_model->getStartMonth();
}
public function TeacherComment($data)
{
$this->db->insert('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function UpdateTeacherComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('teacher_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetTeacherComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('teacher_comments')->row();
}
public function PrincipalComment($data)
{
$this->db->insert('principal_comments', $data);
return $this->db->affected_rows();
}
public function UpdatePrincipalComment($data, $id)
{
$this->db->where('id', $id);
$this->db->update('principal_comments', $data);
return $query = $this->db->affected_rows();
}
public function GetPrincipalComment($student_id, $session_id)
{
$this->db->select('*');
$this->db->where('student_id', $student_id);
$this->db->where('session_id', $session_id);
return $this->db->get('principal_comments')->row();
}
}
The Controller I'm battling with:
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
How do I properly put it in function?
The view:
<span> CLASS TEACHER'S REMARK:
<u style="text-transform: uppercase;"><?php echo $teacher_comment->teacher_comment; ?> </u>
</span><br>
<br>
<span>PRINCIPAL REMARK:
<u style="text-transform: uppercase;"><?php echo $principal_comment->principal_comment; ?></u>
</span>
Let suppose this is your controller file.
class customview extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model("Comment_model");
}
public function yourViewFunction()
{
// get your $id and $student_session_id
$data['teacher_comment'] = $this->Comment_model->GetTeacherComment($id, $student_session_id);
$data['principal_comment'] = $this->Comment_model->GetPrincipalComment($id, $student_session_id);
$this->load->view('your_html_view',$data); //this is the view file name without php
}
}
And now in your html view you can call like this:
print_r($teacher_comment); //you can view the array or object get from model
print_r($principal_comment); //you can view the array or object get from model

Update data using add action

I'm use Grocery CRUD. I create custom button using add_action.
The button for change data to 0. So, after click the button database update column to 0.
Controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Attendance extends Admin_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_builder');
}
public function attendance($visitor_id)
{
$attendance_status = array(
'attendance_status' => 0
);
$update = $this->Attendace_model->update_attendance_status($visitor_id,$attendance_status);
if($update)
{
$this->load->view('Attendance');
}
else
{
alert("error");
}
}
}
Model :
<?php
class Attendance_model extends MY_Model {
function __construct()
{
parent::__construct();
}
function update_attendance_status($visitor_id,$attendance_status)
{
$this->db->where('id', $visitor_id);
$this->db->update('invitation_codes', $attendance_status);
}
}
What code to use in function for update the data to 0/
Controller:
public function attendance($visitor_id)
{
$attendance_status = array(
'attendance_status' => 0
);
$update = $this->YOUR_MODEL_NAME- >update_attendance_status($visitor_id,$attendance_status);
if($update)
{
return true;
}
else
{
return false;
}
}
Model :
public function update_attendance_status($visitor_id,$attendance_status)
{
$this->db->where('id', $visitor_id);
$this->db->update('invitation_codes', $attendance_status);
}

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
}
});

autocomplete textbox are not working in ci

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.

Codeigniter model not loading

I'm a newbe in codeigniter.
something is going wrong with I think the model.
this is the controler:
<?php
class Fuel extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->library('table');
}
public function image() {
$data['title'] = 'test';
$data['main_content'] = 'imagetest';
$this->load->view("template", $data);
}
public function overview() {
$this->load->model('Get_DB');
$this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
when I load the image function, it works just fine, but the function overview is the problem.
this is my model:
<?php
class Get_DB extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function overzicht() {
$query = $this->db->query("SELECT * FROM invoer "
. "ORDER BY datum DESC");
$gen_query = $this->table->generate($query);
return $gen_query;
}
}
and this is my view:
<?php
echo $gen_query;
and if you want to know: my template is this:
<?php
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
now when I open my view I get this message:
A PHP Error was encountered
Severity: Notice Message: Undefined variable: gen_query Filename:
views/overzicht.php Line Number: 3
in the model you see that I have made a var $gen_query
so why is that undifined?
regards,
Ralph
Try:
public function overview() {
$this->load->model('Get_DB');
$data = array();
$data['gen_query'] = $this->Get_DB->overzicht() ; #corrected model function and save the result in `gen_query`
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
In Controler:
public function overview() {
$this->load->model('Get_DB');
$result = $this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$data['re'] = $result;
$this->load->view("template", $data);
}
And In view page you can retrive the result
like
foreach($re->result() as $row)
{
//You can get each row data here $row->your_field_names
}

Resources