简体   繁体   中英

How to display image from database codeigniter

I am trying to display image from database using codeigniter. When a user search for a location, the information will display out including images. But I fail to display out the images. I have saved my image at file outside application. Here is my code.

//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>

//controller

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

//model

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

}
?>

by default Code-igniter will return data in the form of std-object. you need to access that with -> (arrow).

eg jd reply in comment

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

or you need to pass parameter 'array' in function in your model

use this in your model : return $query->result('array'); return (Array)

Instance : return $query->result(); return (Std-Object)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM