I need help Select Sum in codeigniter - codeigniter

I need help to help me correct my php function in codeigniter.
I have a database; I want addirionner all values ​of " from_user_id ".
Check data
I use this sql to get the desired result.
SELECT SUM(kiff_envoi+kiff_recu+visite_recu+profile_visite+da_recu+da_envoi+topic_poster+repon_topic+conv_envoi+conv_recu) AS total_stats FROM score WHERE from_user_id = ?
In codeingniter I​ use this function.
It allows me to check if " from_user_id " exists, if it exists, we calculate the total different tables and we update the result in " total_stats ". If " from_user_id " does not exist, create it.
But my function does not return me the right result.
I had to make a big mistake but I do not know where!
Does someone could help me please.
public function count_tout_stats($user_id) //total des stats
{
$this->db->select_sum(conv_recu, conv_envoi, repon_topic, etc...);
$query = $this->db->from('score');
$this->db->where('from_user_id', $user_id);
$total_stats = $this->db->count_all_results();
if ($this->ttl_stats_score($user_id) > 0) // Check if exists
{
$data = array(
'total_stats' => $total_stats
);
$this->db->where('from_user_id', $user_id);
$this->db->update('score', $data);
}
else
{
$data = array(
'from_user_id' => $user_id,
'total_stats' => $total_stats
);
$this->db->insert('score', $data);
}
return $total_stats;
}
public function ttl_stats_score($user_id)
{
$this->db->where('from_user_id', $user_id);
$this->db->from('score');
return $this->db->count_all_results();
}
Thank you in advance for your assistance
Violette

You can try this:
$this->db->select('(kiff_envoi+kiff_recu+visite_recu+profile_visite+da_recu+da_envoi+topic_poster+repon_topic+conv_envoi+conv_recu) as total_stats');
$this->db->from('score');
$this->db->where('from_user_id', $user_id);
$result = $this->db->get()->row_array();
print_r($result);
You will get your result

Related

Codeigniter : array to string conversion error

Here is my model:
public function count_diabetic(){
$query = $this->db->query("Select count(diabetic) as count_diabetic from member where diabetic is not NULL");
return $query->result_array();
}
public function count_hypertensive(){
$query = $this->db->query("Select count(hypertensive) as count_hypertensive from member where hypertensive is not NULL");
return $query->result();
}
here is my controller:
public function home(){
$this->load->model('Jsv_model');
$data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata($data);
$this->load->view('home');
}
Here is my view with in php tag:
echo $this->session->userdata('count_diabetic');
But Here it shows error that array to string conversion error..
please help me
Inside count_diabetic() you should change $query->result_array()
into
$query->row()->count_diabetic,
it would return number of count only not array.
Do it to count_hypertensive() too.
In PHP, you can display arrays by using the print_r function instead of echo.
For example, you have to change your code to:
print_r($this->session->userdata['count_diabetic']);
You are doing it wrong in this line.
echo $this->session->userdata('count_diabetic');
What should you do?
// To Set a Data in Session
$session_data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata('user_data', $session_data);
// To get the session data
$session_data = $this->session->userdata('user_data');
$user_name = $session_data['username'];
$count_diabetic = $session_data['count_diabetic'];
$count_hypertensive = $session_data['count_hypertensive'];

codeigniter get array data in controller

I have sql query in controller, this query produces number of rows.
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}
then i pass this data to my model,
public function update_cancel_stock($code,$qty)
{
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $order );
$this->db->update("tbl_inventory6");
}
but no any update. please check above code. thanx
Try this
$cond = array("order_id" => $order_no, "location" => $location);
$q1 = $this->db->select("product_code,product_quantity")->from("tbl_order_details")->where($cond)->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
and then model ($code was not used)
public function update_cancel_stock($code,$qty){
$this->db->set('product_quantity', $qty , FALSE);
$this->db->where('product_id', $code);
$this->db->update('tbl_inventory6');
}
You are using row() function of Active Record. It already return only one row. So there is no requirement of foreach. Try this:
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->row();
$result = $this->settings_model->update_cancel_stock($q1->product_code,$q1->product_quantity);
If you are getting multiple records and you are using foreach, you should use result().
$q1 = $this->db->query("SELECT product_code,product_quantity FROM tbl_order_details WHERE order_id='$order_no' AND location='$location'")->result();
foreach($q1 as $v_barcode){
$result = $this->settings_model->update_cancel_stock($v_barcode->product_code,$v_barcode->product_quantity);
}

Codeigniter multiple parameter on update

I am copying this code in Codeigniter official docs, but I don't know why this will throw an error
public function acceptChangeRequest($id,$data1,$accept) {
$data = array(
'status' => $accept,
'approve_by' => $data1,
);
$this->db->where('id', $id);
$this->db->update('change_request',$data);
//return true;
}
Error Number: 1054
Unknown column 'Array' in 'field list'
UPDATE change_request SET status = 'Y', approve_by = Array WHERE id = '22'
It sounds wierd since this code works in my other function. Any idea?
Read The Documentation Clearly
Read About Update Records In Codeigniter
Here IS Demo Function For You
public function updateBasic($data,$user_id){
$userData=array(
'marital_status'=>$data['marital_status'],
'country'=>$data['stateofresidence'],
'city'=>$data['city'],
'caste'=>$data['caste'],
'residential_status'=>$data['residencystatus']
);
$this->db->WHERE('user_id',$user_id)->update('basic_profile',$userData);
return true;
}
You Must Check Through Print_r();what Is coming To your Function In Your Case $data1 Is Coming In the From OF array You Must Extract The Array To Update The Record
Use this function in your model to update the record.
public function acceptChangeRequest($where, $table, $data){
$this->db->where($where);
$this->db->update($table, $data);
return $this->db->affected_rows() > 0;
}
use this code in your controller.
$where = array('id'=>$id);
$data = array(
'status' => $accept,
'approve_by' => $data1,
);
$this->User_model->update($where,'table_name',$data);
What is in your $data1 variable? I can see that it's an array, and that is the problem.
If your $data1 stores indexes like in your table's row, than this is the possible solution for you:
public function acceptChangeRequest($id,$data1,$accept) {
$data1["status"] = $accept;
$this->db->where('id', $id);
$this->db->update('change_request', $data1);
return true;
}
But if your $data1 variable stores only the approve_by information, than your possible solution is:
public function acceptChangeRequest($id,$data1,$accept) {
$data = array(
'status' => $accept,
'approve_by' => $data1["approve_by"]
);
$this->db->where('id', $id);
$this->db->update('change_request', $data);
return true;
}
First of all, you need to know what you expect from $data1, than you can move forward to the solution.
You can see your $data1 variable in your Controller's function:
print "<pre>";
print_r($data1);
print "<pre>";
die();

How to get a value from the last inserted row using Codeigniter

Is there any codeigniter function to do this?
try this code:
$this->db->insert_id();
Refer:
https://www.codeigniter.com/user_guide/database/helpers.html
There is no special function for that. But you can get the id of row that you've just inserted and then get it's data:
$this->db->insert('table_name', $data);
$id = $this->db->insert_id();
$q = $this->db->get_where('table_name', array('id' => $id));
return $q->row();
I have tips for you. When you want to insert data, please return your data array and you can edit it.
public function set_alb_photos() {
$data = array(
'alp_title' => $this->input->post('alp_title'),
'alp_file_location' => $this->input->post('alp_file_location'),
'alp_album_id' => $this->input->get('alb_id'),
'alp_created_at' => date("Y-m-d H:i:s"),
'alp_updated_at' => date("Y-m-d H:i:s")
);
$this->db->insert('alb_photos', $data);
return $data;
}
I hope this help.
Use Active Record method after insertion. return last inserted id
function insert($data)
{
$this->db->insert('tablename', $data);
return $this->db->insert_id();
}
Here is reference
This will help you to get last inserted row from the table.
$last = $this->db->order_by('id',"desc")->limit(1)->get('tablename')->row();
print_r($last);
You can get inserted data by below code,
$query = $this->db->get_where('table1', array('id' => $id)); //get inserted data
return $query->result_array();
Refer:https://www.codeigniter.com/user_guide/database/query_builder.html
What about :
$this->db->get('mytable', 1,0)
Try this only with active record of codeigniter. This code gives you the last row of your table.
$this->db->limit(1);
$this->db->order_by('id','desc');
$query = $this->db->get('tablename');
return $query->result_array();
Try this depending on the options and return :
$data = {} //your array;
// now if your $data array contained all the values of the new row just do this
function yourFunction(){
// insert into array into db
$this->db->insert('table_name', $data);
// get created rows id
$id = $this->db->insert_id();
// update your $data array with the new generated id and return it
$data['id'] = $id;
return $data;
}
// Option 2: if row has fields that auto populates (example current timestamp)
function yourFunctionAlt(){
// insert into array into db
$this->db->insert('table_name', $data);
// get created rows id
$id = $this->db->insert_id();
// get row as a array
$_rowArray = $this-db->get_where('table_name',array('id'=>$id)->row_array();
return $_rowArray;
// or get as a object
$_Object = $this->db->get_where('table_name',array('id'=>$id);
return $_Object;
}

Codeigniter: Can I return multiple values from the same function?

I would like to return the query results along w/ the row count without having to run multiple queries. Is this possible in Codeigniter? The following works for returning the query results, but is it possible to also count the number of entries found?
Controller:
$data['records'] = $this->item_model->searchItem($item_name);
Model:
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query->result();
$bindings = array($this->user_id, '%'.$item_name.'%');
$records = $this->db->query($sql, $bindings)->result();
return array(
'records' => $records,
'count' => count($records),
);
Then, in your controller:
$query = $this->item_model->searchItem($item_name);
$data['records'] = $query['records'];
$data['count'] = $query['count'];
Option one
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
$data['result'] = $query->result();
$data['rows'] = $query->num_rows();
return $data;
Option two
// model
$query = $this->db->query($sql, array($this->user_id, '%'.$item_name.'%'));
return $query;
// controller
$data['query'] = $this->item_model->searchItem($item_name);
// then you have $data['query']->result();
// and $data['query']->num_rows();
You can send as many variables from a function as you want ..However plz remember that a function is supposed to do one UNIT of work . thus this rule must not be violated.
we can used contrel structure
if this {return A;} elseif () {return B;} else {return C;}
ALso we can send (bundle) variables in an array and send

Resources