I have two select boxes, if I select an option, it will give me results according to the options and then the results will display in the 2nd select box. How can I do this?
I think, I have to do a onchange function but I don't know how can I get data from controller in JavaScript .
class StockInController extends MY_Controller {
function index(){
$data['main_content'] = 'stockInView';
$this->load->model('supplierModel');
$query = $this->supplierModel->getAllSuppliers();
if ($query){
$data['records1'] = $query;
}
$this->load->model('categoryModel');
$query = $this->categoryModel->getAllCategories();
if ($query){
$data['records2'] = $query;
}
$this->load->model('itemsModel');
$query = $this->itemsModel->getAllItems();
if ($query){
$data['records3'] = $query;
}
$this->load->view('dashboardTemplate/template',$data);
}
function addStockIn(){
//getting parameters from view
$data = array(
'item_name' => $this->input->post('item_name'),
'cat_id' => $this->input->post('cat_id')
);
//$is_ajax = $this->input->post('ajax'); //or use this line
//$this->input->is_ajax_request();
$result = array();
$this->load->model('stockInModel');
$query = $this->stockInModel->addStocktoDB($data);
if ($query){ //&& any other condition
$result['res'] = 1;//process successful - replace 1 with any message
} else {
$result['res'] = 0;//process failed - replace 0 with any message
}
echo json_encode($result);//at the end of the function.
}
}
Here is the View:
<div class="control-group">
<label for="selsear" class="control-label">Select a Category</label>
<div class="controls">
<select name="select1" id="seslsear" class='cho'>
<option id = "y" value="0">--Select--</option>
<?php foreach($records2 as $r) { ?>
<option value="<?=$r->cat_id?>"><?=$r->cat_name?></option>
<?php } ?>
</select>
</div>
</div>
<div class="control-group">
<label for="selsear" class="control-label">Select an item</label>
<div class="controls">
<select name="select1" id="selsear" class='cho'>
<option id = "y" value="0">--Select--</option>
<?php foreach($records3 as $r) { ?>
<option value="<?=$r->item_id?>"><?=$r->item_name?></option>
<?php } ?>
</select>
</div>
</div>
You will have to write a function on-change of select box & feed data in success of ajax
$('#selsear').change(function() {
var form_data = {
item_name: $('#item_name').val(),
cat_id: $('#selsear').val()
};
$.ajax({
url:'controller_name/function_name',
data:form_data,
datatype:'application/json',
success:function(data){
$.each(data,function(i){
htmlString="<option value='"+data[i]['item_id']+"'>'"+data[i]['item_name']+"'</option>"
});
$("#selsear").html(htmlString);
}
});
The controller function name is the name of function which retrieves data from controller.
I hope this will help you!
Related
Hi any one help me how to get data in drop down from database in codeigniter.below is my code it is not work working. The below code is not working properly the drop down not appearing when click please help me to get data. I am doing it in codeigniter. I want to get data from database in dropdown. I think the code is perfect am new to codeigniter the below code is not working properly the drop down not appearing when click please help me to get data. I am doing it in codeigniter. I want to get data from database in dropdown. I think the code is perfect am new to codeigniter
Model:
public function getdepartment() {
$query = $this->db->query("select department from object_data");
$result = $this->executeSelectQuery($query);
return $result;
}
View:
<div class="col-md-4">
<h4> Courses:</h4>
<?php $dp = $this->base_model->getdepartment(); ?>
</div>
<div class="col-md-8">
<select name="report_type" class="form-control">
<?php foreach($dp as $row) {
echo '<option value="'.$row->department.'">'.$row->department.'</option>';
}?>
</select>
</div>
Controller:
public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}
the above code is not working properly the drop down not appearing when click please help me to get data
Model
public function getdepartment() {
$query = $this->db->query("select department from object_data");
return $query->result();
}
controller
public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}
views
<div class="col-md-4">
<h4> Courses:</h4>
<?php $dp = $this->base_model->getdepartment(); ?>
</div>
<div class="col-md-8">
<select name="report_type" class="form-control">
<?php foreach($dprtmnt as $row) {
echo '<option value="'.$row->department.'">'.$row-
>department.'</option>';
}?>
</select>
</div>
you should try codeigniter own methods it is the good way for practice.
Model :
public function getdepartment() {
$query = $this->db->select('department')->from('object_data')->get();
return $query->result();
}
Controller :
public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}
Views :
<select name="report_type" class="form-control">
<?php foreach($dprtmnt as $row) {
echo '<option value="'.$row->department.'">'.$row-
>department.'</option>';
}?>
</select>
I tried to display data via select option based on the field division and successful , but how do I display all the data ?
Controllers
public function dokumen()
{
$divisi = $this->input->post('divisi');
$data=array('title' =>'KOPKAR - Pelanggan',
'divisi' => $this->induk_internal->get_divisi(),
'get_dokumen' => $this->induk_internal->get_dokumen($divisi),
'isi' =>'admin/DCR/daftarindukinternal'
);
$this->load->view('layout/wrapper',$data);
}
Models
function get_dokumen($divisi) {
$this->db->select('*');
$this->db->from('daftar_detail_internal');
$this->db->where('divisi',$divisi);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
Views
<div class="col-md-3">
<select class="selectpicker" style="display: none;" name="divisi" autofocus>
<option value="if click show all data">ALL</option>
<?php foreach($divisi as $row) { ?>
<option value="<?php echo $row->divisi;?>"><?php echo $row->divisi;?>
</option>
<?php } ?>
</select>
I am new to codeigniter and I am working on some project and I happen to need to display a drop down but filled with data from two or more tables. I don't know if join will solve the problem. Here is my view code:
<div class="form-group">
<label class="control-label">Store Name</label>
<select class="form-control" id="store" name="store">
<?php foreach($dataget as $val)
{
?>
<option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option>
<?php
}
foreach($storename as $value)
{
?>
<option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
<?php
}
?>
</select>
</div>
and here is my model:
function get_store()
{
$this->db->select('StoreName');
$this->db->from('store');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
and here is my controller:
public function poedit()
{
$id = $this->uri->segment(3);
$data['dataget'] = $this->wip_model->getByPOId($id);
$datadrop['storename'] = $this->wip_model->get_store();
$this->load->view('header');
$this->load->view('editpo',$data);
$this->load->view('footer');
}
I am working on the edit page of the project can someone please help. Thanks in advance.
In your controller
public function poedit()
{
$id = $this->uri->segment(3);
$data['dataget'] = $this->wip_model->getByPOId($id);
$data['storename'] = $this->wip_model->get_store();
$this->load->view('header');
$this->load->view('editpo',$data);
$this->load->view('footer');
}
To stop duplicate rows you can use in your view
<div class="form-group">
<label class="control-label">Store Name</label>
<select class="form-control" id="store" name="store">
<?php
$store_name = array();
foreach($dataget as $val)
{
$store_name[] = $val->Store;
?>
<option value="<?php echo $val->Store;?>"><?php echo $val->Store;?></option>
<?php
}
foreach($storename as $value)
{
if(!in_array($value['StoreName'],$store_name))//it will stop duplication of rows...
{
?>
<option value="<?php echo $value['StoreName'];?>"><?php echo $value['StoreName'];?></option>
<?php
}
}
?>
</select>
</div>
Well, looks like you put incorrect view variable
$datadrop['storename'] = $this->wip_model->get_store();
Should be changed to
$data['storename'] = $this->wip_model->get_store();
And I'd recommend to merge 2 arrays ($dataget/$storename) into one in controller, then use one foreach in the view, the code must be better
I want to select an option of product categories from dropdown menu and show products that have that specific category.
Here is the form part from my view:
<?php $attributes = array('method'=>"POST", "class" => "myc", "id" => "myc", "name" => "dropdwn");
echo form_open_multipart('frontend/home/display', $attributes); ?>
<div class="form-group">
<div class="col-sm-9">
<select class="form-control" name="category" onchange="this.form.submit();" />
<option value="" <?php echo set_select('category', 'zero', TRUE); ?>>Categories...</option>
<option value="phone" >Phones</option>
<option value="laptops">Laptops</option>
<option value="accessories" >Accessories</option>
</select>
</div>
</div>
<?php echo form_close(); ?>
As you can see I get the option from dropdown through onchange="this.form.submit();
(if it's not a good idea please suggest other way to do, I just didn't want to use ajax, as I'm not so good at it yet, anyhow suggest what seems better).
Then in my controller I get the option and convert it to array, to use it in my model.
controller part:
public function display($sort_by='product_id', $sort_order='asc', $offset = 0)
{
$this->load->model('model_product');
$selected = implode(" ", $this->input->post());
//var_dump($selected);
$results = $this->model_product->fetch($selected, $limit, $offset, $sort_by, $sort_order);
$data['products'] = $results['rows'];
$data['num_results'] = $results['num_rows'];
//and then goes pagination part, I guess no meaning posting it, as it works fine.
}
My model:
function fetch($selected, $limit, $offset, $sort_by, $sort_order)
{
$sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
$sort_columns = array('product_id', 'name', 'description', 'category', 'country', 'price');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'product_id';
//actual results query
$q = $this->db->select('product_id, name, description, category, country, price, img_name, thumb_name')
->from('products')
->where('category', $selected)
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$ret['rows'] = $q->get()->result();
var_dump($ret['rows']);
die;
//count query
$q = $this->db->select('COUNT(*) as count', FALSE)
->from('products');
$tmp = $q->get()->result();
$ret['num_rows'] = $tmp[0]->count;
return $ret;
}
Here I get no results, probobly because where clause returns no result.
However if I change the where clause such as ->where('category', 'phones') it shows only phones. So how can I pass selected value to the query correctly?
i wil help you.
View file
<div class="box-header">
<h3 class="box-title">Enter Category Details</h3>
</div>
<?php
echo validation_errors(); $attributes = array('id' => 'formCategory','name'=>'formCategory');
?>
<?php echo form_open_multipart(base_url().'moderator/B2BCategory/addcategory'); ?>
<div class="box-body">
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label for="txtcatname">Title of Category :</label>
<input type="text" name="txtcatname" class="form-control" id="txtcatname" placeholder="Category Name " required="required">
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label for="categorysection">Section of the Category :</label>
<select class="form-control" name="categorysection" required>
<option value="" >----Select------</option>
<option value="1">Laptop</option>
<option value="2">Phone</option>
<option value="3">Accessories</option>
</select>
<input type="submit" name="selsub" class="btn btn-primary">
</div>
</div>
</div>
Controller
public function addcategory() {
$this->load->helper(array('form', 'url'));
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
if ($this->input->post('selsub')) {
$data = array('ctg_name' => $this->input->post('categorysection'));
$this->b2bcategory_model->form_insert($data);
}
In Model
public function form_insert($data){
$this->db->insert('jil_category', $data);
}
To display output from table,just use
public function viewall()
{
$this->db -> select('*');
$this -> db -> from('jil_category');
$query = $this -> db -> get();
return $query->result();
}
From
$selected = implode(" ", $this->input->post());
To
$selected = $this->input->post('category');
By the way, echo & var_dump() would be very useful in debugging your controller and/or model.
I am new to CI. I need to Change some dropdown list according to another dropdown.
My controller code:
public function index()
{
$data = array();
$data['from_list'] = $this->from->list_from();
$data['to_list'] = $this->to->list_to();
$data['fromwhere_list'] = $this->fromwhere->list_fromwhere();
//$data['fromwhere_from_list'] = $this->fromwhere->list_fromwhere_from();
$data['towhere_list'] = $this->towhere->list_towhere();
$data['header'] = array('view'=>'header','data'=>array());
$data['main_content'] = array('view'=>'home','data'=>array());
$data['footer'] = array('view'=>'','data'=>array());
$this->load->view('template',$data);
//$this->load->view('home');
}
model code
function list_fromwhere()
{
$this->db->select('fromwhere_id,from_id,from_from_name');
$this->db->from('mt_from_from');
$query = $this->db->get();
return $query->result();
}
function list_from()
{
$this->db->select('from_id,from_name');
$this->db->from('mt_from');
$query = $this->db->get();
return $query->result();
}
My View code
<select class="" name="from" id="from" style="width: 20%;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;border: none;background: #dfe7eb;color: #999999;height: 38px;line-height: 38px;padding-left: 10px;padding-right: 20px;">
<?php
foreach($from_list as $from_item)
{
?>
<option value="<?php echo $from_item->from_id?>"><?php echo $from_item->from_name?></option>
<?php
}
?>
<!-- <option value="volvo">Airport</option>
<option value="saab">Town</option>
<option value="mercedes">Hotel</option> -->
</select>
<select class="" name="from_place" id="from_place" style="width: 56%;-webkit-border-radius: 3px;-moz-border-radius: 3px;border-radius: 3px;border: none;background: #dfe7eb;color: #999999;height: 38px;line-height: 38px;padding-left: 10px;padding-right: 20px;">
<?php
foreach($from_list as $from_item){
foreach($fromwhere_list as $fromwhere_item)
{
if($fromwhere_item->from_id == $from_item->from_id){
?>
<option value="<?php echo $fromwhere_item->fromwhere_id?>"><?php echo $fromwhere_item->from_from_name?></option>
<?php
}
}
}
?>
</select>
When we select the town, country by from dropdown list (firstone), I need to automatically change from place dropdown (secondone).