skip to Main Content

Here is my mysql table (chatizin)

Id Sender Receiver
1 11 6
2 11 96
3 6 96
4 6 11

And Here is my php codes by using codeigniter 3


$UserId=11;
$Receiver=6;

$this->db->where(["Sender" => $UserId,"Receiver"=>$Receiver]);
$this->db->or_where(["Sender" => $Receiver,"Receiver"=>$UserId]);
$this->db->order_by("Id DESC");
$sohbetciler = $this->db->get("chatizin")->result();

What i am trying to do is getting below result

Array
(
    [0] => stdClass Object
        (
            [Id] => 4
            [Sender] => 6
            [Receiver] => 11
            
        )


    [2] => stdClass Object
        (
            [Id] => 1
            [Sender] => 11
            [Receiver] => 6
        )

)

But or_where condition not working as i want. instead it gave me below result. Why it takes Id=3 there is no condition to bring Receiver=96

Array
(
    [0] => stdClass Object
        (
            [Id] => 4
            [Sender] => 6
            [Receiver] => 11

        )

    [1] => stdClass Object
        (
            [Id] => 3
            [Sender] => 6
            [Receiver] => 96

        )

    [2] => stdClass Object
        (
            [Id] => 1 
            [Sender] => 11
            [Receiver] => 6
           
        )

)
$this->db->or_where(["Sender" => $Receiver,"Receiver"=>$UserId]);

in or_where condition "Receiver"=>$UserId this part not working please help me

2

Answers


  1. Chosen as BEST ANSWER

    i found a solution but not sure how accurate

    
    $this->db->where(["Sender" => $user->Id,"Receiver"=>$Receiver]);
    $this->db->or_group_start()->where(["Sender" => $Receiver,"Receiver"=>$user->Id])->group_end();
    $this->db->order_by("Id DESC");
    $sohbetciler = $this->db->get("chatizin")->result();
    
    

  2. You might have to use query grouping. Here is a tweaked version of the example from the documentation:

    $this
        ->db
        ->group_start()
            ->where("Sender", $UserId)
            ->where("Receiver", $Receiver)
        ->group_end()
        ->or_group_start()
            ->where("Sender", $Receiver)
            ->where("Receiver", $UserId)
        ->group_end()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search