我有两个mysql表sla和department,我正在创建sla,在创建sla时,有多个department的选择,
当用户提交SLA表单时,department表中受尊敬的部门得到SLA id,
但是在添加之后,我需要将添加的SLA与更新的部门连接一起拉出来,
下面是我的控制器代码,

if ($this->form_validation->run() !== FALSE) {
    $result = $this->model_admin->updatesla($sla_data); // Add SLA
    if (!$result) {
        $lastentry = $this->model_admin->LastEntrysla(); //Get last added SLA
        foreach($this->input->post('sla_department') as $department_id) {
            $sladepartment_data = array( 'sla_id' => $lastentry->sla_id);
            $this->model_admin->updatedepartmentsla($department_id,$sladepartment_data);//Update SLA to respective multiselected department
        }
        //Now getadded SLA with join of department
        foreach($this->model_admin->getaddedsla($lastentry->sla_id) as $returnsla) {
            $returnslajson = array(  'sla_id' => $returnsla->sla_id,
                'sla_name' => $returnsla->sla_name,
                'sla_days' => $returnsla->sla_days.'D '.$returnsla->sla_hours.'H '.$returnsla->sla_minutes.'M',
                'department_name' => $returnsla->department_name); //Here this SLA is assign to department X and Y, i want department_name = X, y
        }
        echo json_encode($returnslajson);
    }
}

我在每一行都添加了一些评论,
当SLA增加时,我在输出端只得到一个部门,
department_name: "X"
sla_days: "1D 1H 1M"
sla_id: "38"
sla_name: "qweqweq"

我怎么能把两个部门都叫成X,Y
下面是数组的输出,$this->model_admin->getaddedsla($lasentry->sla_id)
Array ( [0] => stdClass Object ( [sla_id] => 38 [sla_name] => qweqweq [sla_days] => 1 [sla_hours] => 1 [sla_minutes] => 1 [department_id] => 20 [department_name] => X ) [1] => stdClass Object ( [sla_id] => 38 [sla_name] => qweqweq [sla_days] => 1 [sla_hours] => 1 [sla_minutes] => 1  [department_id] => 21 [department_name] => Y ) )

提前谢谢

最佳答案

试一试-

    //Now getadded SLA with join of department
    $department_name = $name = $days = $id = array();
    foreach($this->model_admin->getaddedsla($lastentry->sla_id) as $returnsla) {
        $name[] = $returnsla->sla_name;
        $id[] = $returnsla->sla_id;
        $days[] = $returnsla->sla_days.'D '.$returnsla->sla_hours.'H '.$returnsla->sla_minutes.'M';
        $department_name[] = $returnsla->department_name;
    }
    $returnslajson['department_name'] = implode(',', $department_name);
    $returnslajson['sla_id'] = implode(',', $id);
    $returnslajson['sla_name'] = implode(',', $name);
    $returnslajson['sla_days'] = implode(',', $days);
    echo json_encode($returnslajson);

10-08 09:04