Codeigniter active query error - codeigniter

Here is my query
$this->db->select('amount AS SavingAmount');
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientId'=>$user['UserID'],'year'=>$year));
On checking in browser its generating this query below
SELECT *, *, `amount` AS SavingAmount FROM (`cashman_trial_balance`) WHERE `category_id` = '176' AND `clientId` = '122' AND `year` = '2015/2016'
I dont know from where the two stars coming from?
Controller code
$som_var = $this->client->statistics();
Model code
function statistics()
{
/* Some other queries */
$b = $this->get_balances();
}
function get_balances()
{
$this->db->select('amount AS SavingAmount');
$query = $this->db->get_where('cashman_trial_balance',array('category_id'=>'176','clientI‌​d'=>$user['UserID'],'year'=>$year));
return $query->result();
}

use this
$query = $this->db->query("SELECT amount AS SavingAmount FROM table_name WHERE category_id = '176'
AND clientId = '$user['UserID']' AND 'year'= '$year') ");
$result = $query->result_array();
return $result;

You can use the simple way
$query = $this->db->query("SELECT amount AS SavingAmount FROM cashman_trial_balance WHERE category_id = '176' AND clientId=$user['UserID'] AND year=$year");

Related

how to check out of function in codeigniter

i am beginner in codeigniter and want to check the out put of a function in codeigniter so that i can debug it but i am unable to do so for example here is my function in my managementmodel and i have put the slashes before the print_r function. when i want to see the result i remove these but i do not know where to see the result if in view then how please help me. thanks
function department()
{
$sql = "SELECT * FROM department ORDER BY id
ASC";
$query = $this->db->query($sql);
// echo "<pre>";
// print_r($query->result_array());exit;
return $query->result();
}
function get_test_list_info()
{
$sql = "SELECT * FROM test_list ORDER BY id
DESC";
$query = $this->db->query($sql);
//print_r($query->result_array());exit;
return $query->result();
}

AND, OR use In Join query in Codeigniter

How to use Below query in Codeigniter
SELECT * FROM (post) JOIN user ON user.user_id=post.user_id JOIN friend ON (friend.user_id=post.user_id AND friend.friendship_id=1) OR (friend.user_id=1 ANDfriend.friendship_id=post.user_id) WHERE post.type = 'friend' ORDER BY postid desc
You can use this.
$this->db->query(your query);
like this code.
$sql = "SELECT * FROM table WHERE user_id = 1";
$this->db->query($sql);
try this
$query = $this->db
->select('*')
->from('post')
->join('user', 'user.user_id = post.user_id')
->join('friend', 'friend.user_id = (post.user_id AND friend.friendship_id=1) OR (friend.user_id = 1 AND friend.friendship_id = post.user_id)')
->where('post.type', 'friend')
->order_by('postid','DESC')
->get();
$arrResult = $query->result();

Not getting correct SUM amount for users votes from thread and reply table

I am using codeigniter 3.1.0 and I am trying to get the total SUM of two columns from different tables
If my user_id = 1 it should return the total sum of 421 but returns 431 It is adding the votes from other users on my reply table
Question Using Active Record How can I make sure the SUM is getting the correct amount for the users id.
Model Function
public function thread_reputation($user_id) {
$this->db->select('SUM(reply.votes + thread.votes) AS reputation');
$this->db->from('thread', 'left');
$this->db->join('reply', 'reply.user_id = thread.user_id', 'left');
$this->db->where('thread.user_id', $user_id);
$this->db->where('reply.user_id', $user_id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
Controller section where i echo it
$thread_reputation = $this->thread_analytics_model->thread_reputation('1');
echo $thread_reputation->reputation;
Thread Table
Reply Table
Change your query with this
$query = $this->db->query("SELECT SUM(A.votes) as reputation from
(
SELECT reply.votes FROM reply where reply.user_id = $user_id
UNION ALL
SELECT thread.votes FROM thread where thread.user_id = $user_id
)AS A");
Tested and properly working
Check Screenshot:-
Query using ACTIVE REOCRD
$this->db->select("SUM(A.votes) as reputation");
$this->db->from("(
SELECT reply.votes FROM reply where reply.user_id = $user_id
UNION ALL
SELECT thread.votes FROM thread where thread.user_id = $user_id) AS A");
$query = $this->db->get();
Another Solution using $this->db->get_compiled_select();
public function reputation($user_id) {
$this->db->select('thread.votes');
$this->db->from('thread');
$this->db->where('thread.user_id =' . $user_id);
$thread_query = $this->db->get_compiled_select();
$this->db->select('reply.votes');
$this->db->from('reply');
$this->db->where('reply.user_id ='. $user_id);
$reply_query = $this->db->get_compiled_select();
$query = $this->db->query("SELECT SUM(total.votes) as reputation from ($reply_query UNION ALL $thread_query) as total");
if ($query->num_rows() > 0) {
return $query->row();
} else {
return 0;
}
}
When you will use left join, it will return matched column & all other unmatched column from left table. Use inner join & check your result.

codeigniter LEFT JOIN array issue

Can someone tell me how to write this properly?
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$records = $this->db->where('us.group_id', '3');
$data=array();
foreach($records->result() as $row)
{
$data[$row->id] = $row->first_name;
}
return ($data);
}
I'm trying to populate a drop down menu using an array, but i need to only grab users that are part of users_group/group_id = 3
therefore in my very limited knowledge I'm needing:
select X from Users LEFT JOIN users_groups WHERE group_ID = 3
You need to call $this->db->get() in order to actually run your query.
function get_tech() {
$this->db->select('u.id
,u.first_name
,us.id
,us.group_id');
$this->db->from('users u');
$this->db->join('users_groups us','us.id = u.id','left');
$this->db->where('us.group_id', '3');
$records = $this->db->get();
$data = array();
foreach($records->result() as $row){
$data[$row->id] = $row->first_name;
}
return $data;
}

get the last row in the db

What is the correct way to retrieve the last row, sorted desc by id, in CodeIgniter?
I've tried select_max but I want the whole row.
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
Here I am getting the first value, but I want the last row.
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Try this.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
In controller function you need to do following things.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
Should work?

Resources