Grouping Where Clauses in Codeigniter

multiple where condition codeigniter

you can use an array and pass the array.

Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);

$this->db->where($array);

// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

Or if you want to do something other than = comparison

$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);

$this->db->where($array);

CodeIgniter - Grouping where clause

You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions

public function _compile_select($select_override = FALSE)
public function _reset_select()

Now subquery writing in available And now here is your query with active record

$this->db->where('message_from','2');
$this->db->where('message_to','1');

$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();

$this->db->where('message_from','1');
$this->db->where('message_to','2');

$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();

$this->db->select('*');
$this->db->where("$subQuery1");
$this->db->or_where("$subQuery2");
$this->db->get('messages');

Look at this answer of mine. This shows how to use sub queries. This will help

Using Mysql WHERE IN clause in codeigniter

EDITES

Yes i have done it
Rewrite the query this way exactly you want

$this->db->where('message_from','2');
$this->db->where('message_to','1');

$subQuery1 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();

$this->db->where('message_from','1');
$this->db->where('message_to','2');

$subQuery2 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();

$this->db->select('*');
$this->db->where("($subQuery1)");
$this->db->or_where("($subQuery2)");
$this->db->get('messages');

Compile select is with true parameter. Will not produce select clause. This will produce

SELECT * FROM (`messages`) WHERE (`message_from` = '2' AND `message_to` = '1') OR (`message_from` = '1' AND `message_to` = '2')

Grouping WHERE clause in CodeIgniter

@$i = 1;
$n = $this->input->post('timetable_id'.$i);
$or_where = "(";
$or_where.="timetable_id = $n";
for ($i; $i <= 6; $i++) {
$n = $this->input->post('timetable_id'.$i);
$or_where.=" OR timetable_id = $n";
}
$or_where.=")";
$this->db->or_where($or_where);

This will give your expected answer.

How to use OR_Where with AND condition in CodeIgniter?

Hope this will help you :

Use query grouping like this : change your table name and variables according to you

$firstname = 'a';
$lastname = 'b';
$customer_mobile = 'd';
$this->db->select('*')->from('users')
->group_start()
->where('a', $firstname)
->where('b', $lastname)
->where('c', '1')
->or_group_start()
->where('d', $customer_mobile)
->where('e', '1')
->group_end()
->group_end()
->get();
echo $this->db->last_query();

The output will be like this :

SELECT * FROM `users` 
WHERE ( `a` = 'a' AND `b` = 'b' AND `c` = '1'
OR ( `d` = 'd' AND `e` = '1' ) )

For more : https://www.codeigniter.com/user_guide/database/query_builder.html#query-grouping

codeigniter select query with multiple where clause

You can use below code for multiple user defined where clause.

<?php
class Select_Model extends CI_Model
{
public function get_data()
{
if($this->input->post('submit'))
{
$this->db->where('country' => $this->input->post('country'));
$this->db->where('state' => $this->input->post('state'));
$this->db->where('city' => $this->input->post('city'));
$query = $this->db->get('tablename');
return $query->result();
}
}
}
?>

how can i use multiple where clause In codeigniter?

You can chain database clauses, so you would write it as

$this->db->where('category_name','case')->where('publication_status','case')->where('home_headline_sub','case');

This would generate a query's WHERE clause as

// WHERE category_name = 'case' AND publication_status = 'case' AND home_headline_sub = 'case'

Documentation here: http://ellislab.com/codeigniter/user-guide/database/active_record.html#chaining

WHERE clause in the query doesn't work properly in CodeIgniter 3

You have this way too complicated, avoid using php loops to get data which you can get by a simple query.

just use this mysql query, counting the isAvailable rows, when true (1):

$sql=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount 
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";

note: I've changed the alias count to mycount, since count is a reserved word.

your function would look so:

public function bloodTypesChart()
{
$sql=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";

$chartData = [];
$blood_types = $this->db->query($sql)->result_array();

foreach($blood_types as $row)
{
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['mycount'];
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}

here is a sql-fiddle executing the query using a simplified version of your database

Multiple column where_in clause in codeigniter

Assume your data array is like this(should be)

$val1 = array(1,2);
$val2 = array(2,3);

And query should be

$this->db->select('*');
$this->db->from('tab1');
$this->db->where_in('col1',$val1);
$this->db->or_where_in('col2',$val2);
$query = $this->db->get();
$result = $query->result_array();

Or else you can use

$this->db->query("select * from tab1 where (col1,col2) in ($val1,$val2)");


Related Topics



Leave a reply



Submit