简体   繁体   中英

How to display image Codeigniter

I have uploaded an image through a form and successfully stored it in a uploads directory and I have also successfully stored the full path of the image in my database.

But I am facing a problem with showing the image in view :

view :

                 <div class="deal-top-top">
        <?php foreach($getAll as $rec) { ?>
            <div class="col-md-3 top-deal-top">
                <div class=" top-deal">
                    <a href="single.html" class="mask"><img src="<?php echo base_url('upload/screenshoot/').$rec->ss;?>"class="img-responsive zoom-img" alt=""></a>
                 <span class="four"><?php echo $rec->data_kategori;?></span>
                    <div class="deal-bottom">
                        <div class="top-deal1">
                            <h5><a href="single.html"><?php echo $rec->nama_aplikasi;?></a></h5>
                            <p>Instansi: <?php echo $rec->nama_instansi;?></p>
                        </div>
                        <div class="top-deal2">
                            <a href="single.html" class="hvr-sweep-to-right more">More</a>
                        </div>
                    <div class="clearfix"> </div>
                    </div>
                </div>
            </div>
             <?php } ?>  
        <div class="clearfix"> </div>
    </div>       

upload/screenshoot is uploads directory path, ss is the data name in database..

Controller

    $this->load->view('produk_view',['getKategori' =>$getKategori,'getAll' =>$getAll]);

if test with array :

ss is the part of getAll query:

  Array
  (
    [0] => stdClass Object
    ....
        [ss] => 1487492623549.PNG
    ....
    )

Error :

A PHP Error was encountered

Severity: Error

Message: Cannot use object of type stdClass as array

Thank you

Just try with $rec->ss; because you have array having objects .

   echo $rec->ss;//prints 1487492623549.PNG

UPDATE

in order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php:

$autoload['helper'] = array('url');

Or, manually:

$this->load->helper('url');

Then set your image like this..

<img src="<?php echo base_url('upload/screenshoot/'.$res->ss);?>" class="img-responsive zoom-img" alt="">

in your view/HTML side try using

foreach($getAll as $key => $rec)

instead of

foreach($getAll as $rec)

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