I have for some days tried to figure out what the problem is, but I can't.

I have this function:

function gets($status = NULL, $bought = NULL)
{
    $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_clip, clips.price AS clip_price, clips.created_at AS clip_created_at, clips.updated_at AS clip_updated_at, clips.ip_address AS clip_ip_address');
    if ($status != FALSE)
    {
        $this->db->where('leads.status', $status);
    }
    $this->db->from('leads');
    if ($bought != FALSE)
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips', 'clips.lead_id = leads.id');
    }
    else
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips', 'clips.lead_id = leads.id', 'left outer');
    }
    $query = $this->db->get();

    if ($query->num_rows() != 0)
    {
        return $query->result();
    }
}

If I call the function like this $this->gets('approved'); it should return all rows from the leads table WHERE there wasn't found a match in the table clips where partner_id is equal to the current session and where type is minus.

If I called the function like this $this->gets('', 'bought'); it should return all rows from leads where a match was found with the same conditions as above.

I hope you can understand my code and help me.

If you have any questions, feel free to ask!

Thanks!

有帮助吗?

解决方案 2

I rewrote the code, and this is what I got to work.

function gets($status = NULL, $bought = NULL)
{
    // Get the rows where the partner has used clips
    $this->db->where('partner_id', $this->session->userdata('partner_id'));
    $this->db->where('type', '-');
    $query = $this->db->get('clips');

    if ($status != FALSE)
    {
        if(is_array($status))
        {
            $first = TRUE;
            foreach($status as $row)
            {
                if ($first == TRUE)
                {
                    $this->db->where('leads.status', $row);
                    $first = FALSE;
                }
                else
                {
                    $this->db->or_where('leads.status', $row);
                }
            }
        }
        else
        {
            $this->db->where('leads.status', $status);
        }
    }

    $bought_array[] = '';
    if ($query->num_rows() != 0)
    {
        foreach($query->result() as $row2)
        {
                    // Add the ID's of the bought leads to array
            $bought_array[] = $row2->lead_id;
        }
    }

    // If bought leads should not be shown in the results
    if ($bought == FALSE)
    {
        $this->db->where_not_in('id', $bought_array);
    }
    else // If they should...
    {
        $this->db->where_in('id', $bought_array);
    }

    $query2 = $this->db->get('leads');

    if ($query2->num_rows() != 0)
    {
        return $query2->result();
    }
}

其他提示

i think it should be something like this

function gets($status = false, $bought = false)
{
    $this->db->select('leads.*, clips.id AS clip_id, clips.lead_id AS clip_lead_id, clips.partner_id AS clip_partner_id, clips.type AS clip_type, clips.clip AS clip_clip, clips.price AS clip_price, clips.created_at AS clip_created_at, clips.updated_at AS clip_updated_at, clips.ip_address AS clip_ip_address');
    if ($status)
    {
        $this->db->where('leads.status', $status);
    }
    if ($bought)
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips clips', 'clips.lead_id = leads.id');
    }
    else
    {
        $this->db->where('clips.partner_id', $this->session->userdata('partner_id'));
        $this->db->where('clips.type', '-');
        $this->db->join('clips clips', 'clips.lead_id = leads.id', 'left outer');
    }
    $query = $this->db->get('leads leads');

    if ($query->num_rows() != 0)
    {
        return $query->result();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top