amM
amM

Reputation: 529

Fetching data by comparing single value with comma-delimited records in codeigniter

I am trying to fetch posts data of the particular author from database.
I have to fetch data as per post_author.
post_author field contain data like -

2,4,5,9
2,4,5
4
4
4
2,4,10

If the post having multiple authors then values stored in comma-delimited format.
I want to compare post_author(stored in session admin_id) with field post_author.
if suppose I have a post_author 4, that is available in all records, so select query should return all records.
My select query in model -

$this->db->select('p.ID,p.post_date,p.post_title,p.post_url,u.user_login');
$this->db->from('sa_posts p,sa_users u');
$this->db->where('u.ID=p.post_author');
$this->db->where('p.post_author',$this->session->userdata('admin_id')); // Here is a problem to compare post_author.
$query=$this->db->get();
return $query->result();  

Please help me.
Thanks.

Upvotes: 0

Views: 595

Answers (2)

amM
amM

Reputation: 529

After making some changes I got the output what I wanted using FIND_IN_SET()-

$where = "FIND_IN_SET('".$this->session->userdata('admin_id')."', p.post_author)";

$this->db->select('p.ID,p.post_date,p.post_title,p.post_url,u.user_login');
$this->db->from('sa_posts p,sa_users u');
$this->db->where('u.ID=p.post_author');
$this->db->where( $where );
$query=$this->db->get();
return $query->result();

Upvotes: 0

alaric
alaric

Reputation: 987

Try the following in CodeIgniter:

Substitute:

$this->db->where('p.post_author',$this->session->userdata('admin_id'));

for:

$this->db->where(FIND_IN_SET("{$this->session->userdata('admin_id')}", 'p.post_author') != 0);

Upvotes: 2

Related Questions