How to get users and profile table data in codeigniter - php

I have two tables: one is users and other is user_profile and in users table suppose 20 row and user_profile table only two rows.
How could I get all user which has profile or whom has no profile in codeingniter?

You can use join as below
Note : user_id is primary key of users table
$this->db->select('*');
$this->db->join('user_profile up', 'up.user_id = u.user_id', 'left');
$this->db->get('users u')->result_array();

To get the users with profile, use INNER JOIN
public function users_profile(){
$this->db->select('users.*, user_profile.column1 as column1, user_profile.column2 as column2');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'inner');
$query = $this->db->get('users');
return $query->result_array();
}
To get all users with or without profile, use LEFT JOIN
public function all_users(){
$this->db->select('users.*, user_profile.column1 as column1, user_profile.column2 as column2');
$this->db->join('user_profile', 'user_profile.user_id = users.user_id', 'left');
$query = $this->db->get('users');
return $query->result_array();
}
Rememer to make user_id the PRIMARY KEY for both tables

Related

Show data record using id in codeigniter

i got this error message
Column 'id_siswa' in where clause is ambiguous
SELECT * FROM `siswa` `a`
LEFT JOIN `pembayaran_spp` `b` ON `b`.`id_siswa`=`a`.`id_siswa`
WHERE `id_siswa` = '7%E2%80%8B'
I have 2 table.
1.table 'siswa'
structure(id_siswa,nama_siswa,id_tahun_masuk)
2.table 'pembayaran_spp'->
structure(id_pembayaran,id_siswa,jml_pembayaran,id_tahun,date)
I want to show data 'pembayaran_spp' by id_siswa.
so when i click detail on 'siswa', data 'pembayaran' is showed by id_siswa.
My Controller
function detailtagihan($id_siswa)
{
$data['siswa'] = $this->M_keuangan->tagihansiswa($id_siswa);
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('keuangan/v_detailtagihan',$data);
$this->load->view('template/footer');
}
My Model
function tagihansiswa($id_siswa)
{
//$data = array('id_siswa' => $id_siswa );
$this->db->select('*');
$this->db->from('siswa a');
$this->db->join('pembayaran_spp b','b.id_siswa=a.id_siswa', 'left');
$this->db->where('id_siswa',$id_siswa);
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
}
You should mention of which table you want to use the column id_siswa in your where clause. As both of the tables are having a column with same name, you are getting this error.
If you want to use siswa then in your where condition write a.id_siswa
And if you want to use pembayaran_spp then in your where condition b.id_siswa.

codeigniter, join table properties overrides other properties

I'm working on a project using codeigniter 3 and I have a following query
$this->db->select("*");
$this->db->from("questions");
$this->db->join('users', 'users.id = questions.user_id');
$this->db->where('article_id', $articleId);
$this->db->order_by('questions.id', 'DESC');
$query = $this->db->get();
return $query->result();
// query
// SELECT * FROM `questions` JOIN `users` ON `users`.`id` = `questions`.`user_id` WHERE `article_id` = 18 ORDER BY `questions`.`id` DESC
Currently, the id property from users table overrides the questions id one. The question's id is crucial.
I worry, I will have to write a custom query, which I am not really good at.
Try to do this as follow:
$this->db->select("*, questions.id as question_id");
It's obvious that properties with the same name will be overriden. That's why you should assign to them 'unique' names in select statement.
You can specifically mention the ids with alies like -
$this->db->select("questions.id as question_id, users.id as user_id, questions.*, users.*");
$this->db->from("questions");
$this->db->join('users', 'users.id = questions.user_id');
$this->db->where('article_id', $articleId);
$this->db->order_by('questions.id', 'DESC');
$query = $this->db->get();
return $query->result();
This will give you specific columns named with question_id, user_id, questions table's all columns, users table's all columns
Or another better solution to use specific column names in select with alies if required.

selecting a colum from joined tabels in database Codeigniter

I am trying to select column name level_name from table levels,
which contains level_id and level_name,
for a user to know what is their level,
The table of users named as users and contain a level_id and user_id,
but I get this error ->
Column 'level_id' in on clause is ambiguous
SELECT `level_name`
FROM `levels`
JOIN `users` ON `level_id` = `level_id` WHERE `user_id` = '9'
here it is the code in the model
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'level_id = level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
thanks in advance :)
Change the query to
SELECT `level_name`
FROM `levels` l
JOIN `users` u ON `u`.`level_id` = `l`.`level_id`
WHERE `user_id` = '9'
if you like the table name aliasing method, it is shorter and easier to read.
Or
SELECT `level_name`
FROM `levels`
JOIN `users` ON `users`.`level_id` = `levels`.`level_id`
WHERE `user_id` = '9'
If you prefere to use the full table name everywhere.
Because both tables contain a column with the name level_id the query analyser need to know which one you are addressing.
In codeigniter try
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels l');
$this->db->join('users u', 'u.level_id = l.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
Select l.level_name
FROM levels l
JOIN users u
ON u.level_id = l.level_id
and u.user_id = '9'
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'levels.level_id = users.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
public function level_ownprofile($user_id)
{
$this->db->select('l.level_name');
$this->db->from('levels as l');
$this->db->join('users as u', 'l.level_id = u.level_id');
$this->db->where('l.user_id', $user_id);
$query = $this->db->get();
return $query->results();
}

Many to many database association in codeigniter

I am creating a sort of card collection programme with codeigniter and ion_auth for authentication. A user should be able to add a card to his collection.
So i have a users table and a cards table and a junction/bridge table.
users
ID
NAME
and
cards
ID
NAME
and
users_cards
ID
USER_ID
CARD_ID
I want to accomplish a SELECT statement that retrieves the cards from a users through user_cards.
$this->db->select('*');
$this->db->from('cards');
$this->db->join('users_cards', 'users.id = users_cards.user_id', 'inner');
$this->db->join('users', 'users_cards.user_id = users.id', 'inner');
$query = $this->db->get();
return $query->result_array();
I cant seem to get a grasp at the concept of these joins. Can someone please help me?
Looks like the join statement is missing the reference to the 'cards' table.
$this->db->select('*');
$this->db->from('users_cards');
$this->db->join('users', 'users.id = users_cards.user_id', 'inner');
$this->db->join('cards', 'cards.id = users_cards.card_id', 'inner');
$query = $this->db->get();
return $query->result_array();
The idea is that you're trying to get a relationship going between users_cards and the other two tables by referencing their relevant ids
Since you didn't joined users table you can't join based on the users.id reference. So you need to change
$this->db->join('users_cards', 'users.id = users_cards.user_id', 'inner');
to
$this->db->join('users_cards', 'cards.id = users_cards.card_id', 'inner');

Retrieving multiple values associated with a single record. MySQL PHP Codeigniter

I need help with a query on following tables.
table user(
user_id char(5),
username char(12),
columnx char(10));*
table phone(
user_id char(5),
phone_number number(10),
primary(Y, N) char(1));*
Both tables linked on user_id,
Here each user can have multiple phone_numbers.
I need to pullout a set of users and along with their phone numbers. I am trying to do the following.
in model
$this->db->where('columnx', 'something');
$query = $this->db->get('users');
foreach($query->result() as $row) {
$this->db->select('phone_number');
$this->db->where('user_id', $row->user_id);
$this->db->where('primary', 'Y');
$q = $this->db->get('phone');
}
How do I return from model and display multiple phone number for each user, when I my first $query returns multiple users??
Thanks in advance,
Prim
Try using join:
$query = $this->db->select('*')
->from('user as a')
->join('phone as b', 'a.user_id = a.user_id')
->where(array('a.columnx' => 'something'));
->get();
var_dump($query->result());

Categories