// this is my view part
<?php foreach($employees as $employee) : ?>
<option value="<?php echo $employee->first_name ?>">First name</option>
<?php endforeach; ?>
// employee_id is Salary table Foreign key
First you need to get the data:
In your controller you call the function in the model and and send the data to the view:
$data['employees '] = $this->model_name->get_employees();
$this->load->view('view_name',$data);
In the model you execute the sql and send back the data to the controller:
public function get_employees ()
{
$this-> db ->select('*');
$this-> db ->from('employees');
$query = $this->db->get();
return $query->result_array();
}
in the view:
<select name="select_name">
<?php
foreach($employees as $employee)
{
echo '<option value="'.$employee->first_name .'">'.$employee->first_name .'</option>';
}
?>
</select>
Related
This is the structure of my table "task"
projectname
employee
clientname
task
Dependencies are as follows
One project has multiple task
One project has multiple employees
I need to create a dropdown list when user selects a particular project tasks relevant to them will automatically load to the next dropdown list. In this situation I do not need primary and foriegn key relationship. Any help would be really appreciated
This is my controller
public function Task(){
$data['cname'] = $this->welcome4->show_students3();
$data['projects'] = $this->welcome4->show_students();
$data['employee'] = $this->welcome4->show_students2();
$this->load->view('template/navigation');
$this->load->view('template/sideNav');
$this->load->view('template/header');
$this->load->view('Task',$data);
$this->load->view('template/footer');
}
This is my model
function show_students2(){
$query = $this->db->get('employee');
$query_result = $query->result();
return $query_result;
}
function show_students3(){
$query = $this->db->get('clientdetails');
$query_result = $query->result();
return $query_result;
}
function show_students4(){
$query = $this->db->get('task');
$query_result = $query->result();
return $query_result;
}
This is my view
<div class="form-group">
<label>Select Project</label>
</div>
<div class="form-group">
<select name="projectname" class="input form-control">
<option value="none" selected="selected">Select Project</option>
<?php foreach($projects as $s):?>
<option value="<?php echo $s->projectname?>"><?php echo $s->projectname?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Select Client</label>
<select name="cname" class="input form-control">
<option value="none" selected="selected">Select client</option>
<?php foreach($cname as $s):?>
<option value="<?php echo $s->cname?>"><?php echo $s->cname?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label>Select Employee</label>
</div>
<div class="form-group">
<select name="employee" class="input form-control">
<option value="none" selected="selected">Select Employee</option>
<?php foreach($employee as $s):?>
<option value="<?php echo $s->employee?>"><?php echo $s->employee?></option>
<?php endforeach;?>
</select>
</div>
This loads all projects, clients, employee in the database.But now I want when project is selected in the first drodown, second dropdown should show only relevant clients and employees to it. Not all of them
Lets consider this will be your Projects select box, in which you are loading data dynamically on page load with php.
<select name="projectname" id="projectname"></select>
Tasks select box.
<select name="tasks" id="tasks"></select>
Inside your controller add below mentioned code. This function will be used for get data from ajax call.
public function getTasks() {
$Id = $this->input->post('project_id');
if ($Id):
$this->load->model('Task_model', 'Tasks', TRUE);
$data = $this->Tasks->getProjectTasks($Id);
if ($data):
$result['status'] = true;
$result['records'] = $data;
endif;
else:
$result['status'] = false;
endif;
echo json_encode($fdata);
}
Inside model please add this function. Which will fetch data from DB
ans pass it to controller back.
public function getProjectTasks($id = null) {
if ($id):
$this->db->select('id,task');
$this->db->where('project_id', $id);
$this->db->where('status', TRUE);
$query = $this->db->get('tasks');
$records = $query->result();
if ($records):
return $records;
else:
return false;
endif;
else:
return false;
endif;
}
And finally in you View file please add below function.
$(document).on('#projectname', 'change', function() {
var projectId = $(this).val();
$.ajax({
type: 'POST',
url: 'URL',
data: {project_id: projectId},
dataType: "json",
beforeSend: function() {
$('#tasks')
.empty()
.append('<option selected="selected">Select Task</option>');
},
success: function(data) {
if (data.status) {
$.each(data.records, function(i, item) {
$('#tasks').append($('<option>', {
value: item.value,
text: item.text
}
));
});
} else {
$('#tasks')
.empty()
.append('<option selected="selected">No Task available</option>');
}
}
});
});
i have two tables in my database one is requirements another one is users..i have those user names in my requirements form with select box..how to store that user names id in database..
This is my view:
<div class="form-group">
<label>Choose Vendor</label>
<select class="form-control" multiple class="form-control" data-placeholder="user name" name="user_id[]" >
<option value="0"></option>
<?php foreach($user as $rows) { ?>
<option value="<?php echo $rows->user_id?>"><?php echo ucfirst($rows->first_name)?></option>
<?php } ?>
</select>
</div>
This is my controller:
public function requirement()
{
$data["msg"]="";
$this->load->model('RequirementModel');
$data['user']=$this->RequirementModel->getusers();
if($this->input->post())
{
$this->RequirementModel->add_requirement($this->input->post());
redirect(base_url('index.php/Login/dashboard'));
}
$this->load->view('Requirements/requirements',$data);
}
This is my model:
function getusers()
{
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
//echo $this->db->last_query();
return $query->result();
}
model add_requirement:
public function add_requirement($data)
{
$data=array('role_name'=>$post['role_name'],'vacancies'=>$post['vacancies'],'experience'=>$post['experience'],'jd'=>$post['jd'],'hiring_contact_name'=>$post['hiring_contact_name'],'hiring_contact_number'=>$post['hiring_contact_number'],'user_id'=>$user_id);
$this->db->insert('requirements', $data);
}
how to do this?
Thank you
Change your function add_requirement like below and try;
function add_requirement($data)
{
if(isset($data['user_id']) && !empty($data['user_id'])) {
$data['user_id'] = implode(',',$user_id);
}
$data=array('role_name'=>$post['role_name'],'vacancies'=>$post['vacancies'],'experience'=>$post['experience'],'jd'=>$post['jd'],'hiring_contact_name'=>$post['hiring_contact_name'],'hiring_contact_number'=>$post['hiring_contact_number']);
$this->db->insert('requirements', $data);
}
}
I want to make a search form in Codeigniter using two fields.
But I'm confused about making the controller and model.
View:
echo form_open("home/pencarian","class='form-inline navbar-search'");
<input id="srchFld" class="srchTxt" name="keyword" type="text" value=" echo $this->session->userdata("keyword");" />
<select class="srchTxt" name="kategori">
<option>All</option>
foreach ($kat as $kategori){
$id = $kategori['id'];
$nama_kategori = $kategori['nama_kategori'];
$created_at = $kategori['created_at'];
<option value=" echo $id; ?>"><?php echo $nama_kategori; </option>
}
</select>
echo form_submit('action', 'Cari', "class='btn btn-primary'");
echo form_close();
How do I make the controller and model?
Model example_model.php
public function search($search_txt, $search_category) {
$this->load->database();
$sql = "select * from search_table where search_field = ? and search_category = ?;";
$query = $this->db->query($sql, array($search_txt, $search_category));
return $query->result();
}
Controller home
public function pencarian() {
$this->load->model('example_model', 'm');
$search_txt = $this->input->post('srchFld');
$search_category = $this->input->post('kategori');
$data['search_result'] = $this->m->search($search_txt, $search_category);
$this->load->view('example_view', $data);
}
View example_view
<?php foreach($search_result as $r): ?>
<div><?php echo $r->field1 . ' ' . $r->field2; ?></div>
<?php endforeach; ?>
I am new with codeigniter.I want to make a select dorpdown that gets its value and title from database.I tried some codes but it did not work.There are my codes:
Model
function get_sec_info(){
$records=$this->db->query('SELECT sec_id,name FROM section');
if($records->num_rows() > 0)
return $records->result();}
Controller
function sec_ifo(){
$data['rec']=$this->mymodel->get_sec_info();
$this->load->view('article',$data);}
View
<select name="section">
<?php foreach($rec as $row) {?>
<option value="<?php echo $row->sec_id?>"><?php echo $row->name ?></option>"
<?php } ?>
It does not show any error and any option to show
In the controller you set "red" $data['red'] and in the view you access "rec" foreach($rec
Model:
function get_sec_info(){
$this->db->select('sec_id,name');
$records = $this->db->get('section');
return $records->result();
}
Controller:
function sec_ifo(){
$this->load->model('mymodel');
$this->data['red'] = $this->mymodel->get_sec_info();
$this->load->view('article',$this->data);
}
View:
<select name="section">
<?php foreach($red as $row) { ?>
<option value="<?php echo $row->sec_id; ?>"><?php echo $row->name; ?></option>
<?php } ?>
Model
public function getClasse() {
$query(`enter code here`);
$result = $this->db->query($query)->result_array();
foreach ($result as $key => $rows) {
$resultado[] = $rows['DescricaoClasse'];
}
return $resultado;
}
Controller:
public function getClasse() {
$this->load->model('Decisao_monocratica_model');
return $this->Decisao_monocratica_model->getClasse();
}
View
<select id="ClasseProcesso" class="input-xlarge" name="classeProcesso">
<option value="0">Todos os Tipos</option>
<? foreach ($classeProcesso as $key => $classe) { ?>
<option value="<? echo $classe ?>"><? echo $classe ?></option>
<? } ?>
</select>
I am new to CI, I've been working on this for hours with no success, please help! I am trying to generate user profiles by retrieving information from a database and displaying it in the view (using the USERID). I am getting both an Undefined Variable Error and Trying to Get Property Out of Non-Object error. Here is my code:
Model:
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
return $query->result();
}
Controller:
public function profile()
{
$data['query'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $user);
$this->load->view('footer');
}
View:
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $data->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $data->occupation;?>
</div>
Thanks so much in advance, I feel like I have tried everything!
Hi as new to CI you will face some problems but later you will enjoy it here are some simple stuff you are doing wrong
Model
public function my_data()
{
$userid = $this->session->userdata('userid');
$data = array();
$this->db->select('*');
$this->db->from('user_profile');
$this->db->where('userid', $userid);
$query = $this->db->get();
//this will return multiple rows or object of arrays
//return $query->result();
// you need to send only single row
return $query->row();
}
Controller
public function profile()
{
// in data array key name should be same which you will pass to view
$data['row'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
View
<div class="control-group">
<i class="icon-user"></i>
Name: <?php echo $row->name; ?>
</div>
<div class="control-group">
<i class="icon-home"></i>
Location: <?php echo $row->location;?>
</div>
<div class="control-group">
<i class="icon-briefcase"></i>
Occupation: <?php echo $row->occupation;?>
</div>
for more information please read CI user guide you will understand so many stuff from there CI user guide
here change $user to $data
public function profile()
{
$data['result'] = $this->user_model->my_data();
$this->load->view('header_loggedin');
$this->load->view('sidebar_left');
$this->load->view('user/user_profile', $data);
$this->load->view('footer');
}
then if your query returns only one row , you can use
return $query->row_array();
instead of
return $query->result();
in view you can now use
Name: <?php echo $result['name']; ?>
Location: <?php echo $result['location'];?>
Occupation: <?php echo $result['occupation'];?>