Im trying to get the next row everytim ei run the function but it always returns 0
Model
function getNextMuziek($id)
{
$this->db->where('id', $id);
$this->db->limit(1);
$query = $this->db->get('muziek')->num_rows() == 1;
return $query->next_row();
}
According to the documentation, next_row() is to be used to navigate through a result set. This will not serve your purposes. If you want your function to return the next row after the row specified by $id, the easiest thing would be to use a greater than comparison and return the first row. You could modify your code to
$query = $this->db->where('id >', $id)->limit(1)->get('muziek');
if ($query->num_rows() == 1) {
return $query->row();
}
Related
In this image, 5 record are with id 80, but when i fetch them they all are coming but i want to show just one record only.
My rest code is below here
$data['query7'] = $this->ORB_Model->get_skilldash();
public function get_skilldash()
{
$this->load->database('orb');
//$this->db->distinct('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' => $this->session->userdata('master_id')));
return $query->result();
}
Use $query->row() instead of $query->result();
public function get_skilldash()
{
$this->load->database('orb');
$master_id = $this->session->userdata('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' =>$master_id));
return $query->row();
}
If you want a specific row returned you can submit the row number as a digit in the first parameter:
$row = $query->row(3);
Or Simply use it with $this->db->distinct(); do like this:
$this->load->database('orb');
$this->db->distinct();
$master_id = $this->session->userdata('master_id');
$query = $this->db->get_where('skills_tb', array('master_id' => $master_id));
return $query->row();
for more : https://www.codeigniter.com/user_guide/database/results.html#result-rows
public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return $this->db->count_all_results();
}
This is the function I have in my model that counts all rows in my 'tasks' table where the column 'task_status' is equal to 1. It basically returns an integer that I use for my pagination. The query is not working as it doesn't return any data even though I know I have 4 rows that match that requirement in my table.
I was gonna use the returned value for my pagination. Please help. Thanks
Try
return $q->num_rows();
Or
$this->db->where('task_status', '1');
$query = $this->db->get('tasks');
return $query->num_rows();
$this->db->count_all_results(); is not working after the get function, so do this instead:
public function countTasks(){
$q = $this->db->get_where('tasks', array('task_status' => 1));
return return $q->num_rows();
}
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;
}
I am using codeigniter active record class.
What I am trying here is calling get_users method from controller with parameter $user_id.
If $user_id is null get me all the entries from users table otherwise it should give me the entries that match the array passed with get_where() method.
But get_where method is not working instead get() method does the purpose with same parameters. Why would that happen? Thanks.
public function get_users($user_id=null)
{
if($user_id === null){
$q = $this->db->get('users');
}
elseif (is_array($user_id)) {
$q = $this->db->get('users', $user_id);
//$q = $this->db->get_where('users', $user_id); // not working
$result = $q->row_array();
}
return $q->result_array();
}
Your usage of get_where() is wrong:
It should be:
$q = $this->db->get_where('users', array('userid_fieldname_in_table'=>$user_id));
The second parameter should be an associative array where the key is the table's fieldname and the value is the value to look for.
Since you are passing an array it is better to use:
$this->db->where_in('filename', $array_of_values());
You can read more in the documentation
I have the following codeigniter model function:
function get_successful($project_id, $amount_backed){
$data = '';
$this->db->where('id', $project_id);
$this->db->where($amount_backed >= 'funding_goal'); //HELP HERE
$this->db->where('published', '1');
......
return $data;
}
I need to only get records where the variable $amount_backed is higher or equal to the field 'funding_goal'.
How can this be done with codeigniters active record?
One possibility would be:
$this->db->where('funding_goal <=', $amount_backed);
Also see the CodeIgniter User Guide, search for $this->db->where(); and look at Custom key/value method, there is following example:
$this->db->where('id <', $id);
P.s.: there are two more alternatives: Associative array method and Custom string.
you can use :
$this->db->where("funding_goal < $amount_backed")
a < b == b>=a
function get_successful($project_id, $amount_backed){
$data = '';
$this->db->where('id', $project_id);
$this->db->where("funding_goal < $amount_backed"); //HELP HERE
$this->db->where('published', '1');
......
return $data;
}