Codeigniter - select dropdown wont updated - php

i have a form which included an select dropdown which is id_constructor, the problem is the all remaining data is updated into the database except the id_constructor, my table looks like this :
teams
id_team
name
id_constructor
created_at
modified_at
And here is my code :
Controller :
$this->load->database();
$this->load->helper('form');
$this->load->library(array('form_validation'));
$this->load->model('back-end/m_masterteam','teams');
$this->load->model('back-end/m_masterconstructors','constructors');
public function edit($id_team)
{
$data['teams_data'] = $this->teams->getTeam($id_team);
$data['constructors_data'] = $this->constructors->getAllConstructor();
$this->load->view('back-end/template/header');
$this->load->view('back-end/edit-team', $data);
$this->load->view('back-end/template/footer');
}
public function updateTeam()
{
$id_team = $this->input->post('id_team');
$name = $this->input->post('name');
$id_constructor = $this->input->post('id_constructor');
$modified_at = $this->input->post('modified_at');
$data = array(
'id_team' => $this->input->post('id_team'),
'name' => $this->input->post('name'),
'id_constructor' => $this->input->post('id_constructor'),
'modified_at' => date('Y-m-d H:i:s')
);
// print_r($data);exit();
$this->teams->update($data, $id_team);
echo json_encode(array("status" => TRUE));
redirect('admin-page/MasterTeam/');
}
Model :
var $table = 'teams';
var $column = array('name','id_constructor','created_at','modified_at');
var $order = array('teams.name' => 'desc');
function update($data, $id_team)
{
$this->db->where('id_team', $id_team);
$this->db->update('teams', $data);
}
And here is the view for the select option :
<form action="<?php echo base_url('admin-page/MasterTeam/updateTeam') ?>" method="post">
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Constructor</label>
<div class="col-sm-10">
<select class="form-control" name="id_constructor">
<?php foreach($constructors_data as $row)
{?>
<option value="<?php echo $teams_data->id_constructor; ?>"<?php if($row->id_constructor==$teams_data->id_constructor) echo 'selected="selected"';?>><?php echo $row->name; ?></option>
<?php
}
?>
</select>
</div>
</div>
And here is the result for print_r() :
Array ( [id_team] => 16 [name] => test21 [id_constructor] => 3
[modified_at] => 2022-02-28 13:42:11 )
all data except the id_constructor is updated in the database, anyone know the solution? any help is really appreciated. thank you!.

Related

how to display selected multiple dropdown values in edit page in CodeIgniter

how to display selected multiple dropdown values in edit page in CodeIgniter
Values ​​are not displaying in multiple dropdowns on the edit page
this is how to retrieve data in db
<?php $assignuserstable = $this->db->get_where('assignuserstable',array('user_id'=>$user_id))->row_array(); ?>
<div class="form-group col-md-6">
<label for="admin_id"><?php echo get_phrase('Assign User'); ?>
<span class="text-danger">*</span></label>
<select class="form-control selectpicker" name="admin_idd[]" id="admin_id"
placeholder="Assign User" required multiple>
<option value="" hidden><?php echo get_phrase('Select User'); ?></option>
<?php
$system_usertable = $this->db->get('system_usertable')->result_array();
foreach($system_usertable as $row2):
?>
<option value="<?php echo $row2['admin_id'];?>"
<?php if($assignuserstable['admin_id'] == $row2['admin_id'])echo 'selected';?>>
<?php echo $row2['first_name'];?>
</option>
<?php
endforeach;
?>
</select>
</div>
This is how multiple array is added to the database
model
public function addclientdetails(){
$data['business_name'] = html_escape($this->input->post('business_name'));
$data['legal_name'] = html_escape($this->input->post('legal_name'));
$data['status'] = html_escape($this->input->post('status'));
$data['rating'] = html_escape($this->input->post('rating'));
$data['SU_id'] =html_escape($this->input->post('admin_id'));
date_default_timezone_set("Asia/Kolkata");
$data['created_at'] = Date('Y-m-d h:i:s');
$data['created_by'] = $this->session->userdata('admin_id');
$this->db->insert('user_table', $data);
$insertId = $this->db->insert_id
$admin_idd =html_escape($this->input->post('admin_idd'));
$result = array();
foreach($admin_idd AS $key => $val){
$result[] = array(
'user_id' => $insertId,
'admin_id' => $_POST['admin_idd'][$key],
'user_type' => html_escape($this->input->post('user_type')),
);
}
$this->db->insert_batch('assignuserstable', $result);
$insertId = $this->db->insert_id();
return $insertId;
}
it is impossible to identify the problem when you don't share your code. Please share your model and controller too.
At first please echo $system_usertable and $row2 and check whether there is a data or not.
and your option should looks like this
<option value="<?php echo $row2['admin_id'];?>" <?php echo (isset($assignuserstable['admin_id']) && $assignuserstable['admin_id'] == $row2['admin_id'])? 'selected' : NULL; ?>><?php echo $row2['first_name'];?></option>

Codeigniter - Making dropdown menu while array didn't showing in form edit

I'm new with codeigniter and to the point I don't understand about this one. It's work in another view (form add) while this one (form edit) didn't working.
and there isn't any notification of an error.
my case is i would like to show dropdown menu from database in form edit like in the form add.
this is my code (the working one for form add):
controllers Admin.php :
for add data
class Admin extends CI_Controller {
public function tambah_data(){
$this->load->helper('form');
//kategori
$this->load->model('models_kategori_barang', 'mkb');
$datakb = $this->mkb->GetKategoriBarang();
//model
$this->load->model('models_model_barang', 'modb');
$datamdb = $this->modb->GetModelBarang();
//material
$this->load->model('models_material_barang', 'matb');
$datamatb = $this->matb->GetMaterialBarang();
//merk
$this->load->model('models_merk_barang', 'merb');
$datamerb = $this->merb->GetMerkBarang();
$this->load->view('templates/admin/tambah_data',array(
'datakb' => $datakb,
'datamdb' => $datamdb,
'datamatb' => $datamatb,
'datamerb' => $datamerb));
}
}
view tambah_data.php
<?php echo form_open_multipart('crud_barang/do_insert'); ?>
<label>Kode Barang<br>
<input type="text" autofocus placeholder="Kode" name="kode_barang" autofocus required></label>
<label>Nama<br>
<input type="text" placeholder="Nama" name="nama_barang" required></label>
<label>Kategori<br>
<select name="kategori_barang" required>
<option disabled selected>Pilih Katergori...</option>
<?php foreach ($datakb as $dkb) { ?>
<option><?php echo $dkb['nama_kategori_barang'];?></option>
<?php }?>
</select>
</label>
<label>Model<br>
<select name="model_barang" required>
<option disabled selected>Pilih Model...</option>
<?php foreach ($datamdb as $dmdb) { ?>
<option><?php echo $dmdb['nama_model_barang'];?></option>
<?php }?>
</select>
</label>
<label>Material<br>
<select name="material_barang" required>
<option disabled selected>Pilih Material...</option>
<?php foreach ($datamatb as $dmtb) { ?>
<option><?php echo $dmtb['nama_material_barang'];?></option>
<?php }?>
</select>
</label>
<label>Merk<br>
<select name="merk_barang" required>
<option disabled selected>Pilih Merk...</option>
<?php foreach ($datamerb as $dmb) { ?>
<option><?php echo $dmb['nama_merk_barang'];?></option>
<?php }?>
</select>
</label>
<label>Harga<br><input type="text" placeholder="Harga" name="harga_barang" required></label>
<label>Ukuran<br><input type="text" placeholder="Ukuran" name="size_barang" required></label>
<label>Keterangan<br>
<textarea placeholder="Keterangan" name="ket_barang"></textarea>
</label>
<p><input type="submit" value="Masukan" class="btn"></p>
<?php echo form_close(); ?>
model models_barang.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Models_barang extends CI_Model {
public function GetBarang($where=""){
$datab = $this->db->get('barang'.$where);
return $datab->result_array();
}
public function InsertData($tabelName, $datab){
$res = $this->db->insert($tabelName,$datab);
return $res;
}
public function UpdateData($tabelName,$datab,$where){
$res = $this->db->update($tabelName,$datab,$where);
return $res;
}
public function DeleteData($tabelName,$where){
$res = $this->db->delete($tabelName,$where);
return $res;
}
}
this one not work while i put it in form edit :
controller crud_barang.php
class crud_barang extends CI_Controller {
//funciton insert was here
public function edit_data($id_barang){
/*load model dahulu, karena bukan global model*/
$this->load->model('models_barang', 'mb');
$brg = $this->mb->GetBarang(" where id_barang = '$id_barang'");
$datab = array(
'id_barang' => $brg[0]['id_barang'],
'kode_barang' => $brg[0]['kode_barang'],
'nama_barang' => $brg[0]['nama_barang'],
'material_barang'=> $brg[0]['material_barang'],
'model_barang' => $brg[0]['model_barang'],
'kategori_barang' => $brg[0]['kategori_barang'],
'harga_barang' => $brg[0]['harga_barang'],
'size_barang' => $brg[0]['size_barang'],
'merk_barang' => $brg[0]['merk_barang'],
'ket_barang' => $brg[0]['ket_barang']
);
$this->load->view('templates/admin/form_edit_barang',$datab);
$this->load->model('models_kategori_barang', 'mkb');
$this->load->model('models_model_barang', 'modb');
$this->load->model('models_material_barang', 'matb');
$this->load->model('models_merk_barang', 'merb');
$datakb = $this->mkb->GetKategoriBarang();
$datamodb = $this->modb->GetModelBarang();
$datamatb = $this->matb->GetMaterialBarang();
$datamerb = $this->merb->GetMerkBarang();
$this->load->view('templates/admin/form_edit_barang',array(
'datakb' => $datakb,
'datamodb' => $datamodb,
'datamatb' => $datamatb,
'datamerb' => $datamerb), TRUE);
}
public function do_update(){
/*load model dahulu, karena bukan global model*/
$this->load->model('models_barang', 'mb');
$id_barang = $_POST['id_barang'];
$kode_barang = $_POST['kode_barang'];
$nama_barang = $_POST['nama_barang'];
$material_barang = $_POST['material_barang'];
$model_barang = $_POST['model_barang'];
$kategori_barang = $_POST['kategori_barang'];
$harga_barang = $_POST['harga_barang'];
$size_barang = $_POST['size_barang'];
$merk_barang = $_POST['merk_barang'];
$ket_barang = $_POST['ket_barang'];
$data_update = array(
'id_barang' => $id_barang,
'kode_barang' => $kode_barang,
'nama_barang' => $nama_barang,
'material_barang'=> $material_barang,
'model_barang' => $model_barang,
'kategori_barang' => $kategori_barang,
'harga_barang' => $harga_barang,
'size_barang' => $size_barang,
'merk_barang' => $merk_barang,
'ket_barang' => $ket_barang
);
$where = array('id_barang' => $id_barang);
$res = $this->mb->UpdateData('barang', $data_update, $where);
if($res>=1){
$this->session->set_flashdata('pesan_barang','- Update Data Barang Sukses');
redirect('admin/lihat_data');
}else{
echo "<h2>Update Data Barang Gagal</h2>";
}
}
view form_edit_barang.php
<?php echo form_open_multipart('crud_barang/do_update'); ?>
<label>Id Barang<br>
<input type="text" autofocus placeholder="Id Barang" name="id_barang" value="<?php echo $id_barang; ?>" readonly>
</label>
<label>Kode Barang<br>
<input type="text" autofocus placeholder="Kode" name="kode_barang" value="<?php echo $kode_barang; ?>">
</label>
<label>Nama<br>
<input type="text" placeholder="Nama" name="nama_barang" value="<?php echo $nama_barang; ?>">
</label>
<label>Kategori<br>
<select name="kategori_barang">
<option><?php echo $kategori_barang;?></option>
<?php foreach ($datakb as $dkb) { ?>
<option><?php echo $dkb['nama_kategori_barang'];?></option>
<?php }?>
</select>
</label>
<label>Model<br>
<select name="model_barang" required>
<option><?php echo $model_barang;?></option>
<?php foreach ($datamdb as $dmdb) { ?>
<option><?php echo $dmdb['nama_model_barang'];?></option>
<?php }?>
</select>
</label>
<label>Material<br>
<select name="material_barang" required>
<option><?php echo $material_barang;?></option>
<?php foreach ($datamatb as $dmtb) { ?>
<option><?php echo $dmtb['nama_material_barang'];?></option>
<?php }?>
</select>
</label>
<label>Merk<br>
<select name="merk_barang" required>
<option><?php echo $merk_barang;?></option>
<?php foreach ($datamerb as $dmb) { ?>
<option><?php echo $dmb['nama_merk_barang'];?></option>
<?php }?>
</select>
</label>
<label>Harga<br><input type="text" placeholder="Harga" name="harga_barang" value="<?php echo $harga_barang; ?>"></label>
<label>Ukuran<br><input type="text" placeholder="Ukuran" name="size_barang" value="<?php echo $size_barang; ?>"></label>
<label>Keterangan<br><textarea placeholder="Keterangan" name="ket_barang""><?php echo $ket_barang; ?></textarea></label>
<p><input type="submit" value="Simpan" class="btn"></p>
<?php echo form_close(); ?>
the dropdown menu data from database won't show in form edit. i don't understand why, do i miss something or what? cause there isn't notification of error. help me pls, thank you :)
the result in form add (working):
1
while in form edit, only show the original data :
2
I'm guessing its because in your edit_data function you are loading your view twice. Why that isn't giving you notices for undefined $id_barang .etc. is beyond me; also not sure why you aren't seeing the view twice (probably because you have errors suppressed).
Please note the comments.
public function edit_data($id_barang) {
/* load model dahulu, karena bukan global model */
$this->load->model('models_barang', 'mb');
$brg = $this->mb->GetBarang(" where id_barang = '$id_barang'");
$datab = array(
'id_barang' => $brg[0]['id_barang'],
'kode_barang' => $brg[0]['kode_barang'],
'nama_barang' => $brg[0]['nama_barang'],
'material_barang' => $brg[0]['material_barang'],
'model_barang' => $brg[0]['model_barang'],
'kategori_barang' => $brg[0]['kategori_barang'],
'harga_barang' => $brg[0]['harga_barang'],
'size_barang' => $brg[0]['size_barang'],
'merk_barang' => $brg[0]['merk_barang'],
'ket_barang' => $brg[0]['ket_barang']
);
// DUPLICATE, REMOVED
//$this->load->view('templates/admin/form_edit_barang', $datab);
$this->load->model('models_kategori_barang', 'mkb');
$this->load->model('models_model_barang', 'modb');
$this->load->model('models_material_barang', 'matb');
$this->load->model('models_merk_barang', 'merb');
$datakb = $this->mkb->GetKategoriBarang();
$datamodb = $this->modb->GetModelBarang();
$datamatb = $this->matb->GetMaterialBarang();
$datamerb = $this->merb->GetMerkBarang();
// MERGED ARRAY HERE
$this->load->view('templates/admin/form_edit_barang', array_merge($datab, array(
'datakb' => $datakb,
'datamodb' => $datamodb,
'datamatb' => $datamatb,
'datamerb' => $datamerb)));
}

Codeigniter Update method Issue when adding array values into DB

Trying to update college profile which contains all the college info in the database, Some records are manually filled in by data entry, some records have to be updated by the college admin or super admin.
The issue is when I insert an array (Facilities[ ]) into the Db it inserts without any errors, but when i update the same it gives me an error.
The Insertion was done through another controller with insert statement
$company_id = $this->companies_model->add_company($company_array);
which works flawlessly, But update function gives me this error
Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE pp_companies SET facilities = Array, company_description = 'Established in the year....', company_join = 'Why Join us', WHERE ID = '201'
Filename: C:\xampp\htdocs\jobportal\jp_sys\database\DB_driver.php
Line Number: 287
Facilities = 'Array'? any idea how this shows?
Array ( [0] => Engineering [1] => Management )
sends the value when i check through
print_r($this->input->post('facilities'));
But it doesnt get updated in the table
View
<div class="input-group <?php echo (form_error('company_description'))?'has-error':'';?>">
<label class="input-group-addon">College<br> Description <span>*</span></label>
<textarea class="form-control" name="company_description" id="company_description" rows="8" cols="30" ><?php echo $company_description; ?></textarea>
<?php echo form_error('company_description'); ?> </div>
<div class="input-group <?php echo (form_error('company_join'))?'has-error':'';?>">
<label class="input-group-addon">Why join <br> our College? <span>*</span></label>
<textarea class="form-control" name="company_join" id="company_join" rows="8" cols="30" ><?php echo $company_join; ?></textarea>
<?php echo form_error('company_join'); ?> </div>
<?php echo $facilities ?>
<div class="input-group boxwraper <?php echo (form_error('facilities'))?'has-error':'';?>">
<label class="input-group-addon">Facilities Offered <span></span></label>
<select name="facilities[]" class="js-example-tags" multiple="multiple">
<option value="Library" >Library</option>
<option value="Gym" >Gym</option>
<option value="Canteen" >Canteen</option>
.
.
</select>
</div>
<script type="text/javascript">
$(".js-example-tags").select2({
tags: true
})
</script>
Model
public function add_company($data){
$return = $this->db->insert('pp_companies', $data);
if ((bool) $return === TRUE) {
return $this->db->insert_id();
} else {
return $return;
}
}
public function update_company($id, $data){
$this->db->where('ID', $id);
$return=$this->db->update('pp_companies', $data);
return $return;
}
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Edit_College extends CI_Controller {
public function index()
{
$row = $this->employers_model->get_employer_by_id($this->session->userdata('user_id'));
$data['row'] = $row;
$data['title'] = $row->company_name.' - Edit Profile';
$this->form_validation->set_rules('facilities[]', 'Facilities', 'trim|required|strip_all_tags');
$this->form_validation->set_rules('company_description', 'Company Description', 'trim|required|strip_all_tags|secure');
$this->form_validation->set_rules('company_join', 'Company Join', 'trim|required|strip_all_tags|secure');
$data['facilities'] = (set_value('facilities'))?set_value('facilities'):$row->facilities;
$data['company_description'] = (set_value('company_description'))?set_value('company_description'):$row->company_description;
$data['company_join'] = (set_value('company_join'))?set_value('company_join'):$row->company_join;
if ($this->form_validation->run() === FALSE) {
$this->load->view('employer/edit_company_profile_view',$data);
return;
}
//for facility addition into db
$arrcategory1 = $this->input->post('facilities');
foreach($arrcategory1 as $val1)
{
$categoryarr1 = $categoryarr1 . $val1. ",";
}
$categoryarr1 = substr(trim($categoryarr1), 0, -1); //rtrim($categoryarr,",")
$company_array = array(
'facilities'=> $arrcategory1,
'company_description' => $this->input->post('company_description'),
'company_join' => $this->input->post('company_join'),
$this->companies_model->update_company($row->company_ID, $company_array);
$this->session->set_flashdata('msg', '<div class="alert alert-success">×<strong>Success!</strong> Your company profile has been updated.</div>');
$this->session->set_userdata('slug',$company_slug);
redirect(base_url('employer/edit_college'));
}
}

CODEIGNITER: check if ingredient exist

I have table ingredient w/c consist of(ingredient_id,name),category w/c consist of(category_id,name) and category_ingredients w/c consist of(ingredient_id,category_id). I create a form for adding many ingredients by category and i want to check if 1 or more ingredients already exist then i will only have to get the id's of the ingredient and then the other ingredients that don't exist will be inserted in my db. can u guys help me pls?
Here's my code:
VIEW:
<?php echo form_open('dashboard/uploadIngredients', 'class="form-horizontal" enctype="multipart/form-data"'); ?>
<div class="form-group">
<div class="col-sm-10">
<select required class="form-control" name="ingredient_category">
<option value="" selected disabled>Select Ingredient Category</option>
<option value="All">All</option>
<?php foreach($this->products_model->getCategory() as $row): ?>
<option value="<?php echo $row->category_id ?>"><?php echo $row->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<textarea class="form-control" name="ingredients" rows="5" placeholder="Ingredients (EX. onion, oil, pasta)" required></textarea>
</div>
</div>
<div class='form-group'>
<div class="col-sm-10">
<button class="btn btn-lg btn-positive" type="submit"><i class="glyphicon glyphicon-ok"></i> Save Ingredient</button>
</div>
</div>
<?php echo form_close(); ?>
CONTROLLER:
public function uploadIngredients()
{
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
$saveData[] = array('ingredient_id' => null,
'name' => trim($value)
);
}
$ingredient_id = $this->products_model->saveIngredients($saveData);
foreach (explode(',', $this->input->post('ingredient_category')) as $key => $value)
{
foreach ( $ingredient_id as $key => $str ){
$joinData[] = array(
'ingredient_id' => $str,
'category_id' => intval($value)
);
}
//var_dump($joinData); die();
$this->products_model->saveCategoryIngredients($joinData);
redirect('dashboard/add_ingredients');
}
}
MODEL:
public function saveIngredients($ingredient_id)
{
foreach($ingredient_id as $row => $value) {
$query=$this->db->where('ingredient_id', $value->ingredient_id);
$this->db->insert('ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;
}
public function saveCategoryIngredients($data)
{
foreach($data as $row => $value)
{
$this->db->insert('category_ingredient', $value);
$insert_id[] = $this->db->insert_id();
}
return $insert_id;}
}
You just have to add a function to your model, like this :
<?php
public function getIngredientByName($name) {
return $this->db
->from('ingredient I')
->where('I.name', $name)
->get()->row(); //Will return the row of ingredient if ingredient exists, else null
}
And in your controller :
<?php
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
if (!$this->products_model->getIngredientByName($value)) {
$saveData[] = array(
'ingredient_id' => null,
'name' => trim($value)
);
}
}
Thanks Gwendal for answering this question i am modifying this answer a bit to prevent duplicate inserts e.g.:- if user inserts SuGar CaNe but we have in our database Sugar Cane then the with answer's code we will have 2 sugar cane to avoid these type of inserts we can use this code for model
<?php
public function getIngredientByName($name) {
return $this->db
->from('ingredient I')
-> where("( REPLACE( LOWER(I.name), ' ', '') LIKE '".strtolower(preg_replace('/\s+/', '', $name)) ."%')")
->get()->row(); //Will return the row of ingredient if ingredient exists, else null
}
for controller same as
<?php
foreach(explode(',', $this->input->post('ingredients')) as $key => $value)
{
if (!$this->products_model->getIngredientByName($value)) {
$saveData[] = array(
'ingredient_id' => null,
'name' => trim($value)
);
}
}

How do i retrieve the value of dropdown when editing in codeigniter

OK, so I have looked everywhere for a solution to my problem but found none so far.My code looks like this.I have created a dynamic view page in which edit and add views are loaded dynamically.But the problem arises when i try to retain the value of select dropdown during editing.I would be grateful if someone could help me out.
View
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<?php if(#$patient_info){
echo '<h1 class="page-header">Edit Patient</h1>';
}else{
echo '<h1 class="page-header">Add Patient</h1>';
}
?>
<?php if($this->session->flashdata('error')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
<?php } ?>
<?php if($this->session->flashdata('erroredit')){ ?>
<div class="alert alert-danger"><?php echo $this->session->flashdata('erroredit'); ?></div>
<?php } ?>
<?php //echo validation_errors('<div class="alert alert-danger">','</div>'); ?>
<form role="form" method="post" action="<?php echo isset($patient_info) ? site_url('home/patient/edit') .'/' .$patient_info->patientID : site_url('home/patient/new'); ?>">
<div class="form-group">
<?php echo form_error('pname', '<div class="alert alert-danger">', '</div>'); ?>
<label for="pname">Patient Name</label>
<input class="form-control" placeholder="Enter Patient Name" name="pname" value="<?php echo isset($patient_info) ? $patient_info->patientName : ''; ?>">
</div>
<!-- Dropdown menu for selecting clinic -->
<div class="form-group">
<label for="select">Select Clinic Name</label>
<select class="form-control" name="selectClinic">
<option value="none">Select Clinic Below</option>
<?php foreach($allclinic as $key=>$clinic){ ?>
<!--<?php //foreach($clinicByPatient as $clin): ?>-->
<option value="<?php $clinic->clinicID; ?>"
<?php if(isset($patient_info)){
echo 'selected="selected"';
}
?>
>
<?php echo $clinic->clinicName; ?>
</option>
<?php //endforeach; ?>
<?php } ?>
</select>
</div>
<!-- Select Clinic ends-->
<div class="form-group">
<label for="select">Select Dentist</label>
<select class="form-control" name="selectDentist">
<option value="">Select Dentist</option>
<?php foreach($dentistdet as $key=>$dentist){ ?>
<option value="<?php echo $did = $dentist->dentistID;?>"><?php echo $dentist->dentistName; ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary btn-lg btn-block" name="submit" value="<?php echo isset($patient_info) ? 'Update Patient' : 'Add Patient'; ?>" >
</div>
</form>
<div class="form-group">
<input type="submit" class="btn btn-danger btn-lg btn-block" value="Cancel">
</div>
Edit controller
public function edit(){
$id = $this->uri->segment(4);
$this->load->library('form_validation');
$this->load->model('clinic_model');
$this->load->model('dentist_model');
$data['patient_info'] = $this->patient_model->getPatientById($id);
$data['clinicByPatient'] = $this->patient_model->getPatientByClinic();
$data['allclinic'] = $this->clinic_model->getAllClinics();
// $data['clinicdet'] = $this->patient_model->getPatientByClinic();
if($_POST){
$this->form_validation->set_rules('pname', 'Patient Name', 'trim|required|xss_clean');
$this->form_validation->set_rules('paddress', 'Patient Address', 'trim|required|xss_clean');
$this->form_validation->set_rules('pcontact', 'Patient Contact', 'trim|required|xss_clean');
if($this->form_validation->run()== FALSE){
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout', $data);
}else{
$patientname = $this->input->post('pname');
$patientaddress = $this->input->post('paddress');
$patientcontact = $this->input->post('pcontact');
$select = $this->input->post('selectClinic');
$option = $this->input->post('selectDentist');
$edited = $this->patient_model->editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option);
if($edited){
$this->session->set_flashdata('successedit', 'Successfully updated the record');
redirect('home/patient');
}
}
}else{
$data['subview'] = 'patient/patient_new';
$this->load->view('includes/layout',$data);
}
}
Model looks like this
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
class Patient_model extends CI_Model{
public function countPatients(){
$countPatient = $this->db->count_all('patient');
return $countPatient;
}
public function getallpatients(){
$query = $this->db->get('patient');
if($query->num_rows()>0){
return $query->result();
}
else{
return FALSE;
}
}//getallpatients function ends
public function getPatientByClinic(){
$this->db->select('*');
$this->db->from('patient');
$this->db->join('clinic', 'patient.clinicID = clinic.clinicID', 'left');
$this->db->join('dentist', 'patient.dentistID = dentist.dentistID', 'left');
$query = $this->db->get();
if($query->num_rows>0){
return $query->result();
}
}
public function addPatientByClinic($patientname, $patientadd, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientadd,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
return $this->db->insert('patient',$data);
}// method ends
public function deletePatient($id){
$verifyID = array('patientID' => $id);
// $affRows = $this->db->affected_rows();
// $obj = new Patient_model;
if($verifyID){
$this->db->where($verifyID);
$this->db->delete('patient');
if($this->db->affected_rows()){
return TRUE;
}
}
}
public function editpatient($id, $patientname, $patientaddress, $patientcontact, $select, $option){
$data = array(
'patientName' => $patientname,
'patientAddress' => $patientaddress,
'patientContact' => $patientcontact,
'clinicID' => $select,
'dentistID' => $option
);
$query = $this->db->where('patientID', $id)
->update('patient', $data);
if($query){
return true;
}
}//method ends
public function getPatientById($id){
$query = $this->db->where('patientID', $id)
->get('patient');
return $query->row();
}
}//class ends
?>
In the code for your select box, you aren't actually echoing $clinic->clinicID. Thus, when the form is submitted, the value will be empty.
You also need to be careful with how you are choosing which select element will be selected by default - you aren't comparing with anything that will change within the loop. Should you be checking against $clinic->clinicID?

Categories