I am writing a function to count the null column with where condition but there is a problem in this function
protected function _get_mcq_attept_count2( $mcq_id){
$this->load->model('museranswer');
return $this->museranswe>count_by(array('mcq_id'=>$mcq_id,'bookrefrence!='=>" "));
}
this function made the query
SELECT COUNT(*) AS `numrows`
FROM `user_answer`
WHERE `mcq_id` = '321'
AND `bookrefrence` != ' '
this query return the empty column value
I hope this code work for it bcz in code-igniter i always use like this .
protected function _get_mcq_attept_count2($mcq_id)
{
$this->load->model('museranswer');
$where = array('mcq_id'=>$mcq_id);
return $this->museranswe>count_by($where);
}
/******************* FOR MODEL *********************/
public function count_by($where)
{
$this->db->select('count(mcq_id) as numrows');
$this->db->from('user_answer');
$this->db->where($where);
$this->db->where('bookrefrence !=',' ');
$qry = $this->db->get();
return $qry->result_array();
}
Change your query like this array("mcq_id" => "$mcq_id", "bookrefrence IS NOT NULL" => null). Hope you will get right answer. If it does not work, share your model with us.
return $this->museranswer->count_by(array('mcq_id'=>$mcq_id,'length(bookrefrence)>2'));
Related
I tried to fetch data using joins and the data is repeating,
The controller code is:
public function searchjobs2()
{
//$id=$_SESSION['id'];
$lan = $_POST["picke"]; //var_dump($id);die();
$value['list']=$this->Free_model->get_jobs($lan);//var_dump($value);die();
$this->load->view('free/header');
$this->load->view('free/searchjobs2',$value);
}
And the model:
public function get_jobs($lan)
{
$this->db->select('*');
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work.login_id = tbl_work_stats.login_id",'inner');
$this->db->where("language LIKE '%$lan%'");
// $this->db->where('tbl_work_stats.login_id',$id);
$this->db->order_by('insertdate','asc');
$query=$this->db->get()->result_array();//var_dump($query);die();
return $query;
}
I have used
foreach ($list as $row){
...
}
for listing.
Using distinct will remove duplicate fields:
$this->db->distinct();
From what I can see, your query has ambiguity, and an error in the join statement, also your where like is part of the problem, I would recommend trying this even do there are some missing info, find out wich field you need to join from the second table.
public function get_jobs($lan){
$this->db->select("tbl_work_stats.*, tbl_work.fields");
$this->db->from("tbl_work_stats");
$this->db->join("tbl_work", "tbl_work_stats.login_id = tbl_work.login_id","inner");
$this->db->where("tbl_work.language LIKE", "%" . $lan . "%" );
$this->db->order_by("tbl_work_stats.insertdate","asc");
$query=$this->db->get()->result_array();
return $query;}
do you mean to join on login_id?
I am guessing that is the user logging in and it is the same for many entries of tbl_work_stats and tbl_work.
you didn't post your schema, , but login_id doesn't seem like right thing to join on. how about something like tbl_work.id = tbl_work_stats.tbl_work_id or similar?
also CI $db returns self, so you can do:
public function get_jobs(string $lan):array
{
return $this->db->select()
->from('tbl_work_stats')
->join('tbl_work','tbl_work.id = tbl_work_stats.work_id')
->like('language',$lan)
->order_by('insertdate')
->get()
->result_array();
}
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();
}
I am in new in Laravel. I want to make dynamic where queries with laravel query builder.Normally I can make dynamic queries in php
$where = array(
'hello' => 'world'
);
function get($where = null){
if($where == "") $where = "";
//Function which converts where clause into queries
wheretoqueries($where); //converts where clause
$sql = "SELECT * FROM $tbl $where";
return $sql;
}
echo get($where);
If where clause is null queries will be
SELECT * FROM $tbl
If where clause is not null queries will be
SELECT * FROM $tbl WHERE hello = "world"
Laravel orm works fine for where clause if key and value exists
A::where($where)->get();
If where is null following method will not work
You can chain the where queries as:
$query = Model::query();
if (!empty($value)) {
$query->where('column', $value);
}
$query->get();
OR
You can use when method as:
Model::when($value, function ($query) use ($value) {
return $query->where('column', $value);
})
->get();
Try this. If $where variable contain something then the query will execute otherwise it will retrieve all data from A Model.
function get($where = null){
if($where != null){
A::where('field_name', '=', $where)->first();
}else{
A::all();
}
}
Note: if your query return more than one value then you have to use get() method at end of query builder instead of first();
Reference: https://laravel.com/docs/5.3/queries#where-clauses
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;
}
how can I sort the return data from a query using whereHas? In my query i have to get the users data which id exist on interest table but i need to sort it using the datetime column from interest. But, the return query do not sort the data at all.
Here's my code snippets in case it would help you understand my problem.
Model (name: User)
//***relationship***//
public function interest(){
return $this->belongsTo('Interest','id','interest_by');
}
Controller
$userInterested = User::whereHas('interest',function($q) use ($id) {
return $q->where('interest_on', $id)
->orderBy('datetime');
});
$userQuery = $userInterested->get();
return $userQuery;
remove return from where has and try this.like
$userInterested = User::whereHas('interest',function($q) use ($id) {
$q->where('interest_on', $id)
->orderBy('datetime');
})->get();
return $userInterested;
$userInterested = User::whereHas('interest',function($q) use ($id) {
return $q->where('interest_on', $id);
})->orderBy('datetime');
$userQuery = $userInterested->get();
return $userQuery;