Auto Select option form Laravel - php

i want to make my edit is auto select option but when i try is doesnt work. i need my form edit is auto select the data if i click data with id_cms_companies "1" from my table location is auto select and show in the first select option.
and its show the name of table cms_companies
here my code :
<select class='form-control' name='id_cms_companies'>
<option>- Companies -</option>
<?php
$category = DB::table("cms_companies")->get();
foreach($category as $cat)
{
if($cat->name == $row->id_cms_companies)
{
echo "<option value='$cat->name' selected>$cat->name</option>";
}
else
{
echo "<option value='$cat->name'>$cat->name</option>";
}
}
?>
</select>
and here my controller :
public function getEdit($id) {
$data['row'] = locations::find($id);
return view('locations_form',$data);
}
public function postEditSave($id) {
$simpan= array();
$simpan['name']=Request::input('name');
$simpan['id_cms_companies']=Request::input('id_cms_companies');
DB::table('locations')->where('id', $id)->update($simpan);
Session::flash('edit', 'Data berhasil di Edit');
return Redirect::to('locations');
}
here my table :
table locations
and here for my
table cms_companies

You should compare two IDs, no ID with Name
if($cat->name == $row->id_cms_companies) //wrong
if($cat->id == $row->id_cms_companies) //correct

You could use laravel html, it will do it automatically
So you could do something like this:
$category = DB::table("cms_companies")->get()->pluck('name', 'id');
echo Form::select('id_cms_companies', $category, $row->id_cms_companies, ['class' => 'form-control']);
Read more about it here docs

Related

How to show dynamic value exist in selectpicker?

<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 am not able to update my records in CodeIgniter

I am simply updating my records in a database. I am passing Id and status to the controller and want to update to database but I am not able to do it.
Controller name is Examtest, model name is crud_model, view name is categories.
This code is of View:
foreach ($user_data as $key => $val) {
$status = $val['status'];
if ($status == "Active") {
$st = '<p class="text-success">ACTIVE</p>';
$stl = 'Make Inactive';
}else{
$st = '<p class="text-danger">INACTIVE</p>';
$stl = 'Make Active';
}
This code is of Controller, I have created one function :
public function update_status()
{
$this->load->model('crud_model');
$data['user_data']=$this->crud_model->update_status();
if (isset($_REQUEST['sta'])) {
if ($data=="Active") {
$this->session->set_flashdata('msg',"Successfully");
$this->session->set_flashdata('msg_clas','alert-success');
}
else{
$this->session->set_flashdata('msg',"Not Successfully");
$this->session->set_flashdata('msg_clas','alert-danger');
}
}
return $this->load->view('backend/teacher/categories');
}
This code is of model, I had created one function :
function update_status(){
$this->load->database();
$id=$_REQUEST['id'];
$saval=$_REQUEST['sta'];
if ($saval="Active") {
$status= "Inactive";
}
else
{
$status = "Active";
}
$data=array('status'=>$status);
$this->db->where('category_id',$id);
return $this->db->update('tbl_categories',$data);
}
Where am I going wrong? I had just created some record in that I had given Action button, if someone click on select option it will show them a select option if someone choose first option it will update my records in database too.
Please use last_query() method, to print current update query.
To print last query please use -
echo $this->db->last_query();die;
Update query --
$data=array('status'=>$status);
$this->db->where('category_id',$id);
$this->db->update('tbl_categories',$data);
echo $this->db->last_query();die;
return $this->db->affected_rows();
Please try this.

Passing data from Controller to View with CodeIgniter

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);

Populating <select> with mysql data dynamically using CodeIgniter

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>

CodeIgniter: Array to string conversion error on a page supposed to retrieve data from a table, then edit and update

I'm trying to get records and let users of the application edit details of records and update new details. But when I print_r ($row->emp_name) it returns: John Smith and ($row->emp_type) returns Cachier which are correct values. But why those data are not passed into the 'emp_name' and 'emp_type' inputs in the view?
Second question:
I also want to know how to define a value for a dropdown, to be selected on load. Note that the dropdown values has to be from a database and should be retrieved in a similar way described above.
View:
<?php
$js = 'id="emp_name"';
echo form_input('emp_name', $emp_name, set_value('emp_name'), $js);
?>
Controller:
function index() {
$this->load->model('edit/default_price_model');
//$data['query'] = $this->default_price_model->get_employees_table();
$this_employee['emp_name'] = $this->default_price_model->current_cmployees();
//$temp_array=array_merge($data, $this_employee);
$this->load->view('/edit/employees', $this_employee);
}
function form_validation() {
if($this->input->post('this_employee') == "View") {
$this->populate_form();
}
else {
$this->load->library('form_validation');
$this->form_validation->set_rules('emp_name','Employee Name', 'required|min_length[3]|max_length[60]');
$this->form_validation->set_rules('emp_type','Employee Name', 'required');
$this->form_validation->set_rules('emp_description','Optional Description', 'max_length[500]');
if ($this->form_validation->run() == FALSE ) {
$this->index();
}
else {
$this->update_table();
}
}
}
function populate_form() {
$this->load->model('edit/default_price_model');
$selection = $this->input->post('select_employee');
$data['emp_name'] = $this->default_price_model->get_employees_table($selection);
$this->index();
}
Model:
function get_employees_table($selection) {
$query = $this->db->query(" SELECT emp_name, emp_type, emp_description
FROM employees
WHERE emp_name='$selection'
ORDER BY emp_name ASC
LIMIT 1");
if($query->num_rows()>0) {
foreach($query->result() as $row) {
$row->emp_name;
$row->emp_type;
$row->emp_description;
}
}
}
Your form input is just has the form values if set in set_value not your database values it should look like this
<?php
$js = 'id="emp_name"';
echo form_input('emp_name', $emp_name,
set_value('emp_name', isset($emp_name[0]->emp_name) ? $emp_name[0]->emp_name : '')
, $js);
?>
$emp_name is what you have from the controller and has the db values
$data['emp_name'] = $this->default_price_model->get_employees_table($selection);
One thing i have notice you haven't returned any results from the query in your model and also the foreach loop is doing nothing
function get_employees_table($selection) {
$query = $this->db->query(" SELECT emp_name, emp_type, emp_description
FROM employees
WHERE emp_name='$selection'
ORDER BY emp_name ASC
LIMIT 1");
if($query->num_rows()>0) {
return $query->result();
}else{
return 0;
}
}
Hope it helps you

Categories