codeigniter multi joins - codeigniter

Hello i have this join, but i wanna join another tabel named "users" and i want the "profil_billed" row, how can i do that
i mean "WHERE forum_traad.brugernavn = users.profil_billed" something like that
function posts($id)
{
$this->db->select('*,forum_traad.indhold as traad_indhold,
forum_kommentare.indhold as kommentare_indhold,
forum_traad.brugernavn as traad_brugernavn,
forum_traad.id as traad_id
');
$this->db->from('forum_traad');
$this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad', 'forum_traad.id');
$this->db->where('forum_traad.id', $id);
$query = $this->db->get();
if($query->num_rows > 0)
{
return $query->row();
} else {
return false;
}
}

You can write your query as below -
$this->db->select('*,forum_traad.indhold as traad_indhold,
forum_kommentare.indhold as kommentare_indhold,
forum_traad.brugernavn as traad_brugernavn,
forum_traad.id as traad_id, users.profil_billed as billed
');
$this->db->from('forum_traad');
$this->db->join('forum_kommentare', 'forum_kommentare.fk_forum_traad = forum_traad.id');
$this->db->join('users', 'forum_traad.brugernavn = users.profil_billed');
$this->db->where('forum_traad.id', $id);
$query = $this->db->get();
For detailed documentation of the syntax i have used refer below -
http://codeigniter.com/user_guide/database/active_record.html

Related

How to convert this SQL Query on Code Igniter Model

Good Day Masters,
Can anyone help me how to convert this SQL Query into Code Igniter format (model).
SELECT firstName, FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) as age FROM residents_tbl WHERE FLOOR(DATEDIFF(CURRENT_DATE, birthDate)/365.25) >= 18
I don't know how to write it on WHERE clause.
$query = $this->db->select('*');
$query = $this->db->from('residents_tbl');
**$query = $this->db->where('isHead', '1');**
$query = $this->db->order_by('lastName', 'ASC');
$query = $this->db->get('', 15, $this->uri->segment(3));
if ($query->num_rows() > 0) {
return $query->result();
}
TIA.
This is a simplified version with chaining. I just changed the type of 1 from string to number which might caused the problem.
$query = $this->db
->where('isHead', 1)
->get('residents_tbl')
->order_by('lastName', 'ASC');

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 active query error

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");

Codeigniter: Extract partial data from a query

I have a small question pls:
Let's say i have a long query:
$this->db->select('*')
->from('x')
->join(...)
->where('y >',$id)
->where('z <',$nr)
->where(...)
...
How can i put into a different variable all where statements so i can use them individually but only using one single query?
Thank you.
You need to create different model methods. Each method should have the where query outputting results. In your controller, you call each of these methods and store the results in variables.
Why not do this:
Controller:
function controller_method(){
$res1 = $this->model_name->get_where_results1($id, $nr);
$res2 = $this->model_name->get_where_results2($id, $nr);
$res3 = $this->model_name->get_where_results3($id, $nr);
}
Model:
function get_where_results1($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results2($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
function get_where_results3($id, $nr) {
$this->db->select('*')
->from('x')
->join("")
->where("");
$query = $this->db->get();
return $query->result();
}
UPDATE - if you wanted to use filters, i would this way:
Controller:
function controller_method() {
$res1 = $this->model_name->model_method($filters);
}
Model:
function model_method($filters) {
$this->db->select('*');
$this->db->from('x');
$this->db->join("");
if ($filters['filt1']) {
$this->db->where("something", $filters['filt1']);
}
if ($filters['filt2']) {
$this->db->where("something", $filters['filt2']);
}
$query = $this->db->get();
return $query->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;
}

Resources