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

时间:2018-12-24 07:47:23

标签: php mysql codeigniter

我正在创建一个样本图像测试,我想在其中显示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>

2 个答案:

答案 0 :(得分:1)

您在模型和控制器中犯了一个错误,我提到了所做的更改,请记下并尝试,

模型文件

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);
}

答案 1 :(得分:0)

我假设您在上载时将图像存储在目录中 uploads/abc.png

然后您可以从db中获取数据(您已经在执行此操作)

并在视图中:

<img src="echo dir_path./$image->main_image_url;" />

或者如果您要将整个路径存储在 DB 中,则:

<img src="echo $image->main_image_url;" />