我从数据库获取Nameno,如下所示。
$form_data['Nameno']='1,2,3,4';
$getName=$this->Home_model->getNameData($form_data['Nameno']);
现在,我将该Nameno传递给模型以获取Name。所以我用了explode
并传递给foreach
模型
public function getNameData($NameId){
$getTempid=explode(",",$NameId);
$arrayData=[];
foreach ($getTempid as $row){
$where = array('is_tempActive' => 1,'Name_id'=>$row);
$this->db->select('templateName');
$this->db->from('tbl_templatename');
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
//print_r($result);
//$arrayData[]=$result;
}
return $result;
}
我需要来自模型的输出,例如$getName='ABC,XYZ,POD,RED'
在@Barmar建议答案后
控制器
$ids = explode(',', $form_data['Nameno']);
$names = array();
foreach ($ids as $id) {
$names[] = $this->Home_model->getNameData($id)->templateName;;
// print_r($names);
}
$getNames = implode(',', $names);
print_r($getNames);
模型
public function getNameData($tempId){
$where = array('is_tempActive' => 1,'Name_id'=>$row);
$this->db->select('templateName');
$this->db->from('tbl_templatename');
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;
}
在这个问题上您能帮我吗?
答案 0 :(得分:1)
您需要将原始数据分解为数组,在每个元素上调用该函数,然后将结果内插到字符串中。
$ids = explode(',', $form_data['Nameno']);
$names = array();
foreach ($ids as $id) {
$nameData = $this->Home_model->getNameData($id);
$names[] = $nameData[0]->templateName;
}
$getNames = implode(',', $names);
答案 1 :(得分:1)
您可以使用where_in()
通过一个查询来获取结果,而不是循环运行多个查询。
public function getNameData($NameId){
$getTempid = explode(",", $NameId);
$this->db->select('Name');
$this->db->from('tbl_templatename');
$this->db->where('is_tempActive', 1);
$this->db->where_in($getTempid);
$query = $this->db->get();
$result = $query->result();
foreach ($query->result() as $row) {
$names[] = $row->Name;
}
return implode(',', $names);
}