0

So I trying to build a notification system (CodeIgniter) and not store it in my database by this unique ID. Now I got some problem to using `SELECT query.

IMG

Also this is array stored in "value" row:

[{"id":0,"user_id":"1","comment":"That's a Nice Video.","posttime":1403523177,"status":1},{"id":1,"user_id":"4","comment":"Nice to see this..!!","posttime":1403590409,"status":1}]

And this is my (not work) query:

$query = $this->db->get_where('post_meta',array('status'=>1),in_array('user_id', $id));

Ideas?

Note: Notification will be sparated by "user_id".

1
  • 1
    what does it show when you run that? what error mesage? Commented Mar 19, 2015 at 22:41

1 Answer 1

1

You should not use in_array('user_id', $id) on that function because it returns boolean.

On the active record page: https://ellislab.com/codeigniter/user-guide/database/active_record.html you can take a look at parameter it takes for get_where() function

$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

Notice how the third parameter takes $limit which talks about the number of data you'll receive. (Leaving this blank will return you all data).


Some code examples:

If you just want to get the data with status = 1, use the following:

$query = $this->db->get_where('post_meta',array('status'=>1));

If you want to get the data with status = 1 and user_id = $id, use the following:

$this->db->like('value', '"user_id":"'.$id.'"'); 
$query = $this->db->get_where('post_meta',array('status'=>1));

The solution above is not the best, but it should work. The $this->db->like() function will create rules to get data in value row which has "user_id":"$id" where $id is the value that you define.


What if I want to get all notifications and group them based on their ID?

I usually get all the data and then use PHP to group them into its own array. I think it's cheaper than relying on database to do that (Correct me if i'm wrong).

So use the first code:

$query = $this->db->get_where('post_meta',array('status'=>1));

Then iterate them using foreach() or whatever you find convenient.

$data = array();
foreach($query as $k=>$v) {
    // Run a function to get User ID
    $user_id = your_function_here($v->value);

    if(!isset($data[$user_id]))
        $data[$user_id] = array();

    // This is just some example to group them together, you can optimize it to your own liking
    array_push($data[$user_id],$v);
}

your_function_here() is your function to get the user_id from value row on your database.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It's working now. But problems came again after I adding some features. The query may using JOIN TABLE to another. Will make this query first. *EDIT: Salam kenal :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.