0

Any idea why this won't work? I m getting tags a user has set up and want to get other users' ids that have same tags.

Controller:

$this->load->model('tag_model');
            $data['usr_tags'] = $this->tag_model->get_home_tags($user_id);
            $tags = $this->tag_model->get_home_tags($user_id);
            $tag_array = $tags;
            $data['tag_users'] = $this->tag_model->get_tag_users($tag_array);

Model:

function get_tag_users($tag_array)
    {
            //$tag = array('item1','item2','item3');
            $tag = $tag_array;
            $query_str = 'SELECT DISTINCT user_id FROM tags WHERE tag IN ("'. implode('","', $tag) .'")';

            $query = $this->db->query($query_str);

            if($query->num_rows() > 0) {
                    foreach($query->result_array() as $tag_users) {
                            $data[] = $tag_users;
                    }
                    return $data;
            }else{
                    return false;
            }
    }

Error:

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: models/tag_model.php
Line Number: 20
4
  • array(4) { [0]=> array(1) { ["tag"]=> string(3) "lol" } [1]=> array(1) { ["tag"]=> string(4) "here" } [2]=> array(1) { ["tag"]=> string(3) "php" } [3]=> array(1) { ["tag"]=> string(5) "mysql" } } Commented Apr 9, 2012 at 16:52
  • $query_str = 'SELECT DISTINCT user_id FROM tags WHERE tag IN ("'. implode('","', $tag) .'")'; Commented Apr 9, 2012 at 17:02
  • Try $tag[] = $tag_array; Commented Apr 9, 2012 at 17:09
  • Before trying $tag[] = $tag_array; the error showed up 4 times now it only shows up once. Maybe it matters. Commented Apr 9, 2012 at 17:12

2 Answers 2

3

Controller

$this->load->model('tag_model');
$data['usr_tags'] = $this->tag_model->get_home_tags($user_id);
$data['tag_users'] = $this->tag_model->get_tag_users($data['usr_tags']);

Model

function get_tag_users($tag_array)
{
    $tags=array();
    foreach($tag_array as $tag)
    {
        $tags[]= $tag['tag'];
    }
    $query_str = 'SELECT DISTINCT user_id FROM tags WHERE tag IN ('.implode(",", $tags).')';
    $query = $this->db->query($query_str);
    if($query->num_rows() > 0)
    {
        return $query->results();
    }
    else
    {
        return false;
    }
}

Note : Your $data['tag_users'] will contain another array of user ids.

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

11 Comments

Error Number: 1054 Unknown column 'Array' in 'where clause' SELECT DISTINCT user_id FROM tags WHERE tag IN (Array,Array,Array,Array) Filename: /home/chatervi/public_html/dev2/models/tag_model.php Line Number: 21
Can you post what $this->tag_model->get_home_tags($user_id); returns ?
May be you can try $data['tag_users'] = $this->tag_model->get_tag_users($data['usr_tags'][0]); instead, check the controller portion again in the answer.
this is what a var_dump returns array(4) { [0]=> array(1) { ["tag"]=> string(3) "lol" } [1]=> array(1) { ["tag"]=> string(4) "here" } [2]=> array(1) { ["tag"]=> string(3) "php" } [3]=> array(1) { ["tag"]=> string(5) "mysql" } } It just returns the tags.
I think this did it: ($data['usr_tags'][0]) let me run a few tests to see if everything is ok. The errors are not showing up anymore.
|
2

Your $tag_array looks like this:

array(4){
    [0]=> array(1){
        ["tag"]=> string(3) "lol"
    }
    [1]=> array(1){
        ["tag"]=> string(4) "here"
    }
    [2]=> array(1){
        ["tag"]=> string(3) "php"
    }
    [3]=> array(1){
        ["tag"]=> string(5) "mysql"
    }
}

It's actually an array of arrays, you can't use implode on it. The "Array to string" conversion happens because each element of the array is an array, which PHP need to convert to a string in order to "implode" them.

You can use array_map to get each tag from the array.

function get_tag_users($tag_array)
{
    $tag = array_map(function($a){
        return $a['tag'];
    }, $tag_array);
    $query_str = 'SELECT DISTINCT user_id FROM tags WHERE tag IN ("'. implode('","', $tag) .'")';

If you don't have PHP 5.3, you can do it this way:

function get_tag_users($tag_array)
{
    $tag = array_map(create_function('$a', 'return $a["tag"];'), $tag_array);
    $query_str = 'SELECT DISTINCT user_id FROM tags WHERE tag IN ("'. implode('","', $tag) .'")';

2 Comments

Parse error: syntax error, unexpected T_FUNCTION, expecting ')' on this line $tag = array_map(function($a) {
@ciprian: Try the 2nd example, it uses create_function. You can pass functions as parameters only in PHP 5.3+.

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.