I'm having difficulty with display data from the db to dropdown.
This is what I have tried:
Model.php
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
/*
$query = $this->db->get('location');
foreach ($query->result() as $row)
{
echo $row->description;
}*/
$query = $this->db->query('SELECT description FROM location');
foreach ($query->result() as $row)
{
echo $row->description;
}
//echo 'Total Results: ' . $query->num_rows();
}
Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('delivery_model');
}
public function index()
{
$data['title']= 'Warehouse - Delivery';
$this->load->view('include/header',$data);
$this->load->view('include/navbar',$data);
$this->load->view('delivery_view', $data);
$this->load->view('include/sidebar',$data);
$this->load->view('include/footer',$data);
$data['groups'] = $this->delivery_model->getAllGroups();
}
}
View.php
<select class="form-control">
<?php
$data = $this->delivery_model->getAllGroups();
foreach($description as $each)
{ ?><option value="<?php echo $each['description']; ?>"><?php echo $each['description']; ?></option>';
<?php }
?>
</select>
But the results appear on top of my page. It's not appearing on the dropdown list. What am I doing wrong in here? Help is pretty much appreciated. Thanks.
To populate form dropdown in CodeIgniter we are going to use one of the form helper function i.e. form_dropdown() from helper file that will create drop-down field. Also, one can use it to create multi select by passing an array of multiple items.
You should not be calling your model from your view. Instead try calling you model and setting $data['groups']
before you load your views.
Also do not echo the row results in your model unless you want it displayed on your page.
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->model('delivery_model');
}
public function index()
{
$data['title']= 'Warehouse - Delivery';
$data['groups'] = $this->delivery_model->getAllGroups();
$this->load->view('include/header',$data);
$this->load->view('include/navbar',$data);
$this->load->view('delivery_view', $data);
$this->load->view('include/sidebar',$data);
$this->load->view('include/footer',$data);
}
}
Model:
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
/*
$query = $this->db->get('location');
foreach ($query->result() as $row)
{
echo $row->description;
}*/
$query = $this->db->query('SELECT description FROM location');
return $query->result();
//echo 'Total Results: ' . $query->num_rows();
}
View:
<select class="form-control">
<?php
foreach($groups as $row)
{
echo '<option value="'.$row->description.'">'.$row->description.'</option>';
}
?>
</select>
This is what you should do:
Model:
public function __construct()
{
parent::__construct();
}
function getAllGroups()
{
$query = $this->db->query('SELECT description FROM location');
return $this->db->query($query)->result();
}
Controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Delivery_controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('delivery_model');
}
public function index()
{
$data['title']= 'Warehouse - Delivery';
$data['groups'] = $this->delivery_model->getAllGroups();
//I take here a sample view, you can put more view pages here
$this->load->view('include/header',$data);
}
}
View:
<select class="form-control">
<?php foreach($groups as $each){ ?>
<option value="<?php echo $each->description; ?>"><?php echo $each->description; ?></option>';
<?php } ?>
</select>
Codeigniter already has specialized functions that minimize the amount of html that you have to dump in your code:
Model
public function description_pulldown(){
$this->db->from('location');
$query = $this->db->get();
foreach($query->result() as $row ){
//this sets the key to equal the value so that
//the pulldown array lists the same for each
$array[$row->description] = $row->description;
}
return $array;
}
Controller
public function index(){
$data['description_list'] = $this->delivery_model->description_pulldown();
//load all of your view data
$this->load->view('delivery_view', $data);
}
View
echo form_label("Description");
echo form_dropdown('description', $description_list, set_value('description'), $description_list);
If you need to have the view pull up the previous data in the dropdown list, you can do a foreach loop to obtain the previous value of the dropdown from the database ie... $description = $item->description; and in the view, change the 'set_value('description')' to simply '$description.'
Never call a model from a view. It is doable but the again you lose the point of using an MVC in the first place. Call the model from your controller. Get the data and pass the data in to your view.
Use like below.
public function index(){
$data['title']= 'Warehouse - Delivery';
$data['groups'] = $this->delivery_model->getAllGroups();
$this->load->view('include/header',$data);
$this->load->view('include/navbar',$data);
$this->load->view('delivery_view', $data);
$this->load->view('include/sidebar',$data);
$this->load->view('include/footer',$data);
}
In your view, simply loop around the $groups
variable and echo
to your dropdown.
<select class="form-control">
<?php
$i = 0;
while($i < count($groups)){
$val= $groups[$i]['value'];
$des = $groups[$i]['description'];
echo "<option value='$i'>$des</option>";
}
</select>
And your model's function should be,
function getAllGroups(){
$query = $this->db->get('location');
return $query->result_array();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With