I have a query in model using active record , my query is like this
SELECT MID(id,1,1) id_depan_user,MID(id,5,10) id_belakang_user ,
id, nama_lengkap from user where id like '_001%'
how to convert the part in where like condition id like '_001%' I want to make that to be active record, my problem just at like condition like that.
this is my full code
function get_id_child($id_parent){
$this->db->select('MID(id,1,1) id_depan_user',false);
$this->db->select('MID(id,5,LENGTH(id)) id_belakang_user',false);
$this->db->select('id');
$this->db->from('user');
$this->db->like('id', '_'.$id_parent,'after');
$query =$this->db->get();
return $query->result_array();
}
$this->db->like('id ', '_001', 'after');
Use this . you need to concate your variable with _ and assign it to variable your also remove quotes from like query
function get_id_child($id_parent) {
$id = "_" . $id_parent;// assign it to variable
$this->db->select('MID(id,1,1) AS id_depan_user', FALSE);
$this->db->select('MID(id,5,10) AS id_belakang_user', FALSE);
$this->db->select('id');
$this->db->select('nama_lengkap');
$this->db->like('id', $id, 'after');// use after in your query
$query = $this->db->get("user");
$result = $query->result_array();
}
Related
I am trying to retrieve the config_value on the basis of config_name from the table as shown in image in codeigniter project. Even i am not able to understand where to start.I find something like this on internet (for Sample).
$this->db->select('age');
$this->db->where('id', '3');
$q = $this->db->get('my_users_table');
$data = $q->result_array();
echo($data[0]['age']);
Do something like this :
$this->db->where('config_name', 'Account_activation');
$q = $this->db->get('my_users_table');
/* if u r fetching one row use row_array instead of result_array*/
$row = $q->row_array();
echo $row['config_value'];
For more : https://www.codeigniter.com/user_guide/database/index.html
If you want to retrieve config value on the basis of config name you can simply add an where condition in select query just like I am giving here, put whatever config_name you want to retrieve the query and it will print the config value which is there in the same row.
$this->db->select('config_value');
$q = $this->db->get_where('my_users_table',array('config_name'=>'home_page'));
$row = $q->row_array();
echo $row['config_name']; // this will print index.php
Little different than the other answers although functionally the same. You could make a helper and include this function. Really only useful if you only want to get one config item at a time.
function config_item_db($key) {
$CI = & get_instance();
$query = $CI->db->get_where('my_users_table', array('config_name' => $key));
if ($query->num_rows() == 1) {
return $query->row()->{$key};
} else {
return false;
}
}
Use codeignitor's get_where() method and pass the select condtions in an array. Example:
$this->load->database();
$this->db->get_where('table_name', array('database_column_title' => $value1,
'database_column_title' => $value2));
Search results shows all the records when the keywords input is empty, how can I display a message that says search term not found?
What if I want to add another input value too, like input->post('date') ?
Controller:
$keywords = $this->input->post('keywords');
$data['results'] = $this->search_model->get_results($keywords );
$this->template->show('results',$data);
Model:
function get_results($keywords = 'default'){
$query = $this->db->select('*')->from('list')
->where("name LIKE '%$keywords%'")->get();
return $query->result_array();
}
Try using codeigniter $this->db->like method.
If your $keyward is empty then try this...
$keywords = $this->input->post('keywords');
if(!empty($keywords)){
$data['results'] = $this->search_model->get_results($keywords );
}else{
$data['result']="not found";
}
$this->template->show('results',$data);
Or
function get_results($keywords = null){
if(!$keywords){
return false;
}
$query = $this->db->select('*')->from('list')->like("name",$keywords)->get();
return $query->result_array();
}
Every time your query run and return data because you pass $keyword='default' so if $keyword is empty then it search using default
For more.....
It is because you have a like statement.
So if you write,
select * from list where name like '%%';
It will return all.
if you have a empty keyword better you not go for database, since you don't have anything to search.
I've got a table in which a field contains pattern Like this [{"vendor":"10","status":"paid"}] :
table
I want to make a query 'like' in codeigniter , but I got an error:
model :
function get_total_order($id_vendor){
$this->db->like('payment_status', 'vendor":"'.$id_vendor.'","status":"due');
$this->db->from('sale');
return $this->db->count_all_results();
}
view :
<?php
$new_order = $this->crud_model->get_total_order($this->session->userdata('vendor_id'));
echo "<h1>".$new_order."</h1>";
?>
when i run this, i got blank page, how i fix this?
thanks.
Since you use "Like" query type, you should add '%' in the query argument or send a complete argument:
function get_total_order($id_vendor)
{
$this->db->like('payment_status', '%vendor":"'.$id_vendor.'","status":"due%');
$this->db->from('sale');
return $this->db->count_all_results();
}
Try this:
function get_total_order($id_vendor){
$this->db->like('vendor',$id_vendor);
$this->db->like('status',"due");
$this->db->from('sale');
return $this->db->count_all_results();
}
if your searching json data so you have pass the data in like query and like query data should be look like data inside the table how it looks .
your query should be something like this
<?php
$id_vendor =123;
$ss= '%"vendor":"'.$id_vendor.'","status":"due"%';
$sssss ="select * from sale where payment_status like '$ss' ";
echo $sssss;
query look like this
select * from sale where payment_status like '%"vendor":"123","status":"due"%'
?>
and also you can use wildcard (%) more place with your wish.
You can customize where as per your requirement with and condition or another condition.
$where = "payment_status like '%$id_vendor%' OR status like '%$status%'";
$this->db->where($where);
try this one:
Because 'like is time consuming.
function get_total_order($id,$vendor)
{
$this->db->where('vender', $id);
$this->db->where('status',$vendor);
$this->db->get('sale');
$result=$res->result_array();
return $result;
}
You can use $this->db->where_in() like below:-
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
// Produces: WHERE username IN ('Frank', 'Todd', 'James')
For more details, please check below link:-
https://www.codeigniter.com/userguide2/database/active_record.html
I am trying to write this below query in codeigniter format and getting some problem with the aggregate function:
$stmt = "select sum(subscription_amt) as samt, bill_month,
sum(loan_refund_amt*no_of_loan_installment+error_amt) as lamt
from pf_bill_det
where trim(pf_number)='$pfno'
and fin_year='$fyear'
and aproved='Y' group by bill_month";
$query = $this->db->query($stmt);
This query ending up with an error loan_refund_amt*no_of_loan_installment+error_amt is not a column. Please help me how to write this query using codeigniter query format.
why don't you try this
$this->db->select("COUNT(*) AS MyCount");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
OR
$this->db->select("SUM(field_name) AS MySum, username, password");
$this->db->from("MyTable");
$this->db->where("field", $value);
$this->db->get();
BUT
In simple query function you can use
$query = $this->db->query("SELECT COUNT(field_name) AS total_names, fname");
$query->result(); \\ Returns an array of objects
$query->result_array(); \\ Returns result as a pure array
$query->row(); \\ Returns a single result and first row
Try this,
$query=$this->db->query("select sum(subscription_amt) as samt,bill_month,sum(`loan_refund_amt`*`no_of_loan_installment`+`error_amt`) as lamt from pf_bill_det where trim(pf_number)='$pfno' and fin_year='$fyear' and aproved='Y' group by bill_month");
I want to perform this query wth active records :
SELECT * FROM tablename WHERE status = 'A' AND name LIKE 'test'
And i want it to return an array because i want to encode it to json later, so i need to use result_array();
So i tried something like this :
$query = $this->db->select('*')->from('tablename')->where('status', 'A');
$query->like('name', 'test')->get()->result_array();
return $query;
But i got this message when i tried to encode it to json :
type is unsupported, encoded as null
What should i do? Thanks for your help.
Try this:
$data = array();
$rs = $this->db->where('status', 'A')->like('name', 'test')->get('tablename');
if($rs->num_rows()> 0){
$data = $rs->result_array();
}
return $data;
this structure of code doesn't give any error :-
$this->db->where('status', 'A');
$this->db->like('name', 'test')
$query=$this->db->get(tablename);
$data=$query->result();
$json_data=json_encode($data);