我知道这经常被问到并且现在已经回答了几次,但是我可以使用已经存在的问题来解决我的问题。我也刚开始学习codeigniter。这是代码:
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$movies = $this->movielist_model->get_movies();
$data = result();
foreach($movies->result() as $r) {
$data[] = result(
$r->title,
$r->price,
$r->rating
);
}
// IT SAYS THAT THE ERROR IS HERE:
$output = result(
"draw" => $draw,
"recordsTotal" => $movies->num_rows(),
"recordsFiltered" => $movies->num_rows(),
"data" => $data
);
echo json_encode($output);
exit();
}
答案 0 :(得分:0)
您应该array()
public function somefunction() {
$output = array();
$data = array();
$draw = intval($this->input->get("draw"));
$start = intval($this->input->get("start"));
$length = intval($this->input->get("length"));
$movies = $this->movielist_model->get_movies();
foreach($movies->result() as $r) {
$data[] = array(
'title' => $r->title,
'price' => $r->price,
'rating' => $r->rating
);
}
$output[] = array(
"draw" => $draw,
"recordsTotal" => $movies->num_rows(),
"recordsFiltered" => $movies->num_rows(),
"data" => $data
);
}
echo json_encode($output);
}
答案 1 :(得分:0)
创建一个数组$result
并将其放入输出数组中,如果你有更多的输出,你仍然可以在该数组中放入很多东西。
$result = [
"draw" => $draw,
"recordsTotal" => $movies->num_rows(),
"recordsFiltered" => $movies->num_rows(),
"data" => $data
];
$output [] = $result;
echo json_encode($output);
exit();