how to search result in codeigniter using age and cast - php

This is model i have to search result by using mysql and codeigniter result searching fine but it show all male but not showing age and cast properly
public function get_selected($age, $cast) {
$this->db->select('*');
$this->db->from('bride_groom_register');
$this->db->like('age', $age);
$this->db->or_like('cast', $cast);
$this->db->where("gender='male'");
$data = $this->db->get();
if ($data->num_rows() > 0) {
return $data->result_array();
} else {
return FALSE;
}
}

Remove like and or_like use inside where condition. If you want provide gender also give in same line.
$this->db->where("age LIKE %$age% or cast LIKE %$cast%");

Try This:
$this->db->query("SELECT * FROM " . $this->db->dbprefix('bride_groom_register') . " WHERE gender = male AND age = '".$age."' AND cast = '".$cast."' ");
Hopefully it will help you.

Why are you using the like clause instead of the where clause?
$this->db->where('age', $age);
$this->db->where('cast', $cast);
or
$this->db->where([
'age' => $age,
'cast' => $cast
]);

Related

Can't retrieve multi mysql query value in Laravel

I have 3 queries like this in my Controller function:
function getDataDetail(Request $request)
{
$id = $request->get('id');
$taskData['taskData'] = DB::select("SELECT NAME_, PRIORITY_, ASSIGNEE_, DUE_DATE_, START_TIME_, END_TIME_, PROC_DEF_ID_ FROM act_hi_taskinst WHERE PROC_INST_ID_ LIKE '%$id%'");
$varData['varData'] = DB::select("SELECT NAME_, TEXT_ FROM act_hi_varinst WHERE PROC_INST_ID_ LIKE '%$id%'");
$imagepath['imagepathData'] = DB::connection('mysql_2')->select("SELECT imagePath FROM images WHERE processInstanceId = $id");
if(count($taskData) > 0 && count($varData) > 0 && count($imagepath) > 0) {
return view('datatrackingdetail', $taskData, $varData, $imagepath);
} else {
return view('datatrackingdetail');
}
}
And I retrieve query value in my view by foreach loop. Problem is my code work fine with only 2 query variables in if function, if there are 3 query variables in my code and it's not work, error say:
"Undefined variable imagepathData" in my view.
How should I fix it? Thank you very much!
Try
DB::connection('mysql_2')->table('images')->select("imagePath")->where(['processInstanceId' => $id]);
Or you shoud use raw select in your query

WHERE_IN returns only first match in codeigniter

I am trying to get a list of coupons through ajax when the checkboxes are selected. So everything else is working fine but the query is returning only the first match.
So my query is:
$this->db->from('tbl_coupons');
if($storeids !=''){
$ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
}
$this->db->where('coupon_cat', $catid);
$this->db->where('coupon_status', 'active');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$ds = $query->result_array();}
According to this my SQLquery becomes
SELECT * FROM `tbl_coupons`
WHERE `coupon_store` IN('1,97')
AND `coupon_cat` = '16'
AND `coupon_status` = 'active'
But this query is returning values with coupon_store=1 and no results are coming for coupon_store=97
I checked values for coupon store 97 which exists in that category.
use below way if data exist it will be part of query.
storeids = explode(',',storeids);
$ids = array();
foreach($storeids as $val){
$ids[] = $val;
}
if(!empty($ids)){
$this->db->where_in('coupon_store', $ids );
}
hope it will create proper sql query
The query is mostly correct, except at line 2, where you need to make the change as:
WHERE coupon_store IN('1','97')
everything else remains the same.

Multiple Like clause CodeIgniter

I'm trying to use multiple 'Like' to look for matches in more than one column.
PHP & mysql:
$area = $this->session->userdata('area');
if($area == 'inbox'){
$this->db->where('receiver',$user);
$this->db->where('receiver_deleted !=','yes');
}elseif($area == 'sent'){
$this->db->where('sender',$user);
$this->db->where('sender_deleted !=','yes');
}
$this->db->like('sender',$k);
$this->db->or_like('msg',$k);
$this->db->from('messages');
$this->db->join('users','users.username = messages.sender','inner');
$this->db->order_by('read','no');
$sql = $this->db->get();
if($sql->num_rows > 0){
return $sql->result();
}else{
echo "failed!";
}
I've also tried:
$this->db->like('sender',$k);
$this->db->like('msg',$k);
the first one nothing happens. The second one only one is executed, in this case the first like. I cannot get both to work at the same time.
Any help will be appreciated,
Mike
for this, you should use Grouping with where clause.
$area = $this->session->userdata('area');
if($area == 'inbox'){
$this->db->where('receiver',$user);
$this->db->where('receiver_deleted !=','yes');
}elseif($area == 'sent'){
$this->db->where('sender',$user);
$this->db->where('sender_deleted !=','yes');
}
$this->db->group_start(); //group start
$this->db->like('sender',$k);
$this->db->or_like('msg',$k);
$this->db->group_end(); //group ed
or_like will produce an OR between the two likes. You can pass an array to like to put multiple with an AND between them.
Have you tried doing:
$this->db->like(array('sender' => $k, 'msg' => $k));
This will search multiple column with OR in between
$this->db->or_like(array('sender' => $k, 'msg' => $k));/* LIKE OR LIKE */
Use this:
$this->db->or_like('location',$this->input->post('searchtext'));
$this->db->or_like('title',$this->input->post('searchtext'));
and you will get exact you want.
You could do something like this:
$this->db->get_where('tablename',$whereclause);
I have the same problem and it worked.

CodeIgniter problem with limit

I have a LoteModel:
public function selecionar($where = NULL, $limit = NULL, $order_by = NULL) {
if(!is_null($where)) {
$this->db->where($where);
}
if(!is_null($limit)) {
$this->db->limit($limit);
}
if(!is_null($order_by)) {
$this->db->order_by($order_by);
}
return $this->db->get(_LOTE_);//_LOTE_ is a constant for my table name
}
and I call it like this:
$config['per_page'] = '5';
$limite = $config['per_page'].','.$this->uri->segment(3); //the segment is ok, I receive it
$lotes = $this->LoteModel->selecionar(array('quadra_id'=>$quadra_id, 'lote_status'=>1), $limite, 'lote_endereco');
I have checked the SQL result for that query:
SELECT * FROM (`realestate_lote`) WHERE `quadra_id` = '1' AND `lote_status` = 1 ORDER BY `lote_endereco`
The thing is, it’s not generating my LIMIT, it will only work if I add it to the LoteModel method.
I’m working with pagination.
Thanks in advance for any help =)
You can't pass a string like "5,1" to CI's ActiveRecord limit function.
The function requires two parameters.
Split the limit string by "," and pass the two values like the example below.
$limit = explode(',', $limit);
$this->db->limit($limit[0], $limit[1]);
Hope this helps...

codeigniter pass array to query

I'm trying to pass an array to a model which has a query. I'm not sure how to correctly pass the array or if i have to manipulate the array somehow.
I have this array:
Array
(
[0] => 1
[1] => 2
)
I have a controller with this line:
$ratings = $this->login_model->get_ratings($mechanicIds); // get the mechanic ratings
I have this model:
function get_ratings($mechanicId)
{
$sql = "select m.mechanic_id,
m.mechanic_name,
m.city,
m.state,
count(mr.rating_id) as num_ratings,
round(avg(mr.rating_id),2) avg_rating
from mechanic m, mechanic_rating mr, rating r
where m.mechanic_id in (?)
and m.mechanic_id = mr.mechanic_id
and mr.rating_id = r.rating_id";
$query = $this->db->query($sql, $mechanicId);
if($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return false;
}
}
It actually returns results, but the problem is it only returns the results 1 row when it should be returning 2 since there are 2 results in my array. Anyone know what i'm doing wrong?
I found this question which helped.
Below is the code I used.
Controller that contains this:
$mIds_size = count($mIds);
$i = 1;
foreach($mIds as $row)
{
if($i == $mIds_size)
{
$mechanicIds .= $row;
}
else
{
$mechanicIds .= $row.', ';
}
$i++;
}
$ratings = $this->login_model->get_ratings($mechanicIds); // get the mechanic ratings
Model which contains this:
function get_ratings($mechanicId)
{
$this->db->escape($mechanicId);
$sql = "select m.mechanic_id,
m.mechanic_name,
m.city,
m.state,
count(mr.rating_id) as num_ratings,
round(avg(mr.rating_id),2) avg_rating
from mechanic m, mechanic_rating mr, rating r
where m.mechanic_id in ($mechanicId)
and m.mechanic_id = mr.mechanic_id
and mr.rating_id = r.rating_id
group by mechanic_id";
$query = $this->db->query($sql, $mechanicId);
if($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return false;
}
}
Change this line:
$query = $this->db->query($sql, $mechanicId);
to this:
$query = $this->db->query($sql, array(implode(', ',$mechanicId)));
as per the manual
To pass an array into a raw query (not built with helper methods) for an IN condition, use the ? placeholder without wrapping in parentheses.
When binding the array to the placeholder, you must declare your array inside of an array. In other words, the "parameters" (2nd) argument of query() method expects an array which relates to each ? in the SQL string. Because the first ? is bound to an array, the $mechanicIds array must be declared as the first element of the "parameters" argument. I have tested this advice to work successfully in a CodeIgniter3 instance that I have access to.
$sql = "SELECT m.mechanic_id,
m.mechanic_name,
m.city,
m.state,
COUNT(mr.rating_id) AS num_ratings,
ROUND(AVG(mr.rating_id), 2) AS avg_rating
FROM mechanic AS m,
mechanic_rating AS mr,
rating AS r
WHERE m.mechanic_id IN ? /* <--- here */
AND m.mechanic_id = mr.mechanic_id
AND mr.rating_id = r.rating_id";
$query = $this->db->query($sql, [$mechanicIds]);
The DB_driver.php core file contains this portion of code in compile_binds() which escapes each value in the array and wraps the comma-joined string in parentheses before returning the sql string.
...
do
{
$c--;
$escaped_value = $this->escape($binds[$c]);
if (is_array($escaped_value))
{
$escaped_value = '('.implode(',', $escaped_value).')';
}
$sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
}
while ($c !== 0);
...

Categories