如何使用json将数据库中的所有数据显示到Codeigner框架中的视图页面中


How to display all the data from database using json into view page in Codeigniter framework

,模型页面为:

public function categories() {
     $this->load->model('categories_model');
    try {
        $categories = $this->categories_model->get_all();
        if (!empty($categories)) {
            $this->result = $categories;
        } else {
            throw new Exception('No data founds');
        }
    } catch (Exception $e) {
        $this->error = $e->getMessage();
    }
    // array data
    return $this->_prepare_return_data();
}

我是codeigniter和json的初学者。你能帮我如何在视图页面中显示类别吗

一个基本示例。

控制器:

function categories() {
    $this->load->model('categories_model');
    $data = $this->categories_model->getCategories();
    $json = json_encode($data);
    header('Content-Type: application/json');
    print $json;
}

型号:

function getCategories() {
    $q = $this->db->get('categories');
    return $q->result_array();
}