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).
Related
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 display search result from multiple dropdown selection. I have 4 dropdowns. I have one submit button, but I'm still confused how to display search result when I click that submit button. I want to display "institution" and "address" column from "courseplace" table, based on query in mymodel.php below, and display 'No Results' when no match. I want to display search result like on http://www.bhinneka.com/category/notebook___laptop_gaming.aspx (institution image, institution name, and address).
Do I need new page to display search result?
I have tried so many ways but still failed :(
Thank you very much for the answer!
Below is my code (using Codeigniter) :
Controller.php
class Controller extends CI_Controller
{
public function index()
{
$this->load->view('home');
}
}
mymodel.php
class Mymodel extends CI_Model
{
public function getinstitution()
{
include('application/views/home.php');
$institution = $_POST['DropdownInstitution'];
$course = $_POST['DropdownCourse'];
$location = $_POST['DropdownLocation'];
$price = $_POST['DropdownPrice'];
$this->db->select('*');
$this->db->from('courseplace');
$this->db->where('1 = 1');
if ($institution != '') {
$this->db->where('institution', $institution);
}
if ($course != '') {
$this->db->where('course', $course);
}
if ($location != '') {
$this->db->where('location', $location);
}
if ($price != '') {
$this->db->where('price', $price);
}
$data = $this->db->get()->result_array();
return $data;
}
}
home.php
<div class="MenuSearching" id="Searching">
<form action="" method="post">
<div class="Menu1" id="Menu1">
<p> Lembaga</p>
<div style="margin-top:-20px;margin-left:-2px;">
<select name="DropdownInstitution" autofocus required id="DropdownInstitution">
<option value="no" selected="selected">---Choose one---</option>
<option value="FlashCom">FlashCom</option>
<option value="InterNusa">InterNusa</option>
<option value="HexaCompare">HexaCompare</option>
</select>
</div>
</div>
<div class="Menu2" id="Menu2">
<p> Paket Kursus</p>
<div style="margin-top:-20px;margin-left:-2px;">
<select name="DropdownCourse" autofocus required id="DropdownCourse">
<option value="no" selected="selected">---Choose one---</option>
<option value="web">Kursus Web Design</option>
<option value="flash">Kursus Flash Animation</option>
<option value="marketing">Kursus Internet Marketing</option>
</select>
</div>
</div>
<div class="Menu3" id="Menu3">
<p> Lokasi</p>
<div style="margin-top:-20px;margin-left:-3px;">
<select name="DropdownLocation" autofocus required id="DropdownLocation">
<option value="no" selected="selected">---Choose one---</option>
<option value="timur">Surabaya Timur</option>
<option value="barat">Surabaya Barat</option>
<option value="utara">Surabaya Utara</option>
<option value="selatan">Surabaya Selatan</option>
</select>
</div>
</div>
<div class="Menu4" id="Menu4">
<p> Harga</p>
<div style="margin-top:-20px;margin-left:-2px;">
<select name="DropdownPrice" autofocus required id="DropdownPrice">
<option value="no" selected="selected">---Choose one---</option>
<option value="harga1"> kurang dari Rp.750.000</option>
<option value="harga2">Rp.750.000 - Rp.1.500.000</option>
<option value="harga3">Rp.1.500.000 - Rp.2.500.000</option>
<option value="harga4">lebih dari Rp.2.500.000</option>
</select>
</div>
</div>
<div>
<input name="submitbutton" type="submit" id="submitbutton" formmethod="POST" value="Submit">
</div>
</form>
You can use jQuery + Ajax to solve this non-refresh problem.
In you home.php file, send request to your controller and after model return data to controller, data would be append on your webpage.
I would suggest you go through codeigniter demos of model, view and controller.
Please check below code
Controller.php
//make a call to getinstitution function
//load our Mymodel class defined in mymodel.php
//call your view home
class Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->model('Mymodel');
}
public function index()
{
$params = array(
'DropdownInstitution' => $this->input->post('DropdownInstitution'),
'DropdownCourse' => $this->input->post('DropdownCourse'),
'DropdownLocation' => $this->input->post('DropdownLocation'),
'DropdownPrice' => $this->input->post('DropdownPrice'),
);
$data['result'] = $this->mymodel->getinstitution($params);
$this->load->view('home',$data);
}
}
mymodel.php
//No need to include('application/views/home.php');//
class Mymodel extends CI_Model
{
public function getinstitution($params)
{
$institution = $params['DropdownInstitution'];
$course = $params['DropdownCourse'];
$location = $params['DropdownLocation'];
$price = $params['DropdownPrice'];
$this->db->select('*');
$this->db->from('courseplace');
$this->db->where('1 = 1');
if ($institution != '') {
$this->db->where('institution', $institution);
}
if ($course != '') {
$this->db->where('course', $course);
}
if ($location != '') {
$this->db->where('location', $location);
}
if ($price != '') {
$this->db->where('price', $price);
}
$data = $this->db->get()->result_array();
return $data;
}
}
home.php
Please add below code to view home.php
<div>
<?php if(!empty($result)){
foreach($result as $value){ ?>
<?php echo $value ?>
<?php }
}else{
echo 'No results';
} ?>
</div><!--Result div-->
I have list of record in my index page. Now i want to search a particular record/records by choosing the category (e.g phone No, email etc).How can i do this? help..
Here is my view:
<?php
$attributes = array('class'=>'searchform', 'id'=>'searchform');
echo form_open('crud/searchCrud', $attributes);?>
<div class="formelements">
<div class="formlbl">
Search:
</div>
<div class="forminput">
<input type="text" name="searchon" id ="searchon"/>
</div>
<div class="searchtyp">
<select class="searchby" name="searchby">
<option value="0">---Select--- </option>
<option value="name">Name</option>
<option value="email">Email</option>
<option value="phone">Phone</option>
</select>
</div>
<div class="searchinput">
<input type="submit" name="search" value="Search" />
</div>
</div>
<?php echo form_close();?>
Here is My controller:
public function searchCrud()
{
$records = $this->crud_mdl->searchCrud();
foreach ($records as $record) {
echo $record->name;
}
}
Here is My Model:
public function searchCrud()
{
$searchby = $this->input->post('searchby');
$searchon = $this->input->post('searchon');
$this->db->get('test')->result();
return $this->db->like($searchon, $searchby);
}
1) You should never access POST data directly in the Model.
2) Always collect them in Controller and then pass it to the Model.
3) Also always try to create your Model functions, re-usable.
Modified your code a bit. Try running it.
Controller:
public function searchCrud()
{
$searchby = $this->input->post('searchby');
$searchon = $this->input->post('searchon');
$records = $this->crud_mdl->searchCrud($searchby, $searchon);
foreach ($records as $record)
{
echo $record->name;
}
}
Model:
public function searchCrud($searchby, $searchon)
{
$this->db->like($searchon, $searchby);
$query = $this->db->get('test');
if($query->num_rows() > 0)
return $query->result();
else
return FALSE;
}
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!