0

我需要从具有相同名称的数据库中获取数据。我使用 json 数组来获取输出。

function search_review($input, $serviceName){
        $data = array();
        $ipJson = json_encode($input);
        $this->db->select('*');
        $this->db->from('reviews');
        $this->db->where('reviewee_name', $input['reviewee_name']);
        $query = $this->db->get();
        $result = $query->result();
        if (!empty($result)) {
        foreach ($query->result() as $row)
        {
             $data[]['reviewer_id'] = $row->reviewer_id;
             $data[]['reviewee_name'] = $row->reviewee_name;
             $data[]['tournament_played_c_s'] = $row->tournament_played_c_s;
        }

            $data['message'] = 'Review details retrieved successfully.';
            $status = $this->clamo_lib->return_status('success', $serviceName, $data, $ipJson);
        } else {
            $data['message'] = 'Unable to retrieve review details.';
            $status = $this->clamo_lib->return_status('error', $serviceName, $data, $ipJson);
        }
        return $status;

    }

0输出:

我需要得到我的输出

<data>
      <_0/>
      <message>Review details retrieved successfully.</message>
      <reviewee_name>deepi</reviewee_name>
      <reviewer_id>4</reviewer_id>
      <tournament_played_c_s>karnataka,bangalore</tournament_played_c_s>
   </data>
<data>
      <_1/>
      <message>Review details retrieved successfully.</message>
      <reviewee_name>deepi</reviewee_name>
      <reviewer_id>5</reviewer_id>
      <tournament_played_c_s>kerala</tournament_played_c_s>
   </data>

我必须改变什么?

4

1 回答 1

0

这行得通吗?

function search_review($input, $serviceName){
    $data = array();
    $ipJson = json_encode($input);
    $this->db->select('*');
    $this->db->from('reviews');
    $this->db->where('reviewee_name', $input['reviewee_name']);
    $query = $this->db->get();
    // Check if query succeeded
    if ($query !== false) {
        $row_counter = 0;
        // Loop through all results returned
        while ($row = $query->result())
        {
            $data[$row_counter]['reviewer_id'] = $row->reviewer_id;
            $data[$row_counter]['reviewee_name'] = $row->reviewee_name;
            $data[$row_counter]['tournament_played_c_s'] = $row->tournament_played_c_s;
            $data[$row_counter]['message'] = 'Review details retrieved successfully.';
            $row_counter++;
        }
        $status = $this->clamo_lib->return_status('success', $serviceName, $data, $ipJson);
    } else {
        $data[]['message'] = 'Unable to retrieve review details.';
        $status = $this->clamo_lib->return_status('error', $serviceName, $data, $ipJson);
    }
    return $status;
}
于 2013-07-09T10:22:07.380 回答