I'm trying to add an option from the controller to the view directly .. the moment I get the array data from there with the following code:
public function select_dependent() {
$aData = array(); //Variable para pasar a la vista
$data = $this->input->post('id');
$tipus = $this->input->post('tipus');
if($tipus == "modelo") {
$aData['aModels'] = $this->pedidos_model->get_modelos($data);
}
}
In the view, i have the following code:
<select name="modelo" class="span8" id="modelo">
<?if(isset($aModels)):?>
<?foreach($aModels as $row):?>
<option value="<?=$row['IDPRODUCTOS']?>"><?=$row['NOMBRE']?></option>
<?endforeach;?>
<?endif;?>
</select>
And in the model I have this:
public function get_modelos($valor) {
$oQuery = $this->db->query("dbo.CO_GETPRODUCTO #IDTIPOS ='".$valor."'");
$aResult = $oQuery->result_array();
return $aResult;
}
Can you help me? Thank you!
You may use the Form Helper to generate the select/dropdown element easily:
// In your controller method
$this->load->helper('form');
// ...
if($tipus == "modelo") {
$aData['aModels'] = $this->pedidos_model->get_modelos($data);
$aData['selected'] = 'use_a_value_to_be_selected';
// Load the view and pass the $aData
$this->load->view('blogview', $aData);
}
// In your view
echo form_dropdown('modelo', $aModels, $selected, 'class="span8" id="modelo"');
Your question is not so clear about the problem so can't be more specific but you got the idea.
Related
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];
}?>
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>
//model
function shop_dropdown()
{
$this->db->select('shop');
$this->db->from('shop');
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name'];
}
return $data;
}
controller//
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop['select_options'] = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
view//
<?php
echo form_dropdown('shop', $select_options);
?>
this is not not working.please help me creating a drop downlist from database.if you can write a new code.
thanks in advance
Modify like this
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
and in your view
echo form_dropdown('shop', $shop->option);//option is an value taking form database
that's it.accept answer if it useful for you
I am not sure if you have autoload form helper, if you didn't, you can't use the form_dropdown function unless you load it in the controller. I don't see you load form helper anywhere.
http://codeigniter.com/user_guide/helpers/form_helper.html
you are selecting 'shop' column in your model.
I think your model should be like this
function shop_dropdown()
{
$this->db->select('id,name'); //column names you want to select, can be optional if you want to select all columns.
$this->db->from('shop'); //table name, required
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name']; //make sure 'id' and 'name' ,columns are present in table
}
return $data;
}
And I hope you have edited application/config/databse.php