Codeigniter : how to create dynamic dropdown - php

So i tried to create a dynamic dropdown in the form page, while user filling the form, there will be a dynamic dropdown for user to select a certain/specific thing, once it's selected, it can be saved into the database. But when i testing it, the dynamic dropdown show nothing, how can i fix it??
The model :
public function get_jenis_pekerjaan(){
$query = $this->db->get('jenis_pekerjaan');
return $query->result_array();
}
The Controller :
public function job_type(){
$data['jenis_pekerjaan'] = $this->m_lowongan->get_jenis_pekerjaan();
$this->load->view('create_lowongan', $data);
}
public function store_lowongan(){
$user_name = $this->session->userdata('name');
$title = $this->input->post('title');
$lokasi = $this->input->post('lokasi');
$level_pekerja = $this->input->post('jenis_pekerjaan');
$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');
$created_at = date('y-m-d');
$status = 'draft';
$data = array(
'user_name' => $user_name,
'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,
'created_at' => $created_at,
'status' => $status,
);
$this->m_lowongan->input_data($data, 'lowongan');
redirect ('dashboard');
}
The view :
<tr>
<td>Level Pekerjaan</td>
<td>
<select name="jenis_pekerjaan" id="jenis_pekerjaan" class="form-control">
<?php foreach($jenis_pekerjaan as $l) {?>
<option value="<?php echo $l['jenis_pekerjaan']?>"><?php echo $l['jenis_pekerjaan']?></option>
<?php }?>
</select>
</td>
</tr>

I think I made it work by doing this.
I create a function to get the data in the model:
public function get_jenis_pekerjaan(){
return $this->db->get('jenis_pekerjaan');
}
I call that function into my controller:
public function create_lowongan() {
$data['jenis_pekerjaan'] = $this->m_lowongan->get_jenis_pekerjaan()->result();
$this->load->view('create_lowongan', $data);
}
I add the $data_jp in my store function to store the value of jenis_pekerjaan:
public function store_lowongan() {
$data_jp['jenis_pekerjaan'] = $this->m_lowongan->get_jenis_pekerjaan();
$user_name = $this->session->userdata('name');
$title = $this->input->post('title');
$lokasi = $this->input->post('lokasi');
$level_pekerja = $this->input->post('jenis_pekerjaan');
$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');
$created_at = date('y-m-d');
$status = 'draft';
$data = array(
'user_name' => $user_name,
'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,
'created_at' => $created_at,
'status' => $status,
);
$this->m_lowongan->input_data($data, 'lowongan');
redirect ('dashboard');
}
I edit the view so it looks like this for the dynamic dropdown:
<tr>
<td>Level Pekerjaan</td>
<td>
<select name="jenis_pekerjaan" id="jenis_pekerjaan" class="form-control">
<?php foreach($jenis_pekerjaan as $l) {?>
<option value="<?php echo $l->job_type?>"><?php echo $l->job_type?></option>
<?php }?>
</select>
</td>
</tr>
And it work, it show the value from jenis_pekerjaan table, and it store the data into the database :D

Related

How can i check all selected value quantity?

Can anyone help me? how can i check all the product selected on the select or in the option? i'm using codeigniter 3 and i want to check if there was an available stock or none but i'm new and don't know how to do it i wish someone can help me. first it was working if i select a product with 0 quantity it will not process but if i select a first product with a quantity on hand greater than 0 and selected the second one with empty qty it will still process because it only check the stocks of the first product. how can i check all the qty selected in my select option?
my model:
public function create()
{
$user_id = $this->session->userdata('id');
// get store id from user id
$user_data = $this->model_users->getUserData($user_id);
$bill_no = 'BUBBLE-'.strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 4));
$data = array(
'bill_no' => $bill_no,
'date_time' => strtotime(date('Y-m-d h:i:s a')),
'gross_amount' => $this->input->post('gross_amount_value'),
'service_charge_rate' => $this->input->post('service_charge_rate'),
'service_charge_amount' => ($this->input->post('service_charge_value') > 0) ?$this->input->post('service_charge_value'):0,
'vat_charge_rate' => $this->input->post('vat_charge_rate'),
'vat_charge_amount' => ($this->input->post('vat_charge_value') > 0) ? $this->input->post('vat_charge_value') : 0,
'net_amount' => $this->input->post('net_amount_value'),
'discount' => $this->input->post('discount_amount_value'),
'paid_status' => 3,
'user_id' => $user_id,
'table_id' => $this->input->post('table_name'),
'discount_id' => json_encode($this->input->post('discount')),
'discount_percent' => $this->input->post('discount_perct_value'),
'datetoday' => date('Y-m-d h:i:s a'),
'payment_id' => json_encode($this->input->post('payment')),
);
$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
$prodid = $this->input->post('product')[$x];
$prod_quan = $this->model_products->getProductData($prodid);
$inputQuantity = (int)$this->input->post('qty')[$x];
$check = $this->model_orders->check_stock($prodid, $inputQuantity);
if($check == TRUE)
{
$insert = $this->db->insert('orders', $data);
$order_id = $this->db->insert_id();
$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
$items = array(
'order_id' => $order_id,
'product_id' => $this->input->post('product')[$x],
'qty' => $this->input->post('qty')[$x],
'rate' => $this->input->post('rate_value')[$x],
'amount' => $this->input->post('amount_value')[$x],
);
$this->db->insert('order_items', $items);
}
$this->load->model('model_tables');
$this->model_tables->update($this->input->post('table_name'), array('available' => 2));
return ($order_id) ? $order_id : false;
}
else
{
return false;
}
}
}
public function check_stock($productId, $inputQuantity)
{
$product = $this->db->get_where('products', ['id' => $productId])->row();
if(isset($product))
{
if($product->qty < $inputQuantity)
{
$this->session->set_flashdata('error','No more stock on some selected items');
return false;
}
else
{
return true;
}
}
}
and my select in view is something like this:
<select class="form-control select_group update-select" data-row-id="row_1" id="product_1" name="product[]" style="width:100%;" onchange="getProductData(1)" required>
<option disabled selected hidden value=''>--Select Products--</option>
<?php foreach ($products as $k => $v): ?>
<option value="<?php echo $v['id'] ?>"><?php echo $v['name'] ?></option>
<?php endforeach ?>
</select>
Easiest solution is to return false as soon as you find a product that is out of stock, something like the following:
Loop through the selected products and for each one check the stock. If you encounter a product that is out of stock, return false.
If all products are in stock, create the order, then loop through the selected products again and add those to the order in the database.
I haven't tested the code below, so there may be errors. I only changed the if($check == TRUE) part and some curly braces.
public function create()
{
$user_id = $this->session->userdata('id');
// get store id from user id
$user_data = $this->model_users->getUserData($user_id);
$bill_no = 'BUBBLE-'.strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 4));
$data = array(
'bill_no' => $bill_no,
'date_time' => strtotime(date('Y-m-d h:i:s a')),
'gross_amount' => $this->input->post('gross_amount_value'),
'service_charge_rate' => $this->input->post('service_charge_rate'),
'service_charge_amount' => ($this->input->post('service_charge_value') > 0) ?$this->input->post('service_charge_value'):0,
'vat_charge_rate' => $this->input->post('vat_charge_rate'),
'vat_charge_amount' => ($this->input->post('vat_charge_value') > 0) ? $this->input->post('vat_charge_value') : 0,
'net_amount' => $this->input->post('net_amount_value'),
'discount' => $this->input->post('discount_amount_value'),
'paid_status' => 3,
'user_id' => $user_id,
'table_id' => $this->input->post('table_name'),
'discount_id' => json_encode($this->input->post('discount')),
'discount_percent' => $this->input->post('discount_perct_value'),
'datetoday' => date('Y-m-d h:i:s a'),
'payment_id' => json_encode($this->input->post('payment')),
);
$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
$prodid = $this->input->post('product')[$x];
$prod_quan = $this->model_products->getProductData($prodid);
$inputQuantity = (int)$this->input->post('qty')[$x];
$check = $this->model_orders->check_stock($prodid, $inputQuantity);
if($check != TRUE)
{
return false;
}
}
$insert = $this->db->insert('orders', $data);
$order_id = $this->db->insert_id();
$count_product = count($this->input->post('product'));
for($x = 0; $x < $count_product; $x++) {
$items = array(
'order_id' => $order_id,
'product_id' => $this->input->post('product')[$x],
'qty' => $this->input->post('qty')[$x],
'rate' => $this->input->post('rate_value')[$x],
'amount' => $this->input->post('amount_value')[$x],
);
$this->db->insert('order_items', $items);
}
$this->load->model('model_tables');
$this->model_tables->update($this->input->post('table_name'), array('available' => 2));
return ($order_id) ? $order_id : false;
}

how to count number of present(status 1 means present) for each student and input beside each student profile under present column

daily_attendances database
below is under crud_model
public function take_attendance()
{
$students = $this->input->post('student_id');
$data['timestamp'] = strtotime($this->input->post('date'));
$data['class_id'] = html_escape($this->input->post('class_id'));
$data['section_id'] = html_escape($this->input->post('section_id'));
$data['school_id'] = $this->school_id;
$data['session_id'] = $this->active_session;
$check_data = $this->db->get_where('daily_attendances', array('timestamp' => $data['timestamp'], 'class_id' => $data['class_id'], 'section_id' => $data['section_id'], 'session_id' => $data['session_id'], 'school_id' => $data['school_id']));
if($check_data->num_rows() > 0){
foreach($students as $key => $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $attendance_id[$key]);
$this->db->update('daily_attendances', $data);
endforeach;
}else{
foreach($students as $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$this->db->insert('daily_attendances', $data);
endforeach;
}
$this->settings_model->last_updated_attendance_data();
$response = array(
'status' => true,
'notification' => get_phrase('attendance_updated_successfully')
);
return json_encode($response);
}
public function get_presentcount_by_student_id($student_id="",$status ="") {
$checker = array(
'student_id' => $student_id,
'status' => 1
);
$presentcount_by_student_id = $this->db->get_where('daily_attendances', $checker);
return $presentcount_by_student_id->num_rows();
}
under the list.php where it shows the student name and the number of days present for each student.The code is below but it does not count number of presents for each student.It still remains 0
<td>
<?php echo $this->crud_model->get_presentcount_by_student_id($student['user_id'],'status');?>
<br>
</td>
Student detail list shown in table format
student_detail_list
use this
$count=DB::select("SELECT COUNT(*) as num FROM 'your-table-name' where status = 1 ")->get();
and you can access this by $count[0]->num

how to query number of present attendance percentage for each student & show percentage present count CodeIgniter framework

below is the code for crud_model.php
Required to add code to generate present percentage for each student into database when submit attendance
public function take_attendance()
{
$students = $this->input->post('student_id');
$data['timestamp'] = strtotime($this->input->post('date'));
$data['class_id'] = html_escape($this->input->post('class_id'));
$data['section_id'] = html_escape($this->input->post('section_id'));
$data['school_id'] = $this->school_id;
$data['session_id'] = $this->active_session;
$check_data = $this->db->get_where('daily_attendances', array('timestamp' => $data['timestamp'], 'class_id' => $data['class_id'], 'section_id' => $data['section_id'], 'session_id' => $data['session_id'], 'school_id' => $data['school_id']));
if($check_data->num_rows() > 0){
foreach($students as $key => $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $attendance_id[$key]);
$this->db->update('daily_attendances', $data);
endforeach;
}else{
foreach($students as $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$this->db->insert('daily_attendances', $data);
endforeach;
}
$this->settings_model->last_updated_attendance_data();
$response = array(
'status' => true,
'notification' => get_phrase('attendance_updated_successfully')
);
return json_encode($response);
}
database fields
Here is the idea of what you want. You will have to analyze it and put it in your code.
if($check_data->num_rows() > 0){
foreach($students as $key => $student):
//add this in your code-----------
$checker = array(
'school_id' => $this->school_id,
'status' => 1
);
$days_a_week = 5;
$present_days = $this->getStudentDaysByID($checker);
$data['present'] = ($present_days / $days_a_week) * 100;
//--------------------------------
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $attendance_id[$key]);
$this->db->update('daily_attendances', $data);
endforeach;
}else{
foreach($students as $student):
$data['status'] = $this->input->post('status-'.$student);
$data['student_id'] = $student;
$this->db->insert('daily_attendances', $data);
//add this in your code-----------
$last_id = $this->db->insert_id();
$checker = array(
'school_id' => $this->school_id,
'status' => 1
);
$days_a_week = 5;
$present_days = $this->getStudentDaysByID($checker);
$data2['present'] = ($present_days / $days_a_week) * 100;
$attendance_id = $this->input->post('attendance_id');
$this->db->where('id', $last_id);
$this->db->update('daily_attendances', $data2);
//--------------------------------
endforeach;
}
//put this outside your public function
function getStudentDaysByID($checker) {
$StudentDaysByID = $this->db->get_where('daily_attendances', $checker);
return $StudentDaysByID->num_rows();
}

how to display dynamic array values to view from model in codeigniter

here is my model code:
foreach ($query->result() as $row)
{
$data = array(
'ptitle' =>$row->ptitle,
'technology' => $row->technology,
'description' => $row->description,
);
$this->session->set_userdata('project',$data);
}
here is my View code:
<?php
if (isset($this->session->userdata['project']))
{
$ptitle = ($this->session->userdata['project']['ptitle']);
$technology = ($this->session->userdata['project']['technology']);
$description = ($this->session->userdata['project']['description']);
}
?>
when i print array it displays
Array ( [ptitle] => opmp [technology] => hbh [description] => kg ) Array ( [ptitle] => icicse [technology] => vv [description] => bhjv ) .can someone help me to print this values in view
Setting session should be like this
$data = array(
'ptitle' => $row['ptitle'],
'technology' => $row['technology'],
'description' => $row['description'],
);
$this->session->set_userdata($data);
To retrive them use
if (isset($this->session->userdata['project']))
{
$ptitle = $this->session->userdata('ptitle');
$technology = $this->session->userdata('technology');
$description = $this->session->userdata('description');
}
If your are storing multiple as array in SESSION then you surely need foreach in your view too.
Change from
if(isset($this->session->userdata['project']))
{
$ptitle = ($this->session->userdata['project']['ptitle']);
$technology = ($this->session->userdata['project']['technology']);
$description = ($this->session->userdata['project']['description']);
}
into
if(isset($this->session->userdata['project']))
{
foreach($this->session->userdata['project'] as $project)
{
$ptitle = $project['ptitle'];
$technology = $project['technology'];
$description = $project['description'];
echo $ptitle.'<br>'.$technology.'<br>'.$description.'<br>';
}
}
Try setting your array like bellow and then pass it to session set data.
$project = array("project" =>
array(
'ptitle' => "ptitle",
'technology' => "technology",
'description' => "description",
)
);
$this->session->set_userdata($project);

PHP Save array in database and miss data

I need to save in database category like "a & b" but the model save in database just "a" miss the blank space and the &.
This is the array:
$data = array('avenvu' => $_POST['avenvu'],
'brand' => $_POST['brand'],
'category' => $_POST['category'],
'description' => $_POST['description'],
'man_women_shop'=> $_POST['man_women_shop'],
'postdatetime' => date("Y-m-d H:i:s",time()),
'publish' => 1,
'user_id' => $this->session->userdata('user_id'));
$result = $this->personal_closest->insertCloset($data);
And this is the model:
function insertCloset($data) {
$this->db->insert('personal_closest',$data);
}
Change your insertCloset function:
function insertCloset($data) {
$personal['avenvu'] = $data['avenvu'];
$personal['brand'] = $data['brand'];
$personal['category'] = $data['category'];
$personal['description'] = $data['description'];
$personal['man_women_shop'] = $data['man_women_shop'];
$personal['postdatetime'] = $data['postdatetime'];
$personal['publish'] = $data['publish'];
$personal['user_id'] = $data['user_id'];
$this->db->insert('personal_closest',$personal);
}

Categories