如何在codeigniter中显示数据库表的值?

ldxq2e6h  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(325)

我正在创建一个示例图像测试,其中我希望在codeigniter中显示数据库表中的所有图像。问题是,如何使用codeigniter显示数据库表的值?
以下是我的模型代码:

<?php 

    class Image_model extends CI_Model{

        /*Sample test function for the image dropdown list*/
        public function getImages()
        {
            $query = $this->db->select('main_image_url'));
            $this->db->get('product_master');

            return $query->result();
        }
        /*End sample test function*/
    }
 ?>

这是我的控制器的代码:

<?php 
    if(! defined('BASEPATH')) exit('No direct script access allowed');

    class Sample_image_dropdown extends MX_Controller{

        public function __construct()
        {
            parent::__construct();
        }

        public function index()
        {
            $data['main_view'] = 'sample_view/image_dropdown_view';
            $this->load->view('sample_view/image_dropdown_view', $data);
        }

        public function display_all_images()
        {
            $this->load->model('sample_model/image_model');

            $data['images'] = $this->image_model->getImages();

            $data['main_view'] = "sample_view/image_dropdown_view";

            $this->load->view('sample_view/image_dropdown_view', $data);
        }

    }
 ?>

以下是我的观点:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <div>
        <select name="" id="">
            <option value="all">All Image</option>
            <option value="with-image">With Image</option>
            <option value="no-image">Without Image</option>
        </select>
        <input type="submit" value="Search">
    </div>
    <?php if (isset($images)):?>

    <?php foreach ($images as $image):?>
    <table>
        <tr>
            <td>
                <th>
                Images
                </th>
            </td>
        </tr>
        <tr>
            <td>
                <img src="<?php echo "$image->main_image_url";?>">
            </td>
        </tr>
    </table>
    <?php endforeach; ?>
    <?php endif; ?>
</body>
</html>
bqjvbblv

bqjvbblv1#

我假设您在上载时正在目录中存储图像 uploads/.png 然后您可以从数据库中获取数据(您已经在这样做了)
在我们看来: <img src="echo dir_path./$image->main_image_url;" /> 或者,如果将整个路径存储在db中,则: <img src="echo $image->main_image_url;" />

h5qlskok

h5qlskok2#

你在模型和控制器上犯了一个错误,我提到了更改,请记下并尝试,
模型文件

class Image_model extends CI_Model{

    /*Sample test function for the image dropdown list*/
    public function getImages()
    {
        $query = $this->db->select('main_image_url');
        $this->db->from('product_master');
        $query = $this->db->get();

        return $query->result();
    }
    /*End sample test function*/
}

控制器文件,

public function __construct()
{
    parent::__construct();
}

public function index()
{
    $data['main_view'] = 'sample_view/image_dropdown_view';
    $this->display_all_images();
}

public function display_all_images()
{
    $this->load->model('sample_model/image_model');    
    $data['images'] = $this->image_model->getImages();    
    $data['main_view'] = "sample_view/image_dropdown_view";    
    $this->load->view('sample_view/image_dropdown_view', $data);
}

相关问题