Display Data from Database to Dropdown Codeigniter

display data from database to dropdown CodeIgniter

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>

How to get data on database and display in select dropdown in view (Codeigniter)

Try this:

Model:

function getbanklist() {
$this->db->select("id,bank");
$this->db->from('bank');
$query = $this->db->get();
return $query;
}

In your view:

<select name="bank">
<?php foreach($bankdata->result() as $bank){ ?>
<option value="<?php echo $bank->id ?>"><?php echo $bank->bank ?></option>
<?php } ?>
</select>

how to get data in dropdown from database in CodeIgniter

Model

 public function getdepartment() {
$query = $this->db->query("select department from object_data");
return $query->result();
}

controller

 public function employe() {
$data['title'] = 'title';
$this->load->model('base_model');
$data['groups'] = $this->base_model->getCourseAll();
$data['dprtmnt'] = $this->base_model->getdepartment();
$this->load->view('employe', $data);
}

views

<div class="col-md-4">
<h4> Courses:</h4>
<?php $dp = $this->base_model->getdepartment(); ?>
</div>
<div class="col-md-8">
<select name="report_type" class="form-control">
<?php foreach($dprtmnt as $row) {
echo '<option value="'.$row->department.'">'.$row-
>department.'</option>';
}?>
</select>
</div>

Getting a drop down list from a database in CodeIgniter

There are several issue found there. Make changes as below

 function __construct(){ 
parent::__construct();
$this->load->helper('url');
$this->load_model('Site_model');
$this->load->database();
}
public function index(){
$this->load->helper('form');
$data['groups'] = $this->site_model->getAllGroups();
$this->load->view('supplier_add',$data);
}

Finally model

function getAllGroups(){
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}

and now test

How to fetch data from database and display it in a drop down list using Codeigniter

Hope this will help you :

Pass $data['hostels'] in your view as given below :

First your controller assign_hostel should be like this :

public function assign_hostel() 
{
$inner_page_title = 'Assign Hostel';
$this->admin_header('Assign Hostel', $inner_page_title);
$data['hostels'] = $this->hostel_model->get_hostel();
$this->load->view('admin/hostel/assign_hostel' ,$data);
$this->admin_footer();
}

Second your view should be like this :

<div class="form-group">
<label class="form-control-label">Select Hostel</label>
<select class="form-control" name="state_of_origin" required>
<option value="">-Select-</option>
<?php if ( !empty($hostels)) {
foreach ($hostels as $hostel ) { ?>
<option value="<?=$hostel->hostel_name; ?>"><?=$hostel->hostel_name;?></option>
<?php } }?>
</select>
</div>

For more : https://www.codeigniter.com/user_guide/general/index.html

display data after selecting options from dropdown codeigniter

Separate your view in two views:

First view: named sand_main.php in folder sandbox_v

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<div class="form-group">
<label for="paket">Pilih Paket</label>
<select class="form-control form-control-sm" name="paket" id="paket">
<option selected value="">Pilih Paket</option>
<option value="123">123</option>
<option value="324">566</option>
<option value="555">333</option>
<!-- I've used my own code, for checking the result, you should use your:
<option value="">Pilih Paket</option>
<?php
foreach ($datatour as $dttour)
{
if ($id==$dttour->id)
{
$s='selected';
}
else
{
$s='';
}
?>
<option <?php echo $s ?> value="<?php echo $dttour->id;?>"><?php echo $dttour->paket;?></option>
<?php
}
?>
-->
</select>
</div>

<div id="info"></div>

Where JS should be next:

<script type="text/javascript">
$(function(){

$('#paket').unbind('change');
$('#paket').change(function(){

var opt_sel = $('#paket').val();

$.ajax({
method:"POST",
url:'/index.php/sandbox/s1',
data:{
sel_op:opt_sel
}
}).done(function(a){

$('#info').html(a);
}).fail(function(){

alert("It's an epic fail.");
});
});

})
</script>

Second view: named sand_view_2.php in folder sandbox_v

<div class="row">
<div class="col-md-8" align="left">
<label for="jadwal">Jadwal</label>
<div style="border:1px solid #ccc;text-align:left;background-color:white;">

<?= $j_info; ?>

</div>
</div>
</div>

<div class="kolom" style="border:1px solid #ccc;background-color:white;overflow:auto;" align="center">
<div class="row">
<h3>Detail Paket</h3> <br>

<?= $d_info; ?>

<p></p>
</div>

</div>

Controller: in my example sandbox.php

<?php
class Sandbox extends CI_Controller {

public function __construct() {
parent::__construct();
}

public function index() {
// here you should get your $datatour data

$this->load->view('sandbox_v/sand_main.php', [
'datatour' => $datatour
]);
}

public function s1() {

$sel_val = $this->input->post('sel_op');

/*you can use your DB for getting a description for each value
* in this case you should add in __construct() your model as $this->load->model('your_model);
* or use your DB connection directly here.
*/

/* with your model:
* $jdwal_info = $this->your_model->your_jdwal($sel_val);
* $detail_info = $this->your_model->your_detail($sel_val);
*/

// without. It means that you've got info in the other way, for example, in predefined way:

if ($sel_val == 555) {

$jdwal_info = 'descr 1';
$detail_info = 'detail 1';
} else if ($sel_val == 123 ) {

$jdwal_info = 'descr 123';
$detail_info = 'detail 123';
} else if ($sel_val == 324 ) {

$jdwal_info = 'descr 324';
$detail_info = 'detail 324';
} else {

$jdwal_info = 'descr N\A';
$detail_info = 'detail N\A';
}

$this->load->view('sandbox_v/sand_view_2.php',[
'j_info' => $jdwal_info,
'd_info' => $detail_info
]);
}
}

Model: named your_model.php

<?php
class Your_model extends CI_Model {

function __construct()
{
$this->load->database();
}

public function your_jdwal($a) {

// process of getting result from DB over input variable $a (the name of it doesn't matter)
return $res;
}

public function your_detail($a) {

// process of getting result from DB over input variable $a (the name of it doesn't matter)
return $res;
}

}

Check it. It works for me.

codeigniter - display data from data base in dropdown

Things to consider about your code:

You have an error here url: /mtb/Schedule/get_states/"+country_id, missing double quote at the beggining. Always use a debugging tool, like firebug, it will show you the javascript errors.

Another thing, the code

if($country != NULL){

should be

if(!is_null($country)){

take a look at is_null function.

Another thing, instead of application/x-json json header you should use application/json.

Another thing, the code

echo(json_encode($this->state_model->get_states($country)));

should be

echo(json_encode($this->state_model->get_states($country)));
exit;

otherwise unexpected result might be appended to the final json. Example: CodeIgniter profiling.

Another thing, the code

$countries = array();

if ($query -> result()) {
foreach ($query->result() as $country) {
$countries[$country -> country_id] = $country -> country_name;
}
return $countries;
} else {
return FALSE;
}

should be

$countries = array();

foreach ($query->result() as $country) {
$countries[$country -> country_id] = $country -> country_name;
}
return $countries;

because having no countries and returning FALSE will give you an error in form_dropdown function, take a look inside it. The same goes for get_states function.

This is how much I see in a preliminary look of your code.

Retrieving data from database to dropdownlist using codeigniter

Are you accessing form/drop to generate the view? If you want to access this via form/myform you will have to change myform to include the call to $this->school_model->getAll();

public function myform()
{
$data['title'] = "myform";

$this->load->library('form_validation');
$this->load->model('school_model');

if($this->input->post()){
$sdata['school'] = $this->input->post('school');
$this->school_model->addItem($sdata);
}

$data['groups'] = $this->school_model->getAll();
$this->load->view('myform', $data);
}


Related Topics



Leave a reply



Submit