How to get the job title value from another table in codeigniter - php

I have lowongan table that contains title, lokasi, and level_pekerja, and i have apply_job table that contains company_id, job_title, job_loc, and job_lvl. So when the user want to apply the job, they fill the apply form, and once the user submitted the apply form that user already fill, i want the job_title in the apply_job table is filled with the title value from the lowongan table.
The Model :
public function apply_data($where, $data, $table){
$this->db->where($where);
$this->db->insert($table, $data);
}
The Controller :
public function store_apply($id){
$data_lowongan['lowongan'] = $this->db->get_where('lowongan', ['id'=>$id])->result();
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = 2000;
$this->load->library('upload', $config);
if(!$this->upload->do_upload('pdf_file')){
$error = array('error' => $this->upload->display_errors());
$this->load->view('apply_form', $error);
}else{
$data_upload = $this->upload->data();
$company_id = $this->input->post('company_id');
$job_title = $this->input->post('title');
$user_name = $this->session->userdata('name');
$user_desc = $this->input->post('user_desc');
$data = array(
'user_name' => $user_name,
'company_id' => $company_id,
'job_title' => $job_title,
'user_desc' => $user_desc,
'pdf_file' => $data_upload['file_name']
);
$where = array(
'company_id' => $company_id,
);
$this->m_lowongan->apply_data($where, $data, 'apply_job');
redirect('dashboard');
}
}

Just replace the job_title value into your $data array, which includes all the fields you're going to insert through apply_data():
public function store_apply($id) {
// This line doesn't seem to do anything
$data_lowongan['lowongan'] = $this->db->get_where('lowongan', ['id'=>$id])->result();
// Get the lowongan row based on the parsed id
$lowongan = $this->db->get_where('lowongan', ['id' => $id])->row();
// The rest of your code…
$data = array(
'user_name' => $user_name,
'company_id' => $company_id,
'job_title' => $lowongan->title, // Get the title from the $lowongan query
'user_desc' => $user_desc,
'pdf_file' => $data_upload['file_name']
);
// The rest of your code…
}

Related

Codeigniter : $this->session->userdata('user_id') not working

So i'm trying to learn using codeigniter 3, and i'm trying to get the id from users table, the name of the id column on users table is user_id, and i have another table called lowongan, it has id_user to get the id(user_id) from the users table. So when i tried to input and submit that input to be saved into the database, the id_user is null, but i already set it like this
$id_user = $this->session->userdata('user_id');
$data = array(
'id_user' => $id_user
);
But it's still null how can i fix this??
The model :
<?php
class M_lowongan extends CI_Model{
public function show_lowongan(){
return $this->db->get('lowongan');
}
public function input_data($data, $table){
$this->db->insert($table, $data);
}
}
The Controller :
public function store_lowongan(){
$id_user = $this->session->userdata('user_id');
$title = $this->input->post('title');
$lokasi = $this->input->post('lokasi');
$level_pekerja = $this->input->post('level_pekerja');
$pengalaman_kerja = $this->input->post('pengalaman_kerja');
$pendidikan = $this->input->post('pendidikan');
$alamat = $this->input->post('alamat');
$no_wa = $this->input->post('no_wa');
$no_telp = $this->input->post('no_telp');
$min_gaji = $this->input->post('min_gaji');
$max_gaji = $this->input->post('max_gaji');
$job_desc = $this->input->post('job_desc');
$data = array(
'id_user' => $id_user,
'title' => $title,
'lokasi' => $lokasi,
'level_pekerja' => $level_pekerja,
'pengalaman_kerja' => $pengalaman_kerja,
'pendidikan' => $pendidikan,
'alamat' => $alamat,
'no_wa' => $no_wa,
'no_telp' => $no_telp,
'min_gaji' => $min_gaji,
'max_gaji' => $max_gaji,
'job_desc' => $job_desc
);
$this->m_lowongan->input_data($data, 'lowongan');
redirect ('dashboard');
}
do you already create the session? If not, you can create like this:
$row = $this->Auth_model->get_by_username($this->input->post('username'));
$session = array(
'id_users' => $row->id_users,
'name' => $row->name,
'username' => $row->username,
'email' => $row->email,
'usertype' => $row->usertype,
'photo' => $row->photo,
'photo_thumb' => $row->photo_thumb,
);
$this->session->set_userdata($session);
After that you can easily call the session like:
$this->session->name

codeigniter : inserting data/record into 2 tables with the same id

I have two tables. the first table is tb_mhs and the second user.
how do you make the id_user in the tb_mhs table the same as the id_user in the user table?
thank you very much for your attention
tb_mhs
user
Controller Code :-
$email = $this->input->post('email');
$nama = $this->input->post('nama');
$password = SHA1($this->input->post('password'));
$data1 = array(
'email'=>$email,
'password'=>$password,
'nama'=>$nama,
'level'=>2,
'status'=>0
);
$i=$this->input;
$npm = $this->register_m->create('user',$data1);
$data = array(
'email' => $i->post('email'),
'password' => SHA1 ($i->post('password')),
'npm' => $i->post('npm'),
'nama' => $i->post('nama'),
'j_kelamin' => $i->post('j_kelamin'),
'kelas_id' => $i->post('kelas_id'),
'angkatan_id' => $i->post('angkatan_id'),
'internal_id' => $i->post('internal_id'),
'eksternal_id' => $i->post('eksternal_id'),
'latitude' => -6.873776,
'longitude' => 107.575639,
'berkas' => $i->post('berkas'),
);
// $insert = $this->register_m->create('user',$data1);
$insert1 = $this->register_m->create('tb_mhs',$data);
$this->session->set_flashdata('sukses', 'Data Registrasi Berhasil di Tambahkan');
redirect(base_url('register/viewdataregistrasi'), 'refresh');
}
}
Model
public function create($table, $data)
{
$query = $this->db->insert($table, $data);
return $this->db->insert_id();
}
CI has Query Helper function call $this->db->insert_id(), it returns the insert ID number when performing database inserts.
After the line
$npm = $this->register_m->create('user',$data1);
Add this code
$newUserID = $this->db->insert_id();
Then update your $data array like this
$data = array[
'id_user' => $newUserID,
'email' => $i->post('email'),
'password' => SHA1 ($i->post('password')),
'npm' => $i->post('npm'),
'nama' => $i->post('nama'),
'j_kelamin' => $i->post('j_kelamin'),
'kelas_id' => $i->post('kelas_id'),
'angkatan_id' => $i->post('angkatan_id'),
'internal_id' => $i->post('internal_id'),
'eksternal_id' => $i->post('eksternal_id'),
'latitude' => -6.873776,
'longitude' => 107.575639,
'berkas' => $i->post('berkas'),
];
Rest of code is good.
For more information Read.

How to use a request field to save new a variable with that value?

So, I have a function that creates me alerts based on changes made on a profile.And I need to have customizable alerts for every field, like name, city, email etc.Now, I need to make this working and I also need to show me an alert like ... User x changed his name to y, or User z changed his email to w.
Here is my controller
public function generalinfo(Request $request)
{
if ( $user = Sentinel::getUser() ) {
if ( $user->inRole('individuals') ) {
$data = $this->data;
$id = $user->id;
$user = User::findOrfail($id);
$generalinfo = User::firstOrNew(array('email' => $user->email ));
$dob = date("Y-m-d", strtotime($request->get('dob')));
$generalinfo->name = $request['name'];
$generalinfo->city = $request['city'];
$generalinfo->address = $request['address'];
$generalinfo->email = $request['email'];
$generalinfo->website1 = $request['website1'];
$generalinfo->website2 = $request['website2'];
$generalinfo->website3 = $request['website3'];
$generalinfo->save();
$feed = \App\Feed::create([
'user_id' => $id,
'feed_url' => $request['rss'],
'active' => 1
]);
$generalinfo = User::find($id);
$generalinfo->name = 'NewName';
// other $user changes
foreach ($generalinfo->getDirty() as $key => $attribute) {
$log = \App\Log::create([
'user_id' => $id,
'log' => " {$key} changed to {$user->user}.",
'date' => time(),
'type' => 'profile_updates',
'formatted_date' => date_format(date_create(date('Y-m-d')),"F dS, Y"),
'action_user_id' => $id,
'action_name' => $user->name,
'action_username' => $user->username,
'profile_picture' => $user->profile_picture,
]);
}
$generalinfo->save();
}
}
}
This is my problem : $generalinfo->name = 'NewName'; .I need to get the new value from database from $user->name. The problem is, I tried to use this : $generalinfo->name = $request['name']; but with no effect.

How to solve error Number: 1048 Column 'photo_pict' cannot be null?

I got this error:
A Database Error Occurred
Error Number: 1048
Column 'photo_pict' cannot be null
INSERT INTO `tester` (`photo_pict`, `press_pict`, `name_pict`) VALUES (NULL, NULL, NULL)
Filename: C:/xampp/htdocs/pretest/system/database/DB_driver.php
Line Number: 691
I'm using CodeIgniter and I'm a beginner programmer.
I was making a register form, all I have to do is just saving the input from the register form to the database. But every data has been saved to database except the data for saving photo to the database.
Code show here
Model :
function insert($data){
$data = array(
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'birthDate' => $data['birthDate'],
'gender' => $data['gender'],
'posisi' => $data['posisi'],
'media_name' => $data['media_name'],
'media_region' => $data['media_region'],
'media_ctgry' => $data['media_ctgry'],
'company_addrss' => $data['company_addrss'],
'website' => $data['website'],
'editor_email' => $data['editor_email'],
'office_phone' => $data['office_phone'],
'office_fax' => $data['office_fax'],
'personal_email' => $data['personal_email'],
'phone_number' => $data['phone_number'],
'working_email' => $data['working_email']
);
$this->db->insert('tester', $data);
}
function upload($data){
$gambar = array(
'photo_pict'=> $data['photo_pict'],
'press_pict'=> $data['press_pict'],
'name_pict'=> $data['name_pict']
);
$this->db->insert('tester', $gambar);
}
function proses_regist(){
return $this->db->insert('tester', $data);
}
}
My Controller :
public function __construct(){
parent :: __construct();
$this->load->model("Model_regist");
$this->load->helper(array('url','form'));
}
public function index(){
$this->load->view('home');
}
function register(){
$gambar = array(
'press_pict'=>$this->upload->data("photo_pict"),
'photo_pict'=>$this->upload->data("press_pict"),
'name_pict'=>$this->upload->data("name_pict")
);
$data = array(
'first_name' => $this->input->post("first_name"),
'last_name' => $this->input->post("last_name"),
'birthDate' => $this->input->post("birthDate"),
'gender' => $this->input->post("gender"),
'posisi' => $this->input->post("posisi"),
'media_name' => $this->input->post("media_name"),
'media_region' => $this->input->post("media_region"),
'media_ctgry' => $this->input->post("media_ctgry"),
'company_addrss' => $this->input->post("company_addrss"),
'website' => $this->input->post("website"),
'editor_email' => $this->input->post("editor_email"),
'office_phone' => $this->input->post("office_phone"),
'office_fax' => $this->input->post("office_fax"),
'personal_email' => $this->input->post("personal_email"),
'phone_number' => $this->input->post("phone_number"),
'working_email' => $this->input->post("working_email")
);
$this->Model_regist->insert($data);
$this->Model_regist->upload($gambar);
$this->index();
}
private function uploadImage(){
$config['upload_path'] = './gambar/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $this->product_id;
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = true;
$nama_file = "gambar_".time();
$config['file_name'] = $nama_file;
$this->load->library('upload', $config);
if( ! $this->upload->do_upload('berkas')){
$error = array('error' => $this->upload->display_errors());
}else{
$this->upload->data();
}
$this->Model_regist->upload($gambar);
$this->Model_regist->insert($data);
$this->index();
}
I expect the photo that has been chosen can be saved in the database and can be saved in the file that I have made before. The file name is gambar.

Codeigniter Upload featured image and multiple images at the same time

I have modified the code to use a foreach loop, now I can access the files names, and the print_r is printing the files names in an array like this:
Array ( [0] => Uconn.png [1] => UW_Medicine.png [2] => Yale.png ) Axis.png
but I am still getting the following error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
and a databse error:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `yacht_slider` (`yacht_slide_id`, `slide`) VALUES (87, Array)
I just don't know how should I pass the looped files inside the model to upload.
I am trying to upload a featured image in one input, and multiple images in another input at the same time, I tried many ways and methods to do this, but I always end with an error, mostly Array to string conversion error.
the featured image is stored in one database table and the multiple images are stored in another table.
My current code:
HTML:
<div class="form-group">
<label for="" class="control-label">Image</label>
<input type="file" class="form-control" name="featured">
</div>
<div class="form-group">
<label for="" class="control-label">Slider</label>
<input type="file" class="form-control" name="userfile[]" multiple>
</div>
Model:
public function create_yacht($yacht_img, $slider){
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'img' => $yacht_img,
'city' => $this->input->post('city'),
'category' => $this->input->post('category'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description')
);
$this->db->insert('yachts', $data);
$insert_id = $this->db->insert_id();
$data_4 = array(
'yacht_slide_id' => $insert_id,
'slide' => $slider
);
$this->db->insert('yacht_slider', $data_4);
}
Controller:
public function create_yacht(){
$data['title'] = 'Create';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if($this->form_validation->run() === FALSE){
$this->load->view('templates/admin_header');
$this->load->view('admin/create_yacht', $data);
$this->load->view('templates/admin_footer');
}else{
$config['upload_path'] = './assets/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$this->load->library('upload', $config);
$featured = 'featured';
if (!$this->upload->do_upload($featured)) {
$errors = array('errors' => $this->upload->display_errors());
$yacht_img = 'https://via.placeholder.com/1920x1080';
}else{
$data = array('upload_data' => $this->upload->data());
$yacht_img = $_FILES['featured']['name'];
}
foreach ($_FILES['userfile']['name'] as $name) {
$this->upload->initialize($config);
$this->upload->do_upload($name);
$data = array('upload_data' => $this->upload->data());
$slider = $_FILES['userfile']['name'];
}
print_r($slider);
print_r($yacht_img);
$this->yacht_model->create_yacht($yacht_img, $slider);
// redirect('admin');
}
}
I've cleaned up your code a bit and put in some error reporting so users won't be befuddled when an error occurs.
Controller:
public function create_yacht() {
$data['title'] = 'Create';
$data['categories'] = $this->category_model->get_categories();
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('city', 'City', 'required');
if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/admin_header');
$this->load->view('admin/create_yacht', $data);
$this->load->view('templates/admin_footer');
} else {
$this->load->library('upload');
$upload_path = './testupload/';
// just in case, make path if it doesn't exist
// if we can't die
if (!is_dir($upload_path) && #mkdir($upload_path, DIR_WRITE_MODE) == false) {
show_error('Could not make path!');
}
$config['upload_path'] = $upload_path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['file_ext_tolower'] = true;
//$config['encrypt_name'] = true; // might be a good idea
$this->upload->initialize($config);
// featured image
if (!$this->upload->do_upload('featured')) {
show_error($this->upload->display_errors());
}
$yacht_img = $this->upload->data('file_name');
// multi images
$slider_images = array();
$multi_files = $_FILES['userfile'];
if (!empty($multi_files['name'][0])) { // if slider images are required remove this if
$multi_count = count($_FILES['userfile']['name']);
for ($i = 0; $i < $multi_count; $i++) {
$_FILES['userfile']['name'] = $multi_files['name'][$i];
$_FILES['userfile']['type'] = $multi_files['type'][$i];
$_FILES['userfile']['tmp_name'] = $multi_files['tmp_name'][$i];
$_FILES['userfile']['error'] = $multi_files['error'][$i];
$_FILES['userfile']['size'] = $multi_files['size'][$i];
if (!$this->upload->do_upload()) {
// failure cleanup to prevent orphans
#unlink($upload_path . $yacht_img);
if (count($slider_images) > 0) {
foreach ($slider_images as $image) {
#unlink($upload_path . $image);
}
}
show_error($this->upload->display_errors());
}
$slider_images[] = $this->upload->data('file_name');
}
}
$this->yacht_model->create_yacht($yacht_img, $slider_images);
}
}
Model:
public function create_yacht($yacht_img, $slider_images) {
$slug = url_title($this->input->post('title'));
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'img' => $yacht_img,
'city' => $this->input->post('city'),
'category' => $this->input->post('category'),
'price' => $this->input->post('price'),
'description' => $this->input->post('description')
);
$this->db->insert('yachts', $data);
$insert_id = $this->db->insert_id();
if (count($slider_images) > 0) {
foreach ($slider_images as $image) {
$this->db->insert('yacht_slider', array('yacht_slide_id' => $insert_id, 'slide' => $image));
}
}
}
if you are using mySQL 7 then it supports json datatype and you can save as array. If your using lower version of MySQL best solution is to convert your image array into JSON format then insert that into table.
For more details on JSON in MySQL you can refer
https://dev.mysql.com/doc/refman/8.0/en/json.html#json-values

Categories