通过查询图像文件名从数据库中使用AJAX在Codeigniter中显示图像列表

时间:2022-10-06 19:21:05

I have a list of filenames of some images stored in a database. I'm trying to get that list using ajax but I am getting the warning message:

我有一个存储在数据库中的一些图像的文件名列表。我正在尝试使用ajax获取该列表但我收到警告消息:

[json] (php_json_encode) type is unsupported, encoded as null

[json](php_json_encode)类型不受支持,编码为null

Below is my code:

以下是我的代码:

Controller:

<?php
class Create extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('get_files');
    }

    public function index()
    {
        $data['title'] = 'Create New Map';
        $this->load->view('head&foot/create_header', $data);
        $this->load->view('create', $data);
        $this->load->view('head&foot/create_footer');
    }

    // Loads the default tiles into the gallery
    public function update_gallery()
    {
        echo json_encode($this->get_files->get_image_list()); 
    }

}
?>

Model:

<?php

/*
 * Returns a list of files from a database
 */

class Get_files extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function get_image_list(){
        return $this->db->query("SELECT * FROM default_tile");
    }
}

?>

In my view the ajax request is:

在我看来,ajax请求是:

$.ajax({
          url:'create/update_gallery',
          type:'GET',
          success:function(data){
            $('#default_tiles_view').html(data);
          }
       })

Can any see what is causing the warning?

可以看到导致警告的原因吗?

1 个解决方案

#1


2  

The problem is your get_image_list() method. It does not actually return an image list, but a database result object: the result of $this->db->query("SELECT * FROM default_tile").

问题是你的get_image_list()方法。它实际上并不返回图像列表,而是返回数据库结果对象:$ this-> db-> query(“SELECT * FROM default_tile”)的结果。

In that function you will need to loop through the result set to get all your images in a list (array) and return that list from the function.

在该函数中,您需要遍历结果集以将所有图像放入列表(数组)中,并从函数中返回该列表。

Example:

function get_image_list(){
    $images = array();
    $query =  $this->db->query("SELECT * FROM default_tile");

    // simple example
    foreach ($query->result_array() as $row) 
    {
      $images[] = $row;
    }

    return $images;
}

#1


2  

The problem is your get_image_list() method. It does not actually return an image list, but a database result object: the result of $this->db->query("SELECT * FROM default_tile").

问题是你的get_image_list()方法。它实际上并不返回图像列表,而是返回数据库结果对象:$ this-> db-> query(“SELECT * FROM default_tile”)的结果。

In that function you will need to loop through the result set to get all your images in a list (array) and return that list from the function.

在该函数中,您需要遍历结果集以将所有图像放入列表(数组)中,并从函数中返回该列表。

Example:

function get_image_list(){
    $images = array();
    $query =  $this->db->query("SELECT * FROM default_tile");

    // simple example
    foreach ($query->result_array() as $row) 
    {
      $images[] = $row;
    }

    return $images;
}