簡體   English   中英

如何從數據庫Codeigniter顯示圖像

[英]How to display image from database codeigniter

我正在嘗試使用codeigniter從數據庫顯示圖像。 當用戶搜索位置時,信息將顯示出來,包括圖像。 但是我無法顯示圖像。 我已將圖像保存在應用程序外部的文件中。 這是我的代碼。

//view.php

<style>
#searchbutton{
position: absolute;
left:300px;
top:30px;
    }
fieldset {
background-color:#EFEAEA;
margin: 0px 0px 10px 0px;
 padding: 20px;
border-radius: 1px;
width:900px;
margin-left:220px;
margin-top:-10px;
}
#user{
font-style:italic;
font-size: 12px;
text-align:right;
}
#titlereview {
font-style: italic;
font-size:20px;
}
#review {
font-size:16px;
}
</style>

<?=form_open_multipart('viewreview/view');?>
<?php $search = array('name'=>'search',);?>
<div id = "searchbutton">
<?=form_input($search);?><input type=submit value="Search" /></p>
</div>
<?=form_close();?>

<div class = "tablestyle">
<fieldset>
<?php foreach ($query as $row): ?>
<div id = "user">
User: <?php echo $row->name; ?><br>
Visited time: <?php echo $row->visitedtime; ?><br>
</div>
<div id = "titlereview">"<?php echo $row->titlereview; ?>"<br></div>
<div id = "review"><?php echo $row->yourreview; ?><br></div>
<div id = "image"><?php echo '<img src="data:image/jpeg, $row[images]"/>' ?<br><hr><br></div>

<?php endforeach; ?>
</fieldset>
</div>

//控制器

<?php

class viewreview extends CI_Controller {

public function view($page = 'viewreview') //writereview page folder name
{
    $this->load->model('viewreview_model');
    $data['query'] = $this->viewreview_model->get_data();
    $this->load->vars($data);

    if ( ! file_exists('application/views/viewreview/'.$page.'.php')) //link
    {
        // Whoops, we don't have a page for that!
        show_404();
    }

    $data['title'] = 'View Review'; 
    //$data['title'] = ucfirst($page); // Capitalize the first letter
    $this->load->helper('html');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->view('templates/header', $data);
    $this->load->view('viewreview/'.$page, $data);
    $this->load->view('templates/footer', $data);
 }
}
?>

//模型

<?php
class viewreview_model extends CI_Model {

public function __construct()
{
    $this->load->database();
}
public function get_data()
{
    $match = $this->input->post('search');
    $this->db->like('sitename',$match);
    $this->db->or_like('titlereview',$match);
    $this->db->or_like('yourreview',$match);
    $this->db->or_like('suggestion',$match);

    $query = $this->db->get('review');      //pass data to query
    return $query->result();
 }

}
?>

默認情況下,Code-igniter將以std-object的形式返回數據。 您需要使用->(箭頭)進行訪問。

例如jd在評論中回復

<?php echo '<img src="data:image/jpeg,'.$row->images.'" />' ?>

或者您需要在模型的函數中傳遞參數“ array”

在模型中使用它: return $query->result('array'); 返回(數組)

實例: return $query->result(); 返回(標准對象)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM