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
Related
I am learning CodeIgniter; in my program I am trying to populate a dropdown from mysql database but I seemed to be doing something wrong.
Please find below what I tried:
Model
In my model I tried to use different select methods I found online but all failed so I stick with the below method and still failed.
function getAllVendors()
{
$this->db->select('DISTINCT SUBSTRING(product_code,1,3) as vendor')
->order_by('vendor');
$q = $this->db->get('tec_sale_items');
if ($q->num_rows() > 0) {
foreach (($q->result()) as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Controller
public function allVendors(){
$this->data['vendors'] = $this->vendors_sales_model->getAllVendors();
$this->page_construct('reports/vendors_sales_details', $this->data);
}
View
<select class="form-control">
<option value="">All</option>
<?php
foreach($vendors as $ven)
{
echo '<option value="'.$ven['vendor'].'">'.$ven['vendor'].'</option>';
}
?>
</select>
Could someone please point me to what I am doing wrong.
As I see you might be forget to call view into your controller. Load view in the controller $this->load->view("your view file name") and pass the data you got from your database.
First make sure that you are getting proper data from model. And as I see your are using current object of class $this->data['vendors'] so you have to use same in the code
<select class="form-control">
<option value="">All</option> <?php
foreach($this->data['vendors'] as $ven)
{
echo '<option value="'.$ven['vendor'].'">'.$ven['vendor'].'</option>';
}
?>
</select>
May be it's helpful 🙂.
And you should use ci4 it's 3rd version of ci and it's no longer maintained. For better security and performance change php server 7.4 and above.
<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>';
}
?>
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);
Let say we are loading two or more views in the same class method like so:
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');
and you decide to create a variable inside the head view ($cat_name) like this example:
<?php
foreach ($categories as $key => $value) {
$selected = FALSE;
if ($this->router->class == 'category' && $this->router->method == 'id' && $this->uri->segment(3) == $value->id) {
$cat_name = $value->name;
$selected = TRUE;
}
?>
<option value="<?= $value->id; ?>" <?= ($selected ? 'selected' : ''); ?>><?= $value->name; ?></option>
<?php } ?>
This needed a loop to get that variable.
I want to pass that variable ($cat_name) to the next view without redoing the loop, that is just a waste.
what I am trying to achieve is minimizing the number of loops.
instead of loading all of that in controller load it in your view
create new file, let say template
$this->load->view('template',$variable);
and in your template
//do the loop here
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer')
You need to create model for generating selects if You want to save MVC in Your project. I know, that's looks strange, but it will help You many times after
Controller
// load model
$this->load->model('myselect_model');
// get array with marked element
$select_data = $this->myselect_model->setSelected($categories, $this->router->class, $this->router->method, $this->uri->segment(3));
// get filled html
$my_html_select = $this->load->view('select_tpl',array('select'=>$select_data),TRUE);
// use it at any controller
$this->load->vars(array('my_select'=>$my_html_select));
// some views
$this->load->view('header');
$this->load->view('body');
$this->load->view('footer');
Model 'myselect_model'
function setSelected($items, $uri_controller,$uri_method, $uri_value){
// createing temp array for our list
$tmp = array();
foreach($items as $key=>$value){
// appending item
$tmp['options'][$key] = $value;
if ($uri_controller == 'category' && $uri_method == 'id' && $uri_value == $value->id)
{
// saving selected for any reason to use after
$tmp['selected'] = array('name'=>$value->name, 'id'=>$value->id);
// marking this item as selected
$tmp['options'][$key]['selected'] = TRUE;
}
}
// returning completed array
return $tmp;
}
View 'select_tpl'
<?php if(!empty($select)){?>
<select>
<?php foreach($select['options'] as $option){?>
<option value="<?=$option->id?>"<?=(isset($option['selected']) && $option['selected']==TRUE ? " selected=\"selected\"" : "")?>><?=$option->name?></option>
<?php }
</select>
<?php } ?>
views/header
<body><?=$my_select?><i>template here</i>
views/body
<p>some html here and our select goes here-> <?=$my_select?></p>
You could try like this:
Create a model like so:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Custom_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
function printSelect(){
// Do some logic and looping here
$html = "<select><option>...</option></select>";
return $html
}
}
Then call that model from your views like so..
$this->custom_model->printSelect();
Remember to load the model first.
$this->load->model('path/to/your/model/folder/custom_model');
This way, every time you want to print your you can simply call that method from your view.
I hope this helps.
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>