I made this function in codeigniter:
public function get_users_pagination($q, $perpage, $pagenr, $type = 1)
{
$query = $this->db->get_where('korisnici_tbl', array("type" => $type), $perpage,$pagenr);
$this->db->like('email', $q);
$this->db->or_like('name', $q);
return $query->result();
}
But when the result is less than the $perpage variable it still returns rows in the amount of $perpage.
So if the $pagenr is for example 10 and there are 2 rows with type 1 it just returns 8 other rows of type 2 with it.
You can group the ORs like this
$like = '(email LIKE \'%'.$q.'%\'';
$like .= ' OR name LIKE \'%'.$q.'%\')';
$this->db->where($like);
You can try like this way :
$query = $this->db->get_where('korisnici_tbl', array("type" => $type));
$this->db->like('email', $q);
$this->db->or_like('name', $q);
$this->db->limit($pagination_start, $pagination_length);
Just put on your model function:
$limit = 3; //maximum row's which you want to show in page's:
Related
I use codeigniter and store my data like 123,234 .
My table name is schedule and column name batch.
$batch = 123;
$lot =1;
$dataTest = $this->db->query("select q_set from schedule where lot='$lot'
and batch='$batch'")->row();
see my image below
I try to select one value and match my sent value. ## how to match both the values ##
the value of column batch in database is '123,321', and the variable $batch is 123,they are not equal so you can't select it.
you must send a data equal to the one in your database.
try
$lot=1;
$batch = 123321; //or $batch='123,321' i'm not sure the date type of variable $batch
$dataTest = $this->db->query("select q_set from schedule where lot='$lot' and batch='$batch'")->row();
Try this
$batch = "123,321";
$lot =1;
$dataTest = $this->db->query("select q_set from schedule where lot=".$lot."
and batch='".$batch."')->row();
or use
$batch = "123";
$lot =1;
$dataTest = $this->db->query("select q_set from schedule where lot=".$lot."
and batch like '%".$batch."')->row();
Using query builder. Please note if batch is a string it should be $batch = "123,234" plus you can't have commas with integers.
$this->db->select('q_set');
$this->db->where('lot', $lot);
$this->db->where('batch', $batch);
$q = $this->db->get('schedule');
if ($q->num_rows() > 0}
print_r($q->row());
} else {
echo 'no rows';
}
you can also use find_in_set.but it mostly used to find comma seperated values.
$this->db->select('q_set');
$this->db->from("schedule");
$this->db->where("FIND_IN_SET('$lot', lot)");
$this->db->where("FIND_IN_SET('$batch', batch)");
--------------OR---------------
you can try this..
$this->db->select('q_set');
$this->db->from('schedule as S1');
$this->db->where('S1.lot',$lot);
$this->db->where('S1.batch',$batch);
$query = $this->db->get();
return $query->row();
You can use IN Operator,
$result = $this->db->select('*')->from($table);
$result = $this->db->where('lot =',$lot);
$result = $this->db->where_in('batch',$batch);
$result = $this->db->order_by('id','desc');
$result = $this->db->get();
The where_in() operator checks if the value exists in the specified column
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
]);
function renew_status($propertyId){
$code = '';
$code = mt_rand(500000, 999999);
$q = "UPDATE `ps_listings` SET
`accessCode` = $code,
`renew` = '0'
WHERE `id` = $propertyId ";
mysql_query($q) or die(mysql_error());
$this->db->select('accessCode')->from('ps_listings');
$this->db->where(array('id' => $propertyId));
$query = $this->db->get();
$results = $query->result();
return $results[0]->accessCode;
}
I am updating a mysql table through codeigniter function. first time when i access my function it updates wrong number but if i refresh the page correct value is updated.
return $results[0]->accessCode; gives me this code 893195. while in database it saves 997228. Please Help me
Try this
function renew_status($propertyId){
$code = '';
$code = mt_rand(500000, 999999);
$data = array('accessCode' => $code, 'renew' => '0');
$this->db->where('id',$propertyId);
$this->db->update('ps_listings',$data);
$this->db->select('accessCode');
$this->db->where('id',$propertyId);
$query = $this->db->get('ps_listings');
$results = $query->result_array();
return $results[0]->accessCode;
}
1) Use mysql_affected_rows for check works update or no.
2) I think refresh page is wrong way. Use Session for deny double update.
3) For debugging retrieve and print accessCode before updating, after, and in
result(UI).
1) is "id" column unique and not null?
2) what does the following code returns :
echo count($results);
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...
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);
...