Option Selected With Codeigniter and Data from Db

Option selected with codeigniter and data from DB

Yes it is. However you have to know which customer is chosen at the moment, and then inside the loop check if the chosen customer_id is the same as the current one:

<?php 
$chosenCustomer_id = 5; //of course don't hardcode it
foreach ($customers as $c):
$selected = $c->customer_id == $chosenCustomer_id ? 'selected' : '';
?>
<option value="<?php echo $c->customer_id;?>" <?php echo $selected; ?>> <?php echo ><?php echo $c->customer_name; ?></option>
<?php endforeach; ?>

Selected as Selected Database value Codeigniter

do this :

<select required name="base_ini_id" id="base_ini_id" class="form-control"> 

<option value="">Select</option>
<?php foreach($base as $value)
{
?>

<option id="emp" class="specialLink" value="<?php echo $value->id;?>"
<?php if($value->id == $user->base_ini_id){echo "selected";} ?>>
<?php echo $value->base_ini_filename;?></option>

<?php } ?>

</select>

codeigniter get data by selection of dropdown

Use $this->input->post('report_type');

How to display selected option get by id when update a data in codeigniter

Change this code

<select name="category_id">
<?php foreach ($cats as $category) { ?>
<option <?php if($category->category_id == "your desired id"){ echo 'selected="selected"'; } ?> value="<?php echo $category->category_id ?>"><?php echo $category->category?> </option>
<?php } ?>
</select>

I actually added a hint for you as your question was not so clear.

You have to put your desired id to match both for selected category

CodeIgniter: Trying to load the option values from database table based on the selection of first select option

There were a couple of issues regarding the code.

For future reference: see the comments on the OP's post

Main issue was with the click handler:

    $("#car_list").on("change",function(){ 
var value = $(this).val();
$.ajax({ url : "welcome/get_data",
type: "post",
data: {"value":value}, //OP originally used single quotes on the value therefore passing a string instead of the actualy variable

success : function(data){
$("#car_model").html(data);
},
});
});

Issues with the controller and model

 public function get_data()
{
$data = $this->PostModel->get_data(); //OP originally passed $value to the model but $value does not exist
$option ="";

if(count($data) > 0){
foreach($data as $d)
{
$option .= "<option value='".$d->Model."' >".$d->Model."</option>";
}
echo $option;
}

}

how to get values from database in codeigniter based on select box value without refreshing page?

Hope this will help you :

Replace

$root_id = $this->input->post('menu_root_id');

with

$root_id = $this->input->post('name');

Your controller's get_menu_rights method should be like this :

public function get_menu_rights()
{
$root_id = $this->input->post('name');
if(! empty($root_id))
{
$data = $this->login_model->get_menu_check($root_id);
// print_r($data);
echo json_encode($data);
exit;
}
}

Your ajax success function should be like this :

success: function(data)        
{
var html = '';
$.each(data,function(k,v){
alert(v);
html += "<b>menu_code: </b>"+v.menu_code+"<b> menu_name: </b>"+v.menu_name
});
$('#output').html(html);
}

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>


Related Topics



Leave a reply



Submit