array for multiple where_in condition in codeigniter [duplicate] - php

This question already has an answer here:
Codeigniter model with multiple update conditions using manual where statement
(1 answer)
Closed 5 years ago.
$this->db->where('column_field1',$value1);
$this->db->where('column_field2',$value2);
For above two query we can write a single query as:
$arr = array('column_field1'=>'value1', 'columne_field2'=>'value2');
function select($arr){
.. .. ..
... .. . ..
$this->db->where($arr);
}
is there any solution for the where_in query:
function($value1, $value2){
$this->db->where_in('column_field1',$value1);
$this->db->where_in('column_field2',$value2);
}
as I have tried but didn't work:
arr = array('column_field1'=>$arr,'column_field2'=>arr2);
function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where_in($arr);
$query = $this->db->get();
return $query;
}
I want to combine the where_in condition so that i can store multiple column_field and array for it.
$this->db->where_in($arr);
where $arr contains pair of column_filed and array_of_value:
$arr = $array('column_field'=>$arr_of_value);

function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where($arr); // change here
$query = $this->db->get();
return $query;
}
If you want multiple where In then you need to write it twice....It's not possible in single statement.
$this->db->where_in('field1',$cond1);
$this->db->where_in('field2' , $cond2);
Note: Where_in is similar to where id IN (1,2,3...)but in your case you are doing multiple where condition.

Creating custom method is a reasonable solution for this, you can either extend database's active record class or write custom function which takes array and call where_in like below
function multiple_where_in($array){
foreach($array as $key => $data){
$this->db->where_in($key, $data);
}
}
And call like below
function select_in($arr)
{
$this->db->select('*');
$this->db->from('your_table');
$this->multiple_where_in($arr); // call local function created above
$query = $this->db->get();
return $query;
}
// create array like below, fieldname and field_values inside array
$arr = array(
'column_field1'=>array('value1','value2'),
'columne_field2'=>array('value2','value3')
);
// call your select method
$this->select_in($arr);

You can try this solution for your problem.
$this->db->select('*');
$this->db->from('table');
if(!empty($cond1) && !empty($cond2)){
$this->db->where("field1 IN (".$cond1.") AND field2 IN (".$cond2.") ",null,false);
}
$query = $this->db->get();
return $query;
I hope this will helps you. Thanks!

Related

How to make a join and search in data using codeigniter and Php

I make a join between two tables using codeigniter framework and this is my query:
public function SearchDataUnderCondition($firsttable,$secondtable,$data)
{
$this->db->select("atm.* , tender.status as tenderstatus");
$this->db->from($firsttable);
$this->db->join($secondtable,'atm.id_tender=tender.id');
$this->db->where('tenderstatus','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
I am using database model when is remove
$this->db->where('tenderstatus','1');
from my code operation done and i get result but I want to make search under this condition. what is my problem?
Try this
public function SearchDataUnderCondition($data)
{
$this->db->select('atm.* , tender.status');
$this->db->from('atm');
$this->db->join('tender','atm.id_tender=tender.id');
$this->db->where('tender.status','1');
$this->db->like('serial', $data);
$sql = $this->db->get();
return $sql->result();
}
Try this
$this->db->where('tender.status','1');
I think, we can not aliases in where clause
Using aliases (and also making sure you want and WHERE AND LIKE clause):
$fist_table = 'atm';
$second_table = 'tender';
$this->db->select('aliasone.* , aliastwo.status as "tenderstatus"');
$this->db->from("$first_table as aliasone");
$this->db->join("$second_table as aliastwo", "aliastwo.id = aliasone.tender_id");
$this->db->where('aliastwo.status','1');
$this->db->like('serial', $data);
return $this->db->get();

Codeigniter Active Record Class Error vs MySQL Raw Query

In my Model, I wrote this function with MySQL raw query
function get_question_result($lr_id)
{
$raw_query = 'SELECT question_record.qr_id, LEFT(question.question, 50) as question,
question.correct_answer
FROM question_record
INNER JOIN question ON question.q_id = question_record.q_id
WHERE question_record.lr_id = '.$lr_id.' ';
$query = $this->db->query($raw_query);
$questionresult = $query->result_array();
return $questionresult;
}
It worked fine. It gave me the array I want. I continued my project.
Then suddenly I was curious to try it in CI Active Record Class.
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer');
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
It didn't work. It gave me this error
PHP Fatal error: Call to a member function result_array() on a non-object
Just out of curiosity, where did I do wrong?
Was it me writing it wrong or the result data structure with Active Record is just different?
'cause when I tried it again in Active Record without selecting this field
LEFT(question.question, 50) as question
It worked but it didn't give the field I want. Do you guys know why?
In your $this->db->select() call you need pass FALSE as second parameter so that active record will not try to add backticks ` for your columns in select statement
function get_question_result($lr_id)
{
$this->db->select('question_record.qr_id, LEFT(question.question, 50) as question, question.correct_answer',FALSE);
$this->db->from('question_record');
$this->db->join('question', 'question.q_id = question_record.q_id', 'inner');
$this->db->where('question_record.lr_id', $lr_id);
$result = $this->db->get()->result_array();
return $result;
}
According to docs
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names with backticks. This is useful if you need a compound select
statement.
$this->db->select();

what's wrong with my sum query in model php codeigniter?

here's my model code :
function total($id)
{
$this->db->select_sum('score');
$q = $this->db->get('my_table');
$this->db->where('id',$id);
$this->db->group_by('id');
return $q->row()->score;
}
why the output still sum all of row not the specific row with id?
$this->db->get() actually runs the query. You need to call that last.
function total($id)
{
$this->db->select_sum('score');
$this->db->where('id',$id);
$this->db->group_by('id');
$q = $this->db->get('my_table');
return $q->row()->score;
}

Merging array outputs from database in Codeigniter, is this the way how it is done?

I figured a way how to combine two array outputs from a function in another function in the Codeigniter model class. It works well as I expected, but I suspect that this way is not the correct way.
public function get_job_detail($job_id){
$this->db->from('jobs');
$this->db->where('id', $job_id);
$job_details = $this->db->get();
$jobdetails = $job_details->row();
$jobcompany = $this->get_company_details($jobdetails->company_id);
$foo = array();
foreach($jobdetails as $key=>$value){
$foo[$key]=$value;
}
foreach ($jobcompany as $key => $value) {
$foo[$key]=$value;
}
return $foo;
}
public function get_company_details($company_id){
$this->db->from('companies');
$this->db->where('id', $company_id);
$query = $this->db->get();
return $query->row();
}
So in the view page, I normally access the values by using the echo $myvariable->xyz but for some reason the return foo is not an object, therefore i had to access by using the array style echo $myvariable['xyz']. I have been looking around for answers but I couldn't find any (or maybe i am not using the correct keywords).
Is there suppose to be a correct way? or Codeigniter way to do this??
Many thanks!
$this->db->select('*')
->from('jobs')
->join('companies', 'companies.id=jobs.company_id', 'left');

passing multiple arguments to sql query in code igniter without using active records [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed last month.
I have already researched on this but didnt come to get the correct answer, I am trying to pass multiple arguments to an sql query in code igniter without using active records and its not working for me, see what I have done below
in my model I have:
function get_values($id, $age)
{
$sql='SELECT * FROM tblRegister where id=? AND unit=?';
$query=$this->db->query($sql, array('$id','$age'));
return $query->result_array();
}
in the controller I have:
function get_values()
{
$result=$this->register_model->get_values(32, 23);
}
this doesnt work for me, i think its an error with passing the arguments to the query, how to I format the syntax so that it works just fine? Regards
Try variables without quotation marks: array($id, $age)
function get_values($id, $age)
{
$sql='SELECT * FROM tblRegister where id=? AND unit=?';
$query=$this->db->query($sql, array($id,$age));
return $query->result_array();
}
//do it as:
function get_values($id, $age)
{
$this->db->where("id",$id);
$this->db->where("unit",$age);
$query=$this->db->get("tblRegister");
return $query->result_array();
}
// try this in your model
function get_values($id, $age)
{
$array = array('id' => $id, 'unit' => $age);
$this->db->select('*');
$this->db->from('tblRegister');
$this->db->where($array);
$query=$this->db->get();
return $query->result_array();
}
TRY THIS
function get_values($id, $age)
{
$this->db->where('id',$id);
$this->db->where('age',$age);
$query=$this->db->get('tblRegister');
return $query->result_array();
}

Categories