I want to make function that update client.client_status data from given $id parameters from controller end function that fetched from booking.booking_id
Here my controller
function end($book_id = null) {
if (!empty($book_id)) {
// Booking Table
$table = 'booking';
$where = [
'book_id' => $book_id,
];
$data = [
'room_status' => 1,
];
$this->Model_Data->booking_update($table, $where, $data);
// Client Table
$table = 'client';
$client_id = $???????; // How to get booking.client_id from given id parameters
$where = [
'client_id' => $client_id,
];
$data = [
'room_status' => 1,
];
$this->Model_Data->booking_update($table, $where, $data);
$this->session->set_flashdata('book_ended', 'Book Ended');
redirect('book');
}
else {
$this->session->set_flashdata('book_end_error', 'Book End Error');
redirect('book');
}
}
Here my SQL Tables
According to your comment in Question , book_by_client_id is equal to client_id then for :
Accessing a single row:
(1) Result as an Object
$data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row();
$book_by_client_id =$data_result->book_by_client_id;
$client_id = $book_by_client_id; // client_id
(2) Result as an Array
$data_result = $this->db->get_where('booking',array('book_id'=>$book_id ))->row_array();
$book_by_client_id =$data_result['book_by_client_id'];
$client_id = $book_by_client_id; // client_id
Note : for more info about row(); && row_array();
https://codeigniter.com/userguide3/database/results.html#result-rows
I am a beginner for Codeigniter. I'm currently creating a registration page. I want to enter data into 4 tables at once using CodeIgniter. the table is tbl_asesi, tbl_pdd_non_formal, tbl_work, tbl_organization, but new data comes in 1 table, namely tbl_asesi while the other tables are still empty. can you help me fix the code?
The controller that I use is as follows
my controller :
<?PHP
defined('BASEPATH') OR exit('No direct script access allowed');
class Pendaftaran extends CI_Controller {
public function index()
{
$data = array(
'bidang' => $this->m_pendaftaran->get_bidang(),
'subbidang' => $this->m_pendaftaran->get_subbidang(),
'jenis_bidang' => $this->m_pendaftaran->get_jenis_bidang(),
'level' => $this->m_pendaftaran->get_level(),
'okupasi' => $this->m_pendaftaran->get_okupasi(),
'unitinti' => $this->m_pendaftaran->get_unitinti(),
'unitpilihan1' => $this->m_pendaftaran->get_unitpilihan1(),
'unitpilihan2' => $this->m_pendaftaran->get_unitpilihan2(),
'unitpilihan3' => $this->m_pendaftaran->get_unitpilihan3(),
'bidang_selected' => '',
'subbidang_selected' => '',
'jenis_bidang_selected' => '',
'level_selected' => '',
'okupasi_selected' => '',
'unitinti_selected' => '',
'unitpilihan1_selected' => '',
'unitpilihan2_selected' => '',
'unitpilihan3_selected' => '',
);
$this->template->load('static','pendaftaran',$data);
}
function __construct(){
parent::__construct();
$this->load->library(array('form_validation'));
$this->load->model('m_pendaftaran');
$this->load->helper(array('form', 'url'));
}
public function daftarasesi() {
$this->form_validation->set_rules('nama', 'NAMA','required');
$this->form_validation->set_rules('nik', 'NIK','required');
$this->form_validation->set_rules('jenis_kelamin', 'JENIS_KELAMIN','required');
$this->form_validation->set_rules('alamat', 'ALAMAT','required');
$this->form_validation->set_rules('jabatan', 'JABATAN','');
$this->form_validation->set_rules('asal_institusi', 'ASAL_INSTITUSI','required');
$this->form_validation->set_rules('alamat_institusi', 'ALAMAT_INSTITUSI','required');
$this->form_validation->set_rules('no_hp', 'NO_HP','required');
$this->form_validation->set_rules('email','EMAIL','required|valid_email');
$this->form_validation->set_rules('sd', 'SD','required');
$this->form_validation->set_rules('kota_sd', 'KOTA_SD','required');
$this->form_validation->set_rules('tahun_sd', 'TAHUN_SD','required');
$this->form_validation->set_rules('smp', 'SMP','required');
$this->form_validation->set_rules('kota_smp', 'KOTA_SMP','required');
$this->form_validation->set_rules('tahun_smp', 'TAHUN_SMP','required');
$this->form_validation->set_rules('sma', 'SMA','required');
$this->form_validation->set_rules('kota_sma', 'KOTA_SMA','required');
$this->form_validation->set_rules('tahun_sma', 'TAHUN_SMA','required');
$this->form_validation->set_rules('d', 'D');
$this->form_validation->set_rules('kota_d', 'KOTA_D');
$this->form_validation->set_rules('tahun_d', 'TAHUN_D');
$this->form_validation->set_rules('s1', 'S1');
$this->form_validation->set_rules('kota_s1', 'KOTA_S1');
$this->form_validation->set_rules('tahun_s1', 'TAHUN_S1');
$this->form_validation->set_rules('s2', 'S2');
$this->form_validation->set_rules('kota_s2', 'KOTA_S2');
$this->form_validation->set_rules('tahun_s2', 'TAHUN_S2');
$this->form_validation->set_rules('s3', 'S3');
$this->form_validation->set_rules('kota_s3', 'KOTA_S3');
$this->form_validation->set_rules('tahun_s3', 'TAHUN_S3');
$this->form_validation->set_rules('pelatihan', 'PELATIHAN');
$this->form_validation->set_rules('lembaga', 'LEMBAGA');
$this->form_validation->set_rules('kota_pelatihan', 'KOTA_PELATIHAN');
$this->form_validation->set_rules('tahun_pelatihan', 'TAHUN_PELATIHAN');
$this->form_validation->set_rules('kegiatan', 'KEGIATAN');
$this->form_validation->set_rules('institusi', 'INSTITUSI');
$this->form_validation->set_rules('tahun_kerja', 'TAHUN_KERJA');
$this->form_validation->set_rules('nama_organisasi', 'NAMA ORGANISASI');
$this->form_validation->set_rules('status_anggota', 'STATUS ANGGOTA');
$this->form_validation->set_rules('surat_organisasi', 'SURAT ORGANISASI');
$this->form_validation->set_rules('tahun_organisasi', 'TAHUN_ORGANISASI');
$this->form_validation->set_rules('bidang', 'BIDANG','required');
$this->form_validation->set_rules('subbidang', 'SUBBIDANG','required');
$this->form_validation->set_rules('level', 'LEVEL','required');
$this->form_validation->set_rules('jenis_bidang', 'JENIS_BIDANG','required');
$this->form_validation->set_rules('okupasi', 'OKUPASI','required');
$this->form_validation->set_rules('unitinti', 'UNITINTI','required');
$this->form_validation->set_rules('unitpilihan1', 'UNITPILIHAN1','required');
$this->form_validation->set_rules('unitpilihan2', 'UNITPILIHAN2','');
$this->form_validation->set_rules('unitpilihan3', 'UNITPILIHAN3','');
if($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('gagaldaftar', 'Maaf, Pendaftaran Gagal Dilakukan.');
$this->template->load('static','pendaftaran');
}else{
$data_asesi['nama'] = $this->input->post('nama');
$data_asesi['nik'] = $this->input->post('nik');
$data_asesi['jenis_kelamin'] = $this->input->post('jenis_kelamin');
$data_asesi['alamat'] = $this->input->post('alamat');
$data_asesi['jabatan'] = $this->input->post('jabatan');
$data_asesi['asal_institusi'] = $this->input->post('asal_institusi');
$data_asesi['alamat_institusi'] = $this->input->post('alamat_institusi');
$data_asesi['no_hp'] = $this->input->post('no_hp');
$data_asesi['email'] = $this->input->post('email');
$data_asesi['sd'] = $this->input->post('sd');
$data_asesi['kota_sd'] = $this->input->post('kota_sd');
$data_asesi['tahun_sd'] = $this->input->post('tahun_sd');
$data_asesi['smp'] = $this->input->post('smp');
$data_asesi['kota_smp'] = $this->input->post('kota_smp');
$data_asesi['tahun_smp'] = $this->input->post('tahun_smp');
$data_asesi['sma'] = $this->input->post('sma');
$data_asesi['kota_sma'] = $this->input->post('kota_sma');
$data_asesi['tahun_sma'] = $this->input->post('tahun_sma');
$data_asesi['d'] = $this->input->post('d');
$data_asesi['kota_d'] = $this->input->post('kota_d');
$data_asesi['tahun_d'] = $this->input->post('tahun_d');
$data_asesi['s1'] = $this->input->post('s1');
$data_asesi['kota_s1'] = $this->input->post('kota_s1');
$data_asesi['tahun_s1'] = $this->input->post('tahun_s1');
$data_asesi['s2'] = $this->input->post('s2');
$data_asesi['kota_s2'] = $this->input->post('kota_s2');
$data_asesi['tahun_s2'] = $this->input->post('tahun_s2');
$data_asesi['s3'] = $this->input->post('s3');
$data_asesi['kota_s3'] = $this->input->post('kota_s3');
$data_asesi['tahun_s3'] = $this->input->post('tahun_s3');
//tabel tbl_pdd_non_formal
$data_non_formal = array();
$data_non_formal['pelatihan'] = $this->input->post('pelatihan');
$data_non_formal['lembaga'] = $this->input->post('lembaga');
$data_non_formal['kota_pelatihan'] = $this->input->post('kota_pelatihan');
$data_non_formal['tahun_pelatihan'] = $this->input->post('tahun_pelatihan');
//tabel tbl_pekerjaan
$data_pekerjaan = array();
$data_pekerjaan['kegiatan'] = $this->input->post('kegiatan');
$data_pekerjaan['institusi'] = $this->input->post('institusi');
$data_pekerjaan['surat_pendukung'] = $this->input->post('surat_pendukung');
$data_pekerjaan['tahun_kerja'] = $this->input->post('tahun_kerja');
//tbl_organisasi
$data_organisasi = array();
$data_organisasi['nama_organisasi'] = $this->input->post('nama_organisasi');
$data_organisasi['status_anggota'] = $this->input->post('status_anggota');
$data_organisasi['surat_organisasi'] = $this->input->post('surat_organisasi');
$data_organisasi['tahun_organisasi'] = $this->input->post('tahun_organisasi');
//tbl_asesi
$data_asesi['bidang'] = $this->input->post('bidang');
$data_asesi['subbidang'] = $this->input->post('subbidang');
$data_asesi['level'] = $this->input->post('level');
$data_asesi['jenis_bidang'] = $this->input->post('jenis_bidang');
$data_asesi['okupasi'] = $this->input->post('okupasi');
$data_asesi['unitinti'] = $this->input->post('unitinti');
$data_asesi['unitpilihan1'] = $this->input->post('unitpilihan1');
$data_asesi['unitpilihan2'] = $this->input->post('unitpilihan2');
$data_asesi['unitpilihan3'] = $this->input->post('unitpilihan3');
// var_dump($data_asesi);
// var_dump($data_non_formal);
// var_dump($data_pekerjaan);
// var_dump($data_organisasi);
$this->m_pendaftaran->daftarasesi($data_asesi, $data_non_formal, $data_pekerjaan, $data_organisasi);
// $this->m_pendaftaran->daftarasesi($data);
$this->session->set_flashdata('berhasil', 'Pendaftaran Berhasil Dilakukan.');
$this->template->load('static','pendaftaran');
}
}
public function lakukan_download_asesi(){
force_download('berkas/berkas_asesi.rar',NULL);
}
}
the model that I use is as follows
my model :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_pendaftaran extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{
// insert data asesi //
$this->db->insert('tbl_asesi',$asesi);
$nik_fk = $this->db->insert_nik();
// insert data non formal //
$data_non_formal['nik_fk'] = $nik_fk;
$this->db->insert('tbl_pdd_non_formal',$non_formal);
return $insert_nik = $this->db->insert_nik();
// insert data pekerjaan //
$data_pekerjaan['nik_fk'] = $nik_fk;
$this->db->insert('tbl_pekerjaan',$pekerjaan);
return $insert_nik = $this->db->insert_nik();
// insert data organisasi //
$data_organisasi['nik_fk'] = $nik_fk;
$this->db->insert('tbl_organisasi',$organisasi);
return $insert_nik = $this->db->insert_nik();
}
I think you're doing wrong and in Codeigniter there is no query builder function exists called $this->db->insert_nik() and you were returning this, so it was not working. So you shouldn't use this.
You can insert data into multiple tables from one form as following way and Use this in your model.
public function daftarasesi($asesi, $non_formal, $pekerjaan, $organisasi)
{
$this->db->trans_start();
// insert data asesi //
$this->db->insert('tbl_asesi',$asesi);
// insert data non formal //
$this->db->insert('tbl_pdd_non_formal',$non_formal);
// insert data pekerjaan //
$this->db->insert('tbl_pekerjaan',$pekerjaan);
// insert data pekerjaan //
$this->db->insert('tbl_pekerjaan',$pekerjaan);
// insert data organisasi //
$this->db->insert('tbl_organisasi',$organisasi);
$this->db->trans_complete();
if ($this->db->trans_status() === FALSE)
{
$this->db->trans_rollback();
return FALSE;
} else {
$this->db->trans_commit();
return TRUE;
}
}
You can run as many queries as you want between the start/complete
functions and they will all be committed or rolled back based on
success or failure of any given query.
N.B: Always use transaction when you're dealing with multiple insertion/updation.
I need to create a drcode using the last insert id in the prescribed format,
previously I have used cor php to get the code but now in codeigniter am not able to get the code as the previous one. How can i do this? I am providing my controller and model
Controller
public function newdoctor_post() {
$employee_id = $this->post('EMPLOYEE_ID');
$doctorname = $this->post('DOCTOR_NAME');
$mobilenumber = $this->post('DRMOBILE');
$users_data = $this->Rest_user_model->doctor_exists($mobilenumber);
if ($users_data == 1) {
$message = ['status' => 2,
// 'result' => array(),
'message' => 'Doctor already registered'];
} else {
$speciality = $this->post('SPECIALITY');
$longitude = $this->post('LONGITUDE');
$latitude = $this->post('LATITUDE');
$drcode = $this->post('DRCODE');
$createdon = date('Y-m-d H:i:s');
$insert_array = array('EMPLOYEE_ID' => $employee_id, 'DOCTOR_NAME' => $doctorname, 'DRMOBILE' => $mobilenumber, 'SPECIALITY' => $speciality, 'LONGITUDE' => $longitude, 'LATITUDE' => $latitude, 'CREATEDON' => $createdon);
$users_data = $this->Rest_user_model->doctorregistration($insert_array);
$message = ['status' => 1, // 'result' => $users_data,
'message' => 'Doctor Registered Successfully'];
}
$this->set_response($message, REST_Controller::HTTP_OK);
}
Model
public function doctorregistration($data) {
if (!empty($data)) {
$this->db->insert('DOCTORDETAILS', $data);
return $this->db->insert_id();
}
return 0;
}
Code Generation
$sql1="SELECT DRCODE FROM DOCTORDETAILS ORDER BY DRCODEDESC LIMIT 1";
$query=mysql_query($sql1);
if (!$sql1) { // add this check.
die('Invalid query: ' . mysql_error());
}
$output_array2=array();
while($row=mysql_fetch_assoc($query))
{
$ids=$row['DRCODE'];
}
// echo $ids;
if($ids){
$su=1;
$num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
$unique=$num;
}else{
$unique='DR000001';
}
Try this - replace this code below with your Code Generation code.
$sql1="SELECT DRCODE FROM DOCTORDETAILS ORDER BY DRCODEDESC LIMIT 1";
$query=$this->db->query($sql1);
if (!$sql1) { // add this check.
die('Invalid query');
}
$output_array2=array();
foreach($query->result_array() as $row)
{
$ids=$row['DRCODE'];
}
// echo $ids;
if($ids){
$su=1;
$num =$num = 'DR' . str_pad($su + substr($ids, 3), 6, '0', STR_PAD_LEFT);;
$unique=$num;
}else{
$unique='DR000001';
}
You should make a different column fr drcode, leave it blank.
Now make a trigger on insert, it should be run after insert.
I am assuming DOCTORDETAILS as table name and drcode as column name
DELIMITER $$
CREATE TRIGGER trigger_after_insert
AFTER INSERT ON `DOCTORDETAILS` FOR EACH ROW
begin
UPDATE `DOCTORDETAILS` set drcode=CONCAT('DR',new.id);
END$$
DELIMITER ;
The trigger create a new string with your new auto generated id
Trigger in MySQL:
TechonTheNet
MySQL Trigger on after insert
So I've spent hours on trying to fix this one. every time I upload a file. all the data/id3 tags are being inserted twice on my database. before it was working properly now it has this bug and I badly need some help now.
In my controller I am executing update at the same time since I want also to update the data inserted into database with the details from the id3 tags.
Controller
public function save()
{
$id = $this->session->userdata('user_id');
$status = $this->input->post('status');
$original_file = $this->input->post('original_file');
$revised_file = $this->input->post('revised_file');
$song_data = array(
'user_id' => $id,
'song_info_status' => $status,
'song_info_file_name' => $original_file,
'song_info_revised_file_name' => $revised_file,
'song_info_date_added' => 'now()',
'song_info_date_updated' => 'now()'
);
$song_info_id = $this->song_info_model->save($song_data);
$composer_data = array(
'composer_name' => ''
);
$composer_id = $this->composer_model->save($composer_data);
$song_composer_data = array(
'song_info_id' => $song_info_id,
'composer_id' => $composer_id
);
$this->song_composer_model->save($song_composer_data);
$status_data = array(
'song_info_id' => $song_info_id
);
$status_id = $this->status_model->save($status_data);
$tags_info = array();
$tags_info = $this->getTagsInfo(FCPATH ."temp" . "/" .$revised_file);
if(isset($tags_info['Author']))
{
// echo iconv("UTF-8","EUC-JP", $tags_info['Author']);
//echo $str = mb_convert_encoding($tags_info['Author'], "UTF-8", "auto");
$status_data = array(
'status_artist_name' => $tags_info['Author']
);
$this->status_model->update($status_data, $status_id);
}
if(isset($tags_info['Title']))
{
$song_data = array(
'song_info_original_title' => $tags_info['Title']
);
$this->song_info_model->update($song_data, $song_info_id);
}
rename($this->source.$revised_file, $this->destination.$revised_file);
}
In my model I am just getting the data I have from my controller. there's no loop or anything and I really can't find the culprit.
Model
public function save($data)
{
$this->db->insert('song_info', $data);
return $this->db->insert_id();
}
public function update($data, $id)
{
$this->db->where('song_info_id', $id);
$this->db->update('song_info', $data);
}
I have gone through lots of post from StackOverflow and other sites. Following is the solution which I found almost on all sites But its not working for me. :-
return new \Phalcon\Db\Adapter\Pdo\MySql(array(
"host" => 'XXXx',
"username" => 'XXXX',
"password" => 'XXXX',
"dbname" => 'XXXX',
"options" => array(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)
));
My code for calling Store Procedures:
public function AssignPromoterToPromotionVenuAction($bookingSheetID = 0, $promoter_id = 0, $userID = 0)
{
$query = "CALL `x_wf_AgencyAssignPromoter`($bookingSheetID, $promoter_id, $userID)";
$rp = new Promotion();
return new Resultset(null, $rp, $rp->getReadConnection()->query($query));
}
public function RemovePromoterToPromotionVenuAction($bookingSheetID = 0, $promoter_id = 0, $userID = 0)
{
$query = "CALL `x_wf_AgencyRemovePromoter`($bookingSheetID, $promoter_id, $userID)";
$rp = new Promotion();
return new Resultset(null, $rp, $rp->getReadConnection()->query($query));
}
Finally got the solution :
instead of returning a Resultset you can fetch the result of calling the procedure:
return $rp->getReadConnection()->query($query)->fetchAll();
Ref Link