简体   繁体   中英

How to get the string values in array using codeigniter in Json

Here I'm doing codeigniter, here I have one array,inside this array studentAbsentId I stored in json string like this [studentAbsentId] => ["1","2"] ,

My Controller

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

My Model

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

in my controller i print the results means

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) "kanniyappan@g2evolution.co.in"
    ["status"]=>
    string(1) "0"
  }
}

My expected Results

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

For getting my expected answer i written the code like this,

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

I am getting ouput

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

this is not my expected results,please help me anyone how can do,for finding the studentAbsentId names for that purpose i already written the function in my helper

My helper

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

English is not my native language, sorry for that.

In your controller, you should get $_POST data with input class. Source Codeigniter documentation

In your model you can simplify your code by doing this:

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

Why do you return a result_array ? If your model is properly done you should only return object type.

In json, only the objects are surrounded by embrace and they take this forms

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

For more information, go to json.org

What is the purpose of retrieving this json?

I think, you should return student object with absent statut.

Just add your $absentresponse to your array.

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

You dont need to make anotherthings...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM