gpt4 book ai didi

php - 将数组从 Controller 传递到模型 - codeigniter

转载 作者:太空宇宙 更新时间:2023-11-03 10:22:21 25 4
gpt4 key购买 nike

知道为什么这行不通吗?我正在获取用户设置的标签,并希望获取具有相同标签的其他用户的 ID。

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);

型号:

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;
}
}

错误:

A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: models/tag_model.php
Line Number: 20

最佳答案

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']);

型号

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;
}
}

注意:您的 $data['tag_users'] 将包含另一个用户 ID 数组。

关于php - 将数组从 Controller 传递到模型 - codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10076603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com