PHP retrieves individual data from a database based on two rows

I have a table with a name messageswith several rows:

+---------+
| columns |
+---------+
| id      |
| from    |
| to      |
| body    |
| date    |
+---------+

What I want is to get a list of users of me messages or users who have sent me messages.

public function get_users_m($where)
{
    $this->db->select('*');
    $this->db->from('messages');
    $this->db->where($where); // to = my_id or from = my_id 
    $this->db->group_by('from, to');

    return $this->db->get()->result_array();
}

I did this with Codeigniter, but the problem is that I reply, for example, to C, I get the message A (me) to Cand Cto the message A, and I don't want me to want it just Conce, because it doesn't matter if I am the one who sent him a message, or he the one who will send me a message, this is still the same user.

+4
source share
2 answers

How about this

public function get_users_m()
{
    $my_id = 5; # some id
    $this->db->select('*');
    $this->db->from('messages');
    $this->db->where('from == $my_id');
    $this->db->or_where('to == $my_id'); # add this or_where Clause
    $this->db->group_by('user_id');
    $query = $this->db->get();
    $result = $query->result_array();
    return $result;
}

Tpojka

+3

user_id profile_id . , . user_id profile_id.

, - profile_id.

, , .

public function get_users_m($where)
    {
        $this->db->select('profile_id');
        $this->db->from('messages');
        $this->db->where('profile_id != your profile id'); 
        $this->db->group_by('profile_id');
        return $this->db->get()->result_array();
    }
+1

Source: https://habr.com/ru/post/1620133/


All Articles