Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Where statement
#1

[eluser]Unknown[/eluser]
Hey guys,

I'm having a little trouble with the where-statement of codeigniter.

Code:
function getSenderName($sender_id)
    {    
        $i = 0;
        foreach($sender_id as $ID)  
        {
            $name[$i] = $this->getSenderNameSupport($ID['id_sender']);
            $i++;
        }
        
        return $name;
              
    }
    
    function getSenderNameSupport($id)
    {
        $this->db->where('id', $id);
        $query = $this->db->get('users');  
        $query = $query->result_array();

        return $query[0]['username'];
        
    }

The idea is to get the names of senders of messages inside a system by their id in the db. The given parameter $id looks like this:

Code:
Array ( [id_sender] => 1 [id_receiver] => 1 [title] => testmail [text] => this is a testmail dude )
Array ( [id_sender] => 2 [id_receiver] => 1 [title] => test2 [text] => testmail number2 )

When the senders are identical, the code returns the desired array of names. However, if they are different, only the first is returned, the other array element are left blank. I assume that the where-statement is still active when I call the function another time (not sure though) so the database is searched for something like "where id = 1 AND where id = 2", which would result in nothing being returned (as is the case in my code now).

I would really appreciate your help. Thanks in advance
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

I think you might be better off using $this->db->where_in(), then you can get the data you want in a single call to the database. There's something about database calls in for-loops that makes me feel sad inside, so I try to avoid it. Wink




Theme © iAndrew 2016 - Forum software by © MyBB