Here is the code :
if ($param1 == 'do_update') {
$data['student_id'] = $this->input->post('student_id');
$data['title'] = html_escape($this->input->post('title'));
if ($this->input->post('description') != null) {
$data['description'] = html_escape($this->input->post('description'));
}
$data['amount'] = html_escape($this->input->post('amount'));
$data['amount_paid'] = html_escape($this->input->post('amount_paid'));
$data2['amount'] = html_escape($this->input->post('amount'));
/*$data['status'] = $this->input->post('status');*/
$data['creation_timestamp'] = strtotime($this->input->post('date'));
$this->db->where('invoice_id', $param2);
$this->db->update('invoice', $data);
$this->db->where('invoice_id', $param2);
$this->db->update('payment', $data2);
2 tables Invoice and Payment... I can update data in "invoice table"
with a specific ID, but also at the same time, it is updating the
whole column in the "Payment table" instead of that specific ID. I
can tell doing something wrong in the last 4 lines.
$data fields are of Invoice Table and
$data2 fields are of Payment table
Invoice_id is common in both tables..
You aren't actually using your where conditions. Try:
$this->db->where('invoice_id', $param2)->update('invoice', $data);
$this->db->where('invoice_id', $param2)->update('payment', $data2);
Related
I am using Academy LMS Source code is in Github
it's a Learning Management system and it is designed to have one category per course
I want to add multiple categories to one course
In /controllers/API.php I have
// Fetch all the categories
public function categories_get($category_id = "") {
$categories = array();
$categories = $this->api_model->categories_get($category_id);
$this->set_response($categories, REST_Controller::HTTP_OK);
}
// Fetch all the courses belong to a certain category
public function category_wise_course_get() {
$category_id = $_GET['category_id'];
$courses = $this->api_model->category_wise_course_get($category_id);
$this->set_response($courses, REST_Controller::HTTP_OK);
}
then in /models/Api_model.php, I have
// Get categories
public function categories_get($category_id)
{
if ($category_id != "") {
$this->db->where('id', $category_id);
}
$this->db->where('parent', 0);
$categories = $this->db->get('category')->result_array();
foreach ($categories as $key => $category) {
$categories[$key]['thumbnail'] = $this->get_image('category_thumbnail', $category['thumbnail']);
$categories[$key]['number_of_courses'] = $this->crud_model->get_category_wise_courses($category['id'])->num_rows();
}
return $categories;
}
// Get category wise courses
public function category_wise_course_get($category_id)
{
$category_details = $this->crud_model->get_category_details_by_id($category_id)->row_array();
if ($category_details['parent'] > 0) {
$this->db->where('sub_category_id', $category_id);
} else {
$this->db->where('category_id', $category_id);
}
$this->db->where('status', 'active');
$courses = $this->db->get('course')->result_array();
// This block of codes return the required data of courses
$result = array();
$result = $this->course_data($courses);
return $result;
}
then in /model/Crud_model.php, I got
public function get_category_details_by_id($id)
{
return $this->db->get_where('category', array('id' => $id));
}
function get_category_wise_courses($category_id = "")
{
$category_details = $this->get_category_details_by_id($category_id)->row_array();
if ($category_details['parent'] > 0) {
$this->db->where('sub_category_id', $category_id);
} else {
$this->db->where('category_id', $category_id);
}
$this->db->where('status', 'active');
return $this->db->get('course');
}
in SQL course table has a column named category_id int(11) which stores one category per course I've changed it to TEXT format and put comma-separated values like 1,2,3 and used the ways like
$this->db->where_in('category_id',$category_id)
and
$this->db->like('category_id',$category_id)
and
$this->db->where("FIND_IN_SET(".$category_id.",category_id) >", 0);
and got no result I just need courses to have comma-separated values in the category_id column
Simply, I want courses to have multiple categories
DB tables
https://pasteboard.co/JNSxO2kz.png
course table
https://pasteboard.co/JNSy7g0.png
category table
https://pasteboard.co/JNSyhlk.png
category_id table (Mapping Table suggested by #micheal-la-ferla )
https://pasteboard.co/JNSyGGn.png
anybody could help?
The way Academy-LMS is designed, it lets you only add 1 category per course. It limits the user and it can be frustrating.
One workaround which could work is to create a new mapping table with 3 columns as follows:
Field Name | Data Type
------------+------------------
ID | int(11)
Course ID | int(11)
Category ID | int(11)
Obviously you need to have the permission to create new tables to use this workaround. Not sure if this is possible for your implementation of Academy-LMS.
The step above will essentially create a one to many relationship between the course and the category, so that 1 course can then be part of multiple categories. (In reality, this would be a many to many relationship since I am assuming that a category may obviously belong to multiple courses).
Therefore the database design will be similar to the one I created here.
If you implement this change, you would then need to make the following changes to the code you have:
// Get categories
public function categories_get($category_id)
{
if ($category_id != "") {
$this->db->where('category_id', $category_id);
}
$this->db->where('parent', 0);
$categories = $this->db->get('course_id')->result_array();
foreach ($categories as $key => $category) {
$categories[$key]['thumbnail'] = $this->get_image('category_thumbnail', $category['thumbnail']);
$categories[$key]['number_of_courses'] = $this->crud_model->get_category_wise_courses($category['id'])->num_rows();
}
return $categories;
}
Essentially, what I did here was replace the parent in the categories with category_id and category with course_id. Not sure if these are correct. You will need to review these when reading from the database as I never used CodeIgniter.
I have a problem on getting multiple data of a single patient. I can only generate 1 physical examination result and I can't get all of it.
My Controller
public function print_sum_report($case_id){
//fetching all data from different tables
$case_id = 1;
$postnatal_id = 1;
$this->load->model('Prms_model');
$data['n_status'] = $this->Prms_model->get_status_f($case_id);
$data['n_mh'] = $this->Prms_model->get_mh_f($case_id);
$data['n_pe'] = $this->Prms_model->get_pe_f($case_id);
$data['n_post'] = $this->Prms_model->get_pn_f($postnatal_id);
$data['n_infant'] = $this->Prms_model->get_infant_f($case_id);
$this->load->view('report/reportsum', $data);
// print_r($data);
}
My Model
public function get_pe_f($case_id){
// joining 2 tables (physicalexamination and patient_info by ID)
$this->db->select('*');
$this->db->from('physicalexamination');
$this->db->where('case_id', $case_id);
$this->db->join('patient_info', 'patient_info.patient_ID = physicalexamination.Patient_ID');
$query = $this->db->get();
return $query->result();
}
$data['n_pe'] is a result object which contains many rows. You need to iterate over it to gather all the rows:
foreach($data['n_pe'] as $pe) {
var_dump($pe);
}
Also, you could write your model this way:
public function get_pe_f($case_id) {
// joining 2 tables (physicalexamination and patient_info by ID)
return $this->db
->where('case_id', $case_id)
->join('patient_info', 'patient_info.patient_ID = physicalexamination.Patient_ID');
->get('physicalexamination')
->result();
}
I want to get the sum of the column with specific id from one table and get the details from another table with that id with mysql query and codeigniter.
I have used this code:
function total_commision($ids)
{
$values=array();
for($o=0;$o<count($ids);$o++) {
$this->db->select('SUM(commision) AS total_commision', FALSE);
$this->db->where('hierarchy_users_id',$ids[$o]);
$query = $this->db->get('commision');
$value=$query->result()[0];
array_push($values, $value);
}
return $values;
}
I want to get the details of each member from the another table with that id in database using codeigniter query .
you can try the following
function total_commision($ids)
{
$query = $this->db
->select('SUM(commision) AS total_commision, admin_credentials.*', FALSE)
->from("commision")
->join("admin_credentials", "admin_credentials.id = commision.hierarchy_users_id", "left")
->where_in('commision.hierarchy_users_id', $ids)
->group_by('commision.hierarchy_users_id')
->get();
return $query->result();
}
I want to update the products leased count when a product is rented to a customer.
The owner of the website can rent multiple things. I do this by cloning with Ajax.
Now, when someone has rented like 2 chairs and 1 table, i want to update the products_leased row for both products. How would i do that?
The error i get:
`SQLSTATE[01000]: Warning: 1265 Data truncated for column 'product_leased' at row 1 (SQL: update `products` set `product_leased` = 3 5, `updated_at` = 2017-06-07 19:57:03 where `id` = 1)`
My controller:
public function create_order($id) {
$customer = Customer::find($id);
$pr = Product::get();
$rent_from = Request::input('rent_from');
$rent_till = Request::input('rent_till');
$productArray = Request::get('product_name');
$productCountArray = Request::get('product_count');
$rentProduct = new cRent;
$rentProduct->customer_id = $customer->id;
$rentProduct->rent_from = $rent_from;
$rentProduct->rent_till = $rent_till;
$rentProduct->rented_products = implode(', ', $productArray);
$rentProduct->rented_products_count = implode(', ', $productCountArray);
$rentProduct->save();
$pru = Product::find($id);
$pru->product_leased = implode(' ', $productCountArray);
$pru->update();
return back();
}
The code above works only when one product has been rented. Not when i rent 2 products.
Thanks in advance.
Why are you passing in the same $id to
$customer = Customer::find($id);
and
$pru = Product::find($id);
?
Instead of imploding the product names and counts and storing them together, why not create separate rent records for each product and relate them as a one-to-many? Assuming product name and product count line in the arrays, something to the effect of:
foreach(array_combine($productArray, $productCountArray) as $product_name => $count){
$product = Product::where('name', $product_name)->first();
$rentProduct = new cRent;
$rentProduct->customer_id = $customer->id;
$rentProduct->rent_from = $rent_from;
$rentProduct->rent_till = $rent_till;
$rentProduct->product_id = $product->id
$rentProduct->count = $count;
$rentProduct->save();
}
Better yet, don't pass in the product names, instead pass the product ids in from the form.
$rentProduct = new cRent;
the code above indicates only one instance of your cRent off-course if you want have another transaction with the database with a different values or data another instance is needed
I think what you want is something like this
public function create_order($id) {
$customer = Customer::find($id);
$pr = Product::get();
$rent_from = Request::input('rent_from');
$rent_till = Request::input('rent_till');
$productArray = Request::get('product_name');
$productCountArray = Request::get('product_count');
foreache($productArray as $prod){
$rentProduct = new cRent;
$rentProduct->customer_id = $customer->id;
$rentProduct->rent_from = $rent_from;
$rentProduct->rent_till = $rent_till;
$rentProduct->rented_products = $prod
$rentProduct->rented_products_count = //not sure in this line
$rentProduct->save();
}
$pru = Product::find($id);
$pru->product_leased = implode(' ', $productCountArray);
$pru->update();
return back();
}
I am looking for a simpler query to return a row from joined tables as if it were newly inserted with default values, except for the primary/foreign key which ideally should be returned as NULL. This is so I can use a single data entry form to either add a new entry or edit an existing one.
I am using CodeIgniter, DB_ActiveRecord class and postgre DB driver.
The function I came up with is shown below, with the part I would like to simplify marked with a comment "How to optimize?":
public function get_entry()
{
$id = $this->input->post('id');
if ($id) {
// Get existing entry from joined tables.
$query = $this->db
->join('b', 'a.id = b.id')
->get_where('a', array('a.id' => $id));
} else {
// Get a new "default" entry from joined tables.
// How to optimize? Ideally it would:
// 1. Not need a transaction.
// 2. Not insert (causes holes in the id sequence.)
// 3. Not use a PHP trick of overlaying same-named results.
$this->db->trans_begin();
// Note: I modified insert() so that empty array gives "DEFAULT VALUES"
// and 3rd parameter gives "RETURNING ..."
$query = $this->db
->insert('a', array(), 'id');
if ($this->db->trans_status() and $query->num_rows()) {
$id = $query->row()->id;
$query = $this->db
->insert('b', array('id' => $id));
if ($this->db->trans_status() and $query) {
$query = $this->db
// Note: This trick overlays NULL onto value in id
->select('*, NULL AS id')
->join('b', 'a.id = b.id')
->get_where('a', array('a.id' => $id));
}
}
// Note: Trash the temporary insert.
$this->db->trans_rollback();
}
if ($query and $query->num_rows()) {
return $query->result();
}
return FALSE;
}