<select class="selectpicker" multiple data-live-search="true">
<?php
$this->db->select('*');
$this->db->from('candidate_team');
$where = "candidate_id='".$id."' and member_type='Recruiter'";
$this->db->where($where);
$qqs = $this->db->get();
$result = $qqs->result_array();
foreach($result as $row)
{
echo '<option value="'.$row['member_name'].'">'.$row['member_name'].'</option>';
}
?>
</select>
I have a multi-select dropdown. Now, What I want if a value exists in the database then the value already show in select picker. So, How can I do this? Please help me.
Thank You
You need to get DB record in controller and then pass it to view to display with selectpicker
Controller
I'm supposing you've configured database
public function getselectpiker(){
$this->load->model('model_name');
$data['result'] = $this->model_name->get_data($id); // Pass candidate id here
$this->load->view('file_name', $data);
}
Model
public function get_data($id){
return $this->db->get_where('candidate_team', ['candidate_id' => $id, 'member_type' => 'Recruiter'])->result_array();
}
View
<select class="selectpicker" multiple data-live-search="true">
<?php
foreach($result as $row){
echo '<option value="'.$row['member_name'].'">'.$row['member_name'].'</option>';
}
?>
Related
why kategori_nama didn't show in fatalist.
here my controller
function index(){
$data['title']='Portal Database Buku';
$data['dropdown']=$this->m->ambildataKategori('ref_kategori');
$this->load->view('home', $data);
}
my models
function ambildataKategori(){
return $this->db->get('ref_kategori');
}
views
<input name="kategori_id">
<datalist>
<?php while($rows=mysql_fetch_assoc($dropdown)){ ?>
<option value="<?php echo $rows["kategori_nama"];?>">
<?php } ?>
</datalist>
In Codeigniter 4 the helper form_datalist(string $name, string $value, array $options) is available within the form_helper in the same way as form_dropdown().
Its worth flagging though, that the helper helpfully outputs an input type text as well - which you have no control over so may well want to edit that out of the helper!
Hope this will help you :
Note : make sure you have loaded database and model in controller or in autoload.php
Your model method ambildataKategori should be like this
public function ambildataKategori()
{
$query = $this->db->get('ref_kategori');
if ($query->num_rows() > 0 )
{
/*make sure your table has data
print_r($query->result_array());
*/
return $query->result_array();
}
}
Your view should be like this :
<?php
if ( ! empty($dropdown)){ ?>
<datalist>
<?php foreach($dropdown as $item) {?>
<option value="<?php echo $item["kategori_nama"];?>">
<?php }?>
</datalist>
<?php }?>
for reference : https://www.codeigniter.com/user_guide/general/index.html
i want to make my edit is auto select option but when i try is doesnt work. i need my form edit is auto select the data if i click data with id_cms_companies "1" from my table location is auto select and show in the first select option.
and its show the name of table cms_companies
here my code :
<select class='form-control' name='id_cms_companies'>
<option>- Companies -</option>
<?php
$category = DB::table("cms_companies")->get();
foreach($category as $cat)
{
if($cat->name == $row->id_cms_companies)
{
echo "<option value='$cat->name' selected>$cat->name</option>";
}
else
{
echo "<option value='$cat->name'>$cat->name</option>";
}
}
?>
</select>
and here my controller :
public function getEdit($id) {
$data['row'] = locations::find($id);
return view('locations_form',$data);
}
public function postEditSave($id) {
$simpan= array();
$simpan['name']=Request::input('name');
$simpan['id_cms_companies']=Request::input('id_cms_companies');
DB::table('locations')->where('id', $id)->update($simpan);
Session::flash('edit', 'Data berhasil di Edit');
return Redirect::to('locations');
}
here my table :
table locations
and here for my
table cms_companies
You should compare two IDs, no ID with Name
if($cat->name == $row->id_cms_companies) //wrong
if($cat->id == $row->id_cms_companies) //correct
You could use laravel html, it will do it automatically
So you could do something like this:
$category = DB::table("cms_companies")->get()->pluck('name', 'id');
echo Form::select('id_cms_companies', $category, $row->id_cms_companies, ['class' => 'form-control']);
Read more about it here docs
I'm having difficulty with display data from the db to dropdown.
This is what I have tried:
form_model.php
function getEmployee()
{
$this->db->select('username');
$query = $this->db->get('tbl_usrs');
return $query->result();
}
form_controller.php
public function evaluate()
{
$data['employee'] = $this->form_model->getEmployee();
$this->load->view('evaluate_view');
}
evaluate_view.php
<select class="form-control">
<?php
foreach($employee as $row)
{
echo '<option value="'.$row->username.'">'.$row->username.'</option>';
}
?>
</select>
It's giving me an error saying I have an unidentified variable employee in my view file. I've seen problems relating to this but so far all their solutions didn't work for me.
When you load the view you have to send the data like this:
$this->load->view('evaluate_view', $data);
Please help me. i can not use my dataTable properly. what I want to do is
select from table and use thewherefunction. but i cant do it properly.
here is my controller code
public function reporttable ()
{
$zz = array('empnumber' => $this->input->post('empnumber'));
//$this->db->order_by("surname", "asc");
$this->datatables->select('date_auto,particulars,earned_VL,aul_VL,balance_VL,aulx_VL,earned_SL,aul_SL,balance_SL,aulx_SL,date_leave,action_taken')
->from('tblreport')->where($zz);
echo $this->datatables->generate();
}
this is my supposed query:
select * from tblreport where empnumber = (the empnumber in my textbox.)
there, i get a value from a textbox to my view. but it didn't work. i know that is wrong. can you please help me with my problem? thank you.
<p align="center"> <?php echo $this->table->generate();?></p></div>
<?php foreach ($tblemployee as $row){?>
<input type="text" name="empnumber" readonly="readonly" value="<?php echo $row->empnumber;?>"/>
<input type="hidden" name="empnumber" value="<?php echo $row->empnumber;?>"/>
here is my view for guide. thank you.
as Simple you can use
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
Try this :
To make it more simplier.
In model :
public function reporttable ($id){
$this->db->select('*');
$this->db->from('tblreport');
$this->db->where('empnumber', $id);
$query = $this->db->get();
return $query->return_array(); // use this if so want to return many query if not you can also use first_row('array')
}
In controller :
public function function_name (){
$data['variable_name'] = $this->model_name->reporttable($id); // change the model_name by your own model where your function reporttable is located and use the variable_name for your view,
$this->load->view('view_name' , $data); // load the view page you want to display.
}
In Controller
$data['tblemployee'] = $this->model_name->reporttable($id)//assign your data base value to variable
$this->load->view('your view name',$data )
in Model
public function reporttable($id)
{
$query = $this->db->query("select * from tblreport where empnumber = '$id'");
$result = $query->result_array();
return $result; // this will return your data as array
}
In view
<?php
foreach ($tblemployee as $row)
{
echo $row['id];
}?>
I'm stacked on this matter. I have a model that retrieves data from a mysql database
class load_model extends CI_Model{
function __construct(){
parent::__construct();
}
Function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
foreach ($records->result() as $row)
{
$data[$row->SupplierID] = $row->Name;
}
return ($data);
}
}
?>
This model submits value to a function in my controller
public function getSupplier()
{
$this->load->model('load_model');
$data['unit'] = $this->load_model->loadsuppliers();
$this->load->view('SupplierMGT', $data);
}
and I want to display the retrieved data to my view as a combo box. I tried to check if I am able to retrieve database values using echo json_encode($data) and it returns {"unit":{"2":"test","3":"Delta"}} ,
Could you help me with this? I tried using
<?php foreach($unit as $result):
print_r($result);
endforeach;?>
to check if i am able to pass the value but i failed.
Small changes in the model:
function loadsuppliers()
{
$this->db->select('SupplierID, Name');
$records=$this->db->get('supplier');
$data=array();
if($records->num_rows() > 0){
$data = $records->result_array();
}
return ($data);
}
In your view SupplierMGT.php write this:
<select name="" id="" multiple="">
<?php
if(isset($unit) && is_array($unit) && count($unit) > 0){
foreach($unit as $key=>$each){
?>
<option value="<?=$each['SupplierID']?>"><?=$each['Name']?></option>
<?php
}
}
?>
</select>