• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display images from uploads folder using database

#1
[eluser]anyamanggar[/eluser]
please help me, I'm just learning codeigniter. I'm trying to create image gallery using a database to store the paths and use uploads folder for storing pictures and managed to do that thanks to the discussions on this forum, but I do not just want to keep it but want to display it as well. roughly can help me.

for save to folder uploads, there it is..

admin.php (controller)
Code:
<?php

class Admin extends CI_Controller {
    
    public function __construct()
       {
            parent::__construct();
            // Your own constructor code
       }
    
    function index() {
   $this->load->model('admin_model');
    
        $data = array();
    
        if($query = $this->admin_model->retrieve()) {
            $data['records'] = $query;
        }
        
        $data['title'] = "Admin";
        $this->load->view('panelview', $data);

    }

    function create() {

        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents')
            );
            
            $config = array(
                'upload_path' => 'uploads/',
                'allowed_types' => 'gif|jpeg|jpg|png',
                'max_size' => '5120' // 5MB
            );

            $this->load->library('upload', $config);
            $this->upload->initialize($config);

            if ($this->upload->do_upload()) {
                $data = array(
                    'upload_data' => $this->upload->data()
                );
                
                $file_name = $data['upload_data']['file_name'];
                $save['img_path'] = $file_name;
            }
             $this->load->model('admin_model');
            $this->admin_model->create($save, $data);
        }
        
        $data['title'] = "Add News";
        $this->load->view('addnews', $data);
        
        if($_POST != NULL) {
            redirect('../admin/');
        }

    }  

    function delete() {
        
        $this->admin_model->delete();
        $this->index();

    }

    function update($id) {
        
        if ($_POST) {
            $save = array(
                'title'  => $this->input->post('title'),
                'contents'  => $this->input->post('contents')
            );
            $this->admin_model->update($save, $id);
        }
        
        $data['news'] = $this->admin_model->get($id);
        $data['title'] = "Update";
        $this->load->view('editnews', $data);

        if($_POST != NULL) {
            redirect('../admin/');
        }
        
    }

}

admin_model.php
Code:
<?php

class Admin_model extends CI_Model {
    
     function __construct()
    {
        parent::__construct();
    }
    
    function create($data) {

        $this->db->insert('data', $data);

    }

    function retrieve() {
        
        $this->db->order_by("id", "desc");
        $query = $this->db->get('data');
        return $query->result();

    }

    function update($data, $id) {

        $this->db->where('id', $id);
        $this->db->update('data', $data);

    }

    function delete() {

        $this->db->where('id', $this->uri->segment(3));
        $this->db->delete('data');

    }
    
    
    function get($id) {
        
      return $this->db->get_where('data', array('id' => $id))->row();
    
    }
    
}



panelview.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
        &lt;style type="text/css"&gt;
            body {font-family: sans-serif;}
            #form {width: 675px; margin: auto; border: 2px solid #000; padding: 10px;}
        &lt;/style&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;
        <h1 align="center">Add News</h1>
        <div id="form">
            &lt;?php
                echo form_open_multipart('../admin/create');
                    echo "<label>Title:</label><br />";
                    echo form_input('title') . "<br /><br />";      
            
                    echo "<label>Image:</label><br />";
                    echo form_upload('userfile') . "<br /><br />";
            
                    echo "<label>Content:</table><br />";
                    echo form_textarea('contents') . "<br /><br />";
                    
                    echo form_submit('', 'Submit');
                echo form_close();
            ?&gt;
        </div>
    &lt;/body&gt;
&lt;/html&gt;


#2
[eluser]Rodrigo Graça[/eluser]
I have this code that can help you:

Code:
&lt;?
$arrayData = explode(".", $row->file_name);

if( $arrayData[1]=="swf"){
    ?&gt;
    <img src="&lt;?=base_url()?&gt;assets/images/swf.png" border="0" title="SWF" alt="SWF" width="75" height="75">
    &lt;?
}
else {
    ?&gt;
    <img src="&lt;?=base_url()?&gt;assets/uploads/&lt;?=$row-&gt;file_name?&gt;" border="0" title="&lt;?=$row-&gt;file_name?&gt;" alt="&lt;?=$row-&gt;file_name?&gt;" width="75" height="75">
    &lt;?
}
?&gt;

Forget the "if" part. I used the image html tag "img" to include the image that i received in "$row->file_name" from the database. The file name is for example "something.png"

And this is on the view file (yours is "panelview.php")

#3
[eluser]anyamanggar[/eluser]
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: row

Filename: views/panelview.php

Line Number: 18

something wrong i guest .. this my panelview.php after update from your code


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
        &lt;style type="text/css"&gt;
            body {font-family: sans-serif;}
            #form {width: 675px; margin: auto; border: 2px solid #000; padding: 10px;}
        &lt;/style&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;
    <div id="list_gallery">
    &lt;?
$arrayData = explode(".", $row->file_name);

if( $arrayData[1]=="swf"){
    ?&gt;
    <img src="&lt;?=base_url()?&gt;assets/images/swf.png" border="0" title="SWF" alt="SWF" width="75" height="75">
    &lt;?
}
else {
    ?&gt;
    <img src="&lt;?=base_url()?&gt;assets/uploads/&lt;?=$row-&gt;file_name?&gt;" border="0" title="&lt;?=$row-&gt;file_name?&gt;" alt="&lt;?=$row-&gt;file_name?&gt;" width="75" height="75">
    &lt;?
}
?&gt;  
    </div>
    
        <h1 align="center">Add News</h1>
        <div id="form">
            &lt;?php
                echo form_open_multipart('../admin/create');
                    echo "<label>Title:</label><br />";
                    echo form_input('title') . "<br /><br />";      
            
                    echo "<label>Image:</label><br />";
                    echo form_upload('userfile') . "<br /><br />";
            
                    echo "<label>Content:</table><br />";
                    echo form_textarea('contents') . "<br /><br />";
                    
                    echo form_submit('', 'Submit');
                echo form_close();
            ?&gt;
        </div>
    &lt;/body&gt;
&lt;/html&gt;

#4
[eluser]Rodrigo Graça[/eluser]
You can't use "$row" like that:
In your case i think that is: "records" that you use


First i have this on my controller:

Code:
$this->load->model("configs_model");

$data["rows"] = $this->configs_model->getConfigs();
                
$this->load->view("select", $data);


And on my view i have a loop to show all images:

Code:
&lt;?
foreach($rows as $row)
     {
    ?&gt;
    <tr id="&lt;?=$row-&gt;id?&gt;">

                                        <td>&lt;?=$row->id?&gt;</td>
                                        
                                        
                                        <td>&lt;?=$row->file_name?&gt;</td>
                                        
                                        <td>
                                            &lt;?
                                            $arrayData = explode(".", $row->file_name);
                                            
                                            if( $arrayData[1]=="swf"){
                                                ?&gt;
                                                <img src="&lt;?=base_url()?&gt;assets/images/swf.png" border="0" title="SWF" alt="SWF" width="75" height="75">
                                                &lt;?
                                            }
                                            else {
                                                ?&gt;
                                                <img src="&lt;?=base_url()?&gt;assets/uploads/&lt;?=$row-&gt;file_name?&gt;" border="0" title="&lt;?=$row-&gt;file_name?&gt;" alt="&lt;?=$row-&gt;file_name?&gt;" width="75" height="75">
                                                &lt;?
                                            }
                                            ?&gt;
                                        </td>
    </tr>
    &lt;?
    } ?&gt;

#5
[eluser]anyamanggar[/eluser]
i am still confuse.. could you add to my code please.. i will appriciate

#6
[eluser]Rodrigo Graça[/eluser]
Try this:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt;
        &lt;meta http-equiv="Content-Type" content="text/html; charset-UTF-8"&gt;
        
        &lt;style type="text/css"&gt;
            body {font-family: sans-serif;}
            #form {width: 675px; margin: auto; border: 2px solid #000; padding: 10px;}
        &lt;/style&gt;
        
    &lt;/head&gt;    
    &lt;body&gt;

    <div id="list_gallery">
<table name="list" id="list">
<thead>
   <tr>
    <th>
     ID
    
    </th>
<th>
     file name
    
    </th>
<th>
     image
    
    </th>
    
   </tr>
  </thead>
<tbody>
    &lt;?
    if(count($records)>0 && $rows!="")
    {
     foreach($rows as $row)
     {
    ?&gt;
    <tr id="&lt;?=$row-&gt;id?&gt;">
     <td>
      <span class="bosCheckbox tiptip">
       &lt;input type="checkbox" name="selection" value="1" /&gt;
       <span class="box" title="Check"></span>
      </span>
     </td>
                                        <td>&lt;?=$row->id?&gt;</td>
                                        
                                        
                                        <td>&lt;?=$row->file_name?&gt;</td>
                                        
                                        <td>
                                                <img src="&lt;?=base_url()?&gt;assets/uploads/&lt;?=$row-&gt;file_name?&gt;" border="0" title="&lt;?=$row-&gt;file_name?&gt;" alt="&lt;?=$row-&gt;file_name?&gt;" width="75" height="75">
                                              
                                        </td>
                                      
                                      
    </tr>


    &lt;?
    }
    }
   ?&gt;

     </tbody>
  </table>
</div>
    
        <h1 align="center">Add News</h1>
        <div id="form">
            &lt;?php
                echo form_open_multipart('../admin/create');
                    echo "<label>Title:</label><br />";
                    echo form_input('title') . "<br /><br />";      
            
                    echo "<label>Image:</label><br />";
                    echo form_upload('userfile') . "<br /><br />";
            
                    echo "<label>Content:</table><br />";
                    echo form_textarea('contents') . "<br /><br />";
                    
                    echo form_submit('', 'Submit');
                echo form_close();
            ?&gt;
        </div>
    &lt;/body&gt;
&lt;/html&gt;



Notes:

Maybe some erros.... (HTML or PHP)
Where is "file_name" you should change to the name of your fiel in the database. (Or change the database field name to "file_name" Big Grin )
All my files are .jpg or .png (if you want to display another type you need to change "img" tag....)

#7
[eluser]anyamanggar[/eluser]
thx Guys, its work thank you

#8
[eluser]Rodrigo Graça[/eluser]
[quote author="anyamanggar" date="1337762843"]thx Guys, its work thank you[/quote]

worked? without errors? WTF? Big Grin
(i writed the code here, with copy & paste)

#9
[eluser]Unknown[/eluser]
also tried the code submitted by Rodrigo.. but it just showed the filename and not the image itself..pls how do i go about that.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.