Welcome Guest, Not a member yet? Register   Sign In
Problem when trying to display image from database
#1

[eluser]FabD[/eluser]
Hi,

I can't display images stored into a database. After searching on the forum, i have found some idea and I did it.

First the piece of code in the form/view file :
Code:
<img src="&lt;?php echo base_url() ?&gt;index.php/signal/marches/displayimage/&lt;?php echo $marches['MA_ID'] ?&gt;" border='0' align='left'/>;

which gives me in the source file for an Id equal to 1 :
Code:
<img src="http://localhost/gerico/index.php/signal/marches/displayimage/1" border='0' align='left'/>;

the function int the controller :
Code:
function displayimage(){    
    $Id = $this->uri->segment(4);
    echo 'ID ='.$Id;
    $image = $this->MMarches->getImage($Id);
    if (!is_null($image)) {
        header("Content-type: image/jpeg");
        print($image['0']['MA_PHOTO']);
    }        
}

but no ID displayed via the 'echo' statement.

and finally the function in the model :
Code:
function getImage($Id){
     $Q = $this->db->query("SELECT MA_PHOTO FROM GERICO.REF_MARCHES WHERE MA_ID=".$Id);
     if ($Q->num_rows() > 0){            
          $data = $Q->row_array();
        }
    $Q->free_result();    
     $str = $this->db->last_query();
     echo $str;
     print_r($data);
     return $data;        
}

but again, nothing is displayed. So I guess the the call the controller's function doesn't work. But I don't see what is wrong.

Can you help me ?
#2

[eluser]TheFuzzy0ne[/eluser]
These functions might work a little better:
Code:
function displayimage($Id=FALSE)
{
    if ($Id))
    {
        $image = $this->MMarches->getImage($Id);
        header("Content-type: image/jpeg");
        print($image);
    }        
}

function getImage($Id)
{
    $data = '';
    $Q = $this->db->query("SELECT MA_PHOTO FROM GERICO.REF_MARCHES WHERE MA_ID=".$Id);
    if ($Q->num_rows())
    {
        $data = $Q->row_array();
        $data = $data['MA_PHOTO']
        $Q->free_result();  
    }
    return $data;
}
The above code is untested.
#3

[eluser]FabD[/eluser]
I applied the proposed code ... but still no image displayed.
It seems that it doesn't reach the function displayimage()

In the form/view, I have changed the call to the function displayimage() by a dummy call to an undefined function 'toto' :

Code:
<img src="&lt;?php echo base_url() ?&gt;index.php/signal/marches/toto/&lt;?php echo $marches['MA_ID'] ?&gt;" border='0' align='left'/>;
or
Code:
<img src="http://localhost/gerico/index.php/toto/1" border='0' align='left'/>

and .... nothing happens, no errors reported. So it seems that CI ignores the call into the 'src' parameter of the image ...

Any ideas ? where do I have to put the call to display the image ? Is my way of working wrong ?
#4

[eluser]TheFuzzy0ne[/eluser]
It looks to me like your src attribute is not set correctly. What the name of your controller?

The src attribute should look like this:
Code:
src="&lt;?php echo site_url("controller_name/display_image/$image_id"); ?&gt;"
#5

[eluser]FabD[/eluser]
I have tried with site_url() insteod of using base_url() ... without success.
The html source code seems right :
Code:
<img src="http://localhost/gerico/index.php/signal/marches/displayimage/1" border='1'/>

The URL which call the page is
Code:
http://localhost/gerico/index.php/signal/marches/edit/1

the controller 'marches' is in the folder 'application/controllers/signal/'.
#6

[eluser]TheFuzzy0ne[/eluser]
What happens when you put that address into your address bar?

Have you checked the the database is actually returning an image, as I suspect it's not.
#7

[eluser]FabD[/eluser]
when i put the url
Code:
http://localhost/gerico/index.php/signal/marches/displayimage/1
into the browser I get an error :
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: Id

Filename: signal/marches.php

Line Number: 74

( ! ) Fatal error: Call to a member function num_rows() on a non-object in C:\WebLocal\Gerico\system\application\models\mmarches.php on line 68
Call Stack
#    Time    Memory    Function    Location
1    0.0023    64544    {main}( )    ..\index.php:0
2    0.0068    115040    require_once( 'C:\WebLocal\Gerico\system\codeigniter\CodeIgniter.php' )    ..\index.php:115
3    0.5081    3393240    call_user_func_array ( )    ..\CodeIgniter.php:232
4    0.5081    3393240    Marches->displayimage( )    ..\CodeIgniter.php:0
5    0.5126    3425264    MMarches->getImage( )    ..\marches.php:74

As concerns the database (Oracle), I have put a jpeg image into the record with id 1
using PL-SQL Developer.
#8

[eluser]TheFuzzy0ne[/eluser]
Your code is quite buggy. I'd recommend you go with what I proposed, and iron out any creases. The model passes back a string which is empty if no image is found.
#9

[eluser]FabD[/eluser]
I think that the code that I ran is very closed to the one you proposed ;-)

into the controller :
Code:
function displayimage($id=FALSE){
     echo 'AAAAAAAAAAA' ;
     if ($id) {
           $image = $this->MMarches->getImages($Id);
            header("Content-type: image/jpeg");
            print($image);
     }  
}

into the model :
Code:
function getImage($Id){
    $Q = $this->db->query("SELECT MA_PHOTO FROM GERICO.REF_MARCHES WHERE MA_ID=".$Id);
    if ($Q->num_rows())       {
           $data = $Q->row_array();
           $data = $data['MA_PHOTO'];
           $Q->free_result();  
    }    
    return (isset($data)) ? $data : '';
        
}

It doesn't display the first line of displayimage() : echo 'AAAAAAAAAAA' ;
So, in my opinion, this method is never reached.
Why ? that's the question ...
#10

[eluser]TheFuzzy0ne[/eluser]
I beg to differ. When you called on the URL directly, you got a PHP error related to that function. Fix that and you're one step closer to fixing the function. Work directly with the URL in the address bar until it does what you want it to do.




Theme © iAndrew 2016 - Forum software by © MyBB