2017-04-14 13 views
0

在這裏,我做的笨,我在這裏有一個數組,這個數組裏面studentAbsentId我存儲在JSON字符串這樣[studentAbsentId] => [「1 」, 「2」]怎樣用笨在數組中的字符串值以JSON

我的控制器

public function admin_getabsentlist() 
{ 

    $data = array(
    "schoolId" => $_POST['schoolName'], 
    "classId" => $_POST['className'], 
    "sectionId" => $_POST['sectionName'], 
    "absentDate" => date("Y-m-d", strtotime($_POST['absentDate'])) 
    ); 
    $absentresponse= $this->Admin_attendance_model->admin_options_listdisplay($data); 
} 

我的模型

public function admin_options_listdisplay($params) 
{ 
    $this->db->select('*'); 
    $this->db->where('status !=', '1'); 
    $this->db->where('schoolId =', $params['schoolId']); 
    $this->db->where('classId =', $params['classId']); 
    $this->db->where('sectionId =', $params['sectionId']); 
    $this->db->where('studentAbsentDate =', $params['absentDate']); 
    return $this->db->get('student_absent_list')->result_array(); 
} 
在我的控制器

我打印結果意味着

的var_dump($ absentresponse);

array(1) { 
    [0]=> 
    array(9) { 
    ["absendId"]=> 
    string(1) "1" 
    ["studentAbsentId"]=> 
    string(9) "["1","2"]" 
    ["studentAbsentDate"]=> 
    string(10) "2017-04-11" 
    ["schoolId"]=> 
    string(1) "2" 
    ["classId"]=> 
    string(1) "1" 
    ["sectionId"]=> 
    string(1) "1" 
    ["reg_date"]=> 
    string(19) "2017-04-13 01:01:03" 
    ["created_by"]=> 
    string(29) "[email protected]" 
    ["status"]=> 
    string(1) "0" 
    } 
} 

我的預期結果

{ 
    "status": 1, 
    "data": 
    [ 
    { 
     "studentabsentId":"1" 
     "studentabsentName":"Kani" 
     "studentAbsentDate": "2017-04-12" 
    }, 
    { 
     "studentabsentId":"2" 
     "studentabsentName":"yuvi" 
     "studentAbsentDate": "2017-04-12" 
    } 
    ] 
} 

爲了得到我預期的答案我寫這樣的代碼,

$optionsList = array(); 
     foreach($absentresponse as $hmres) 
     { 
      $hmparent['studentabsentId']=json_decode($hmres['studentAbsentId']); 
      $hmparent['studentAbsentDate']=$hmres['studentAbsentDate']; 
      array_push($optionsList,$hmparent); 
     } 


     $return = array("status" => 1, "data" => $optionsList); 
     echo json_encode($return); 

我正在輸出中

{ 
    "status": 1, 
    "data": [ 
    { 
     "studentabsentId": [ 
     "1", 
     "2" 
     ], 
     "studentAbsentDate": "2017-04-11" 
    } 
    ] 
} 

這不是我期待的結果,請幫助我的人怎麼辦,用於尋找studentAbsentId爲此的名字我已經編寫的功能在幫助我

我的助手

if (! function_exists('getStudentnameById')){ 
    function getStudentnameById($id){ 
     $ci =& get_instance(); 
     $ci->load->database(); 

     $ci->db->select('firstName'); 
     $ci->db->where('student_id', $id); 
     $q = $ci->db->get('new_student')->result(); 
     return $q[0]->firstName; // particulart filed 
    } 
} 
+1

我投票關閉這一問題作爲題外話,因爲StackOverflow的是不是一個自由的人羣來源的代碼生成器。 –

回答

0

英語不是我的母語,對不起那個。

在你的控制器中,你應該得到具有輸入類的$ _POST數據。 來源Codeigniter documentation

在你的模型,你可以這樣做簡化代碼:

public function admin_options_listdisplay($params) 
{ 
    return $this->db->select('*') 
        ->where('status !=', '1') 
        ->where('schoolId =', $params['schoolId']) 
        ->where('classId =', $params['classId']) 
        ->where('sectionId =', $params['sectionId']) 
        ->where('studentAbsentDate =', $params['absentDate']) 
        ->get('student_absent_list') 
        ->result_array(); 
} 

爲什麼你返回result_array?如果你的模型正確完成,你應該只返回對象類型。

在JSON,只是對象由擁抱包圍,他們藉此形成

{ 
    'string' : value, 
    'string' : value 
} 

欲瞭解更多信息,請訪問json.org

什麼是檢索此JSON的目的是什麼?

我認爲,你應該返回缺席statut的學生對象。

0

只需將你的$ absentresponse添加到你的數組中。

echo json_encode(array("status"=>true, "data"=>$absentresponse)); 

你不需要做anotherthings ...

相關問題