dynamic codeigniter select not working - codeigniter

controller
car.php
<?php
class Car extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('company_model');
}
public function index()
{
//starts by running the query for the countries
//dropdown
$data['companydrop'] = $this->company_model->company();
//loads up the view with the query results
$this->load->view('car_view', $data);
}
//call to fill the second dropdown with the cities
public function car_model()
{
//set selected country id from POST
echo $company_id = $this->input->post('company_id',TRUE);
//run the query for the cities we specified earlier
$cardata['cardrop']=$this->company_model->car($company_id);
print_r($cardata);
$output = null;
foreach ($cardata['cardrop'] as $row)
{
//here we build a dropdown item line for each
// query result
$output .= "<option value='".$row->car_model."'>".$row->car_model."</option>";
}
echo $output;
}
}
?>
model
company_model
<?php
class Company_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
//fill your contry dropdown
public function company()
{
$this->db->select('company_id,company_name');
$this->db->from('company');
$query = $this->db->get();
// the query mean select cat_id,category from
//category
foreach($query->result_array() as $row){
$data[$row['company_id']]=$row['company_name'];
}
// the fetching data from database is return
return $data;
}
//fill your cities dropdown depending on the selected city
public function car($company_id=string)
{
$this->db->select('car_id,car_model');
$this->db->from('car');
$this->db->where('company',$company_id);
$query = $this->db->get();
return $query->result();
}
}
?>
view
car_view
<html>
<head>
<title>car dealers</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#companydrop").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo base_url();?>index.php/car/car_model",
data: {id:$(this).val()},
type: "POST",
success:function(data){
$("#cardrop").html(data);
alert(data);
}
});
});
});
</script>
<style>
body{
no-repeat;
background:url(../../../video-fallback-background.jpg)
}
</style>
</head>
<body>
<!--company dropdown-->
<?php echo form_dropdown('companydrop',$companydrop,'','class="required" id="companydrop"'); ?>
<br />
<br />
<!--car dropdown-->
<select name="cardrop" id="cardrop">
<option value="">Select</option>
</select>
<br />
</body>
</html>
dynamic dropdown is not working as the first select which is the
company name is working as it is fetched from database,but car model is not working,it not fetched to the dropdown.i need to fetch the car company model from database and then after selecting the company the model of that specified company has to be listed in the second dropdown.i have created database in phpmyadmin and created two table car and company,in company copany_id and company_name where as in car has car_id,car_name and company_id

Check this sample code for creating dropdown in codeigniter.
<?php
$js = 'id="unicode" class="form-control"';
$unicode = array(
'2' => 'No',
'1' => 'Yes'
);
echo form_dropdown('unicode', $unicode, set_value('unicode'), $js);
?>
Here Dropdown id is unicode,class is form-control.
Html will look like :
<select name="unicode" id="unicode" class="form-control">
<option value="2">No</option>
<option value="1">Yes</option>
</select>
You can get you values from db in an array and then store it in a variable like $unicode.Hope this helps.Check this ref link
For setting another dropdown based on first dropdown:
$("#dropdown1").change(function () {
var end = this.value;
$('#dropdown2').val(end );
});

In Your Car Controller Please remove print_r($cardata); first.
Then see in your console what response you are getting from the call. I suggest you to get data in json format and parse it on client end. It is the best practice.

i corrected the code and finally it worked,i will post the correct code, if it helps anyone in future.thanks to everyone who tried to help me..
controller
car.php
<?php
class Car extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('company_model');
}
public function index()
{
$data['companydrop'] = $this->company_model->company();
$this->load->view('car_view', $data);
}
public function car_model()
{
$company_id = $this->input->post('company_id',TRUE);
$cardata['cardrop']=$this->company_model->car($company_id);
$output = null;
foreach ($cardata['cardrop'] as $row)
{
$output .= "<option value='".$row->car_model."'>".$row->car_model."</option>";
}
echo $output;
}
}
?>
model
company_model
<?php
class Company_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function company()
{
$this->db->select('company_id,company_name');
$this->db->from('company');
$query = $this->db->get();
foreach($query->result_array() as $row){
$data[$row['company_id']]=$row['company_name'];
}
return $data;
}
public function car($company_id)
{
$this->db->select('car_id,car_model');
$this->db->from('car');
$this->db->where('company_id',$company_id);
$query = $this->db->get();
return $query->result();
}
}
?>
view
car_view
<html>
<head>
<title>car dealers</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#companydrop").change(function(){
/*dropdown post *///
$.ajax({
url:"<?php echo base_url();?>index.php/car/car_model",
data: {company_id:$(this).val()},
type: "POST",
success:function(data){
$('#cardrop option[value!=0]').remove()
$("#cardrop").append(data);
}
});
});
});
</script>
<style>
body{
no-repeat;
background:url(../../../video-fallback-background.jpg)
}
</style>
</head>
<body>
<center><font color="#333366"><strong></strong><h2>CR Motors</h2></font></center>
<center><font color="#FF8000"><h3>Select the car to purchase...</h3></center></font>
<!--company dropdown-->
<tr>
<td>
<font color="#00FF99">
Select the company</font>
<?php echo form_dropdown('companydrop',$companydrop,'','class="required" id="companydrop"'); ?> </td>
</tr>
<br />
<br />
<!--car dropdown-->
<tr>
<td>
<font color="#00FF99">
Select the model</font>
<select name="cardrop" id="cardrop">
<option value="0">Select</option>
</select>
</td>
</tr>
<br />
</body>
</html>

Related

Select2 The Result Cannot Be Load in CodeIgniter

When I type in my select box the result always show "The result cannot be load". I am using CodeIgniter for my framework and select2 in my select box and am a newbie in CodeIgniter and Select2. I have already search it through all articles that can be related to my problem but it still can't work. I think I messed my ajax but I don't know how and where I should fix it.
Here is my controller
<?php
class Admin extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('admin_model');
}
function search_book(){
$booksClue = $this->input->get('booksClue');
$data_book = $this->admin_model->get_book($booksClue, 'id_book');
echo json_encode($data_book);
}
}
?>
Here is my model
<?php
class Admin_model extends CI_Model{
function get_book($booksClue, $column){
$this->db->select($column);
$this->db->like('id_book', $booksClue);
$data = $this->db->from('book')->get();
return $data->result_array();
}
}
?>
And here is my view
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$('.searchingBook').select2({
placeholder: 'Masukkan ID Buku',
minimumInputLength: 1,
allowClear: true,
ajax:{
url: "<?php echo base_url(); ?>/admin/searchbook",
dataType: "json",
delay: 250,
data: function(params){
return{
booksClue: params.term
};
},
processResults: function(data){
var results = [];
$.each(data, function(index, item){
results.push({
id: item.id_book,
text: item.id_book
});
});
return{
results: results
};
}
}
});
});
</script>
<table>
<tr>
<td><b>ID Buku</b></td>
<td> : </td>
<td>
<select class="searchingBook" style="width: 500px">
<option></option>
</select>
</td>
</tr>
</table>
<br><br>
</body>
</html>
Big thanks for your help!
Update your model function like below:
function get_book($booksClue, $column){
$this->db->select($column);
$this->db->from('book');
$this->db->like('id_book', $booksClue);
return $this->db->get()->result_array();
}

filtering table results using ajax

I'm working on a php online shop website using codeigniter framework and i want to be able to filter my table results using checkboxes with ajax
view:
<input type="checkbox" name="brand" value="acer">
<input type="checkbox" name="brand" value="lenovo">
<input type="checkbox" name="pret" value="1000">
<table>
<tbody>
<?php foreach ($laptops_toate as $laptops_all) { ?>
<tr>
<td><img src="http://localhost:82/ci/images/emag/<?php echo $laptops_all->file ?>"></td>
<td><p>Laptop <?php echo $laptops_all->brand ?> </p>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Controller:
public function laptops()
{
$filter = array(
'pret' => $this->input->get('pret'),
'brand' =>$this->input->get('brand')
);
$data['laptops_toate'] = $this->emag_model->laptops_toate_grid($filter);
$this->renders('emag/laptops', $data);
}
model:
public function laptops_toate_grid($filter = null){
$this->db->select('*')
->from('laptop_notebook');
// $query = $this->db->get('laptop_notebook')->result();
// return $query;
if($filter['brand']){
$this->db->where('brand', $filter['brand']);
}
if($filter['pret']){
$this->db->where('pret', $filter['pret']);
}
$query = $this->db->get()->result();
return $query;
}
The problem is now at the ajax code, i don't know how to send the data filter to the server in order to receive the success function.
View:
<script>
$("input[checkbox]").change(function(){
$.ajax({
url: route,
dataType: 'json',
success: function(data){
$.each(data, function(index, element) {
$("tbody").empty();
$("tbody").append("<tr><td>"+
"Laptop "+element.brand+""+
"</td></tr>");
});
}
});
Controller:
public function laptops()
{
$filter = array(
'pret' => $this->input->get('pret'),
'brand' =>$this->input->get('brand')
);
echo json_encode($this->emag_model->laptops_toate_grid($filter));
}
Now just do console.log(data); first inside the $.each() to see what your array looks like.

dependent drop down list in codeigniter

dependent drop down list in are not working
my controller
<?php
class User extends CI_Controller {
public function __construct() {
parent::__construct();
$this -> load -> model('country_model');
}
function index()
{
$data['countries'] = $this -> country_model -> get_countries();
$this -> load -> view('post_view', $data);
}
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)));
}
}
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');
$countries = array();
if ($query -> result()) {
foreach ($query->result() as $country) {
$countries[$country -> id] = $country -> country_name;
}
return $countries;
} else
{
return FALSE;
}
}
}
my 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;
}
}
}
?>
and my view (post_view)
<html>
<head>
<script src="//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: "Co/"+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>
<form method="get">
<label for="category">Parent Category</label>
<select name="countries" id="parent_cat">
<?php foreach($countries as $m)
{
?>
<option value="<?php echo $m->id; ?>"><?php echo $m->category_name; ?></option>
<?php
}
?>
</select>
</form>
<?php $countries['#'] = 'Please Select'; ?>
<label for="country">Country: </label>
<?php 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
my country db
id country_name
1 India
2 Sri Lanka
my city db
id city_name country_id
1 Mumbai 1
2 Kolkatta 1
3 Colombo 2
4 Matara 2
Because you did not create it, add this line to your views/post_view.php file :
<?php echo form_dropdown('country_id', $countries, '') ?>

Unable to create the third drop down using ajax in codeigniter

I am trying to create ajax dropdown in three layers state, city and location in codeigniter 2.1.4. The first layer is working fine I am able to fetch the city list from state id but unable to fetch location from city id. I thing I am doing some mistake in the ajax I need help. My code is mentioned below:
view
<div id="innerdiv1">
<label>State</label>
<br />
<select name="state_id" id="state_id">
<option value="">-- Select State --</option>
<?php foreach ($states as $all_states): ?>
<option value="<?=$all_states['id'];?>"><?=$all_states['state'];?></option>
<?php endforeach ?>
</select>
</div>
<div id="innerdiv2">
<label>City</label>
<br />
<div id="city">
<select name="city_id" id="city_id">
<option value="">-- Select City-- </option>
</select>
</div>
</div>
<div id="innerdiv1">
<label>Location</label>
<br />
<div id="location">
<select name="location_id" id="location_id">
<option value="">-- Select Location-- </option>
</select>
</div>
</div>
Ajax
$(document).ready(function () {
$('#state_id').change(function () {
var selState = $(this).val();
console.log(selState);
$.ajax({
url: "pages/get_cities",
async: false,
type: "POST",
data: "state="+selState,
dataType: "html",
success: function(data) {
$('#city').html(data);
}
})
});
$('#city_id').change(function () {
var selCity = $(this).val();
alert(selCity);
console.log(selCity);
$.ajax({
url: "pages/get_locations",
async: false,
type: "POST",
data: "cities="+selCity,
dataType: "html",
success: function(data) {
$('#location').html(data);
}
})
});
});
</script>
city model
<?php
class City_model extends CI_Model {
public function __construct() {
$this -> load -> database();
//$this->output->enable_profiler(TRUE);
}
function get_cities($state){
if($state != NULL){
$this->db->where('state_id', $state);
$query = $this->db->get('city');
$cities = array();
$html = '';
if($query->result())
{
$html .= '<select id="city_id" name="city_id">';
$html .= '<option value="">-- Select City --</option>';
foreach ($query->result() as $city)
{
//$cities[$city->id] = $city->city;
$html .= '<option value="'.$city->id .'">'.$city->city.'</option>';
}
$html .= '</select>';
return $html;
}
else
{
return FALSE;
}
}
else
{
$html = '<option value="">--Select City--</option>';
return $html;
}
}
}
Controller
class Pages extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('home_model');
$this->load->model('sub_cat_model');
$this->load->model('state_model');
$this->load->model('city_model');
$this->load->model('location_model');
$this->load->library('email');
}
public function index()
{
$data['state'] = $this->home_model->get_state();
$data['title'] = 'Rimi Classified - Home';
$this->load->view('templates/header', $data);
$this->load->view('index', $data);
$this->load->view('templates/footer', $data);
}
public function sign_up()
{
$data['states'] = $this->state_model->get_states();
$data['error'] = '';
$data['title'] = 'Rimi Classified - Sign up';
$this->load->view('templates/header1', $data);
$this->load->view('sign-up', $data);
$this->load->view('templates/footer', $data);
}
public function get_cities()
{
$state_id = $this->input->post('state');
echo $this->city_model->get_cities($state_id);
}
public function get_locations()
{
$city_id = $this->input->post('cities');
echo $this->location_model->get_locations($city_id);
}
}
Replace
$('#city_id').live("change", function () {
with below code
$(document).on("change", "#city_id", function(){
try to add an alert statement in your first ajax call's success function. also add an error function to know that the first AJAX call is completing successfully.
Use the documentation from here to check if you've correctly implemented the AJAX model.
Jquery-Ajax

Codeigniter - MPDF not exporting data

I am new to Codeigniter and want to export data present in my MYSQL database into PDF file using MPDF. The code is as follows:
View:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Export PDF</title>
</head>
<body>
<div id="container">
<h4>Member Data</h4>
<table border="1">
<tr>
<th>group_id</th>
<th>group_name</th>
<th>Archieved</th>
</tr>
<?php
foreach ($member as $rows) {
echo $rows['group_id'];
?>
<tr>
<td><?php echo $rows['group_id'] ?></td>
<td><?php echo $rows['group_name']?></td>
<td><?php echo $rows['archieved'] ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<br> <br>
<a href='<?php echo base_url(); ?>index.php/member_con/topdf'><span style='color:green;'>Export to Pdf</span></a>
</div>
<?php
?>
</body>
</html>
Controller:
<?php
class Member_con extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('member_model');
$this->load->helper('url');
$this->load->library('mpdf');
}
public function index() {
$data['member'] = $this->member_model->alldata();
$this->load->view('member_view', $data);
}
function topdf() {
$this->mpdf->useOnlyCoreFonts = true;
$filename = "VISH";
$data['member'] = $this->member_model->alldata();
$html = $this->load->view('member_view', $data['member'], true);
$this->mpdf->setTitle('Posts');
$this->mpdf->writeHTML($html);
$this->mpdf->output($filename, 'D');
}
}
?>
Model:
<?php
class Member_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function Member_Model() {
parent::Model();
}
function alldata()
{
$this->db->select('*');
$this->db->from('groups');
$this->db->order_by('group_id','ASC');
$getData = $this->db->get();
if($getData->num_rows() > 0)
return $getData->result_array();
else return null;
}
}
?>
with this code, it is giving me a blank PDF file, with only text as 'Member Data' and 'Export as pdf'. I have checked whether it is passing data to view, and yes it is doing so.
But don't know what is the matter with 'foreach' loop. I is printing everything outside 'foreach' loop, but bot the data members. Can anyone please let me know what should I do?
Thanks in advance....
Got the answer. In controller, instead of
$html = $this->load->view('member_view', $data['member'], true);
I used following:
$html = $this->load->view('member_view', $data, true);

Resources