Ciprian
Ciprian

Reputation: 3226

Pass an array from controller to model - codeigniter

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

Upvotes: 0

Views: 5349

Answers (2)

The Alpha
The Alpha

Reputation: 146191

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.

Upvotes: 3

gen_Eric
gen_Eric

Reputation: 227240

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) .'")';

Upvotes: 2

Related Questions