Welcome Guest, Not a member yet? Register   Sign In
[Newbie]uploading image + save into table (db)
#1

[eluser]Unknown[/eluser]
hi all...
i'm a completely newbie here
i have a problem n' i still confuse about uploading image and then saving "them" into database
here's the code i've done..

the controller :
Code:
<?
class Produsen extends Controller
{
    
    function Produsen(){
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }

        function input($id = 0)
    {    
          $this->load->helper('form');
        $this->load->helper('html');
        $this->load->model('produsen_model');
        
        if($this->input->post('tambah'))
        {
            if($this->input->post('id'))
            {
                $this->produsen_model->entry_update();
            }
            else
            {
                $this->produsen_model->entry_insert();
            }
        }
        $data = $this->produsen_model->general();
            if((int)$id > 0)
            {
                $query = $this->produsen_model->get($id);
                
                $data['fid']['value'] = $query['id'];
                $data['fnama']['value'] = $query['nama'];
                $data['fkategori']['value'] = $query['kategori'];
                $data['falamat']['value'] = $query['alamat'];
                $data['ftelepon']['value'] = $query['telepon'];
                $data['ffax']['value'] = $query['fax'];
                $data['fwebsite']['value'] = $query['website'];
                $data['femail']['value'] = $query['email'];
                $data['fjenis']['value'] = $query['jenis'];
                $data['ftipe']['value'] = $query['tipe'];
                $data['feksekutif']['value'] = $query['eksekutif'];
                $data['fposisi']['value'] = $query['posisi'];
                $data['fpic']['value'] = $query['pic'];            
            }
        $this->load->view('produsen_input',$data);    
    }
}
?>

the model :
Code:
<?
class produsen_model extends Model
{
    function produsen_model()
        {
            parent::Model();
            $this->load->helper('url');              
        }
    
    function general()
        {
            $this->load->library('IndagMenu');
            $menu = new IndagMenu;
            $data['menu']       = $menu->show_menu();
        $data['base']        = $this->config->item('base_url');
        $data['css']        = $this->config->item('css');
                  
                        $data['fid']['value']   = 0;
            $data['nama']        = 'Nama';
            $data['kategori']    = 'Kategori';
            $data['alamat']        = 'Alamat';
            $data['telepon']    = 'Telepon';
            $data['fax']        = 'Fax';
            $data['website']    = 'Website';
            $data['email']        = 'E-mail';
            $data['jenis']        = 'Jenis';
            $data['tipe']        = 'Tipe';
            $data['eksekutif']    = 'Eksekutif';
            $data['posisi']        = 'Posisi';
            $data['pic']        = 'Gambar';
            
            $data['fnama']        = array('name'=>'nama','align'=>'left','size'=>'30');
            $data['fkategori']    = array('name'=>'kategori','align'=>'left','size'=>'30');
            $data['falamat']    = array('name'=>'alamat','align'=>'left');
            $data['ftelepon']    = array('name'=>'telepon','align'=>'left','size'=>'15');
            $data['ffax']        = array('name'=>'fax','align'=>'left','size'=>'15');
            $data['fwebsite']    = array('name'=>'website','align'=>'left','size'=>'30');
            $data['femail']        = array('name'=>'email','align'=>'left','size'=>'30');
            $data['fjenis']        = array('name'=>'jenis','align'=>'left','size'=>'30');
            $data['ftipe']        = array('name'=>'tipe','align'=>'left','size'=>'30');
            $data['feksekutif']    = array('name'=>'eksekutif','align'=>'left','size'=>'30');
            $data['fposisi']    = array('name'=>'posisi','align'=>'left','size'=>'30');
            $data['fpic']        = array('name'=>'pic','type'=>'file','align'=>'left');
            
            return $data;
        }
        
    
    function entry_insert()
        {
        $this->load->database();
        $data = array
                (
                'nama'=>$this->input->post('nama'),
                'kategori'=>$this->input->post('kategori'),
                'alamat'=>$this->input->post('alamat'),
                'telepon'=>$this->input->post('telepon'),
                'fax'=>$this->input->post('fax'),
                'website'=>$this->input->post('website'),
                'email'=>$this->input->post('email'),
                'jenis'=>$this->input->post('jenis'),
                'tipe'=>$this->input->post('tipe'),
                'eksekutif'=>$this->input->post('eksekutif'),
                'posisi'=>$this->input->post('posisi'),
                'pic'=>$this->input->post('pic')                
                );
            if ($data['nama']==""||$data['alamat']==""||$data['jenis']=="")
            {
                echo "[removed]alert('Data Tidak Boleh Kosong.');document.location.href='../produsen/input';[removed]";
            }
        else
        
        $this->db->insert('produsen',$data);
        }    
                
}
?>

so where should i call the uploading class and how to save it into a db ?
thx
regards...digimage
#2

[eluser]adamfairholm[/eluser]
Hey digimage,

Could you provide some insight as to what this program does and where image uploading fits into it?

As for saving images to a database, you wouldn't really "save" an image to a database, but the images would sit somewhere on the server and you'd have a table of references to those images. A simple example would be a folder on the server labeled images, and a table called "images" - each row has an id and the file name of the image, like:

Code:
id         filename
----------------------
1          beach
2          snow
3          cats

Then you could display all those images like this (where images is a query of one or more images you want).

Code:
<?php foreach($images->result() as $row) { ?>

<img >config->item('base_url');?&gt;images/&lt;?=$row->filename;?&gt;.jpg" />

&lt;?php } ?&gt;

Of course, for storing images in a database you'd want to include an alt and title tag and put those in there and maybe some other info, but that's the gist of it.
#3

[eluser]Unknown[/eluser]
yup...
i'm sorry what i mean is storing image in a server and then storing the 'path' into my table ;-)




Theme © iAndrew 2016 - Forum software by © MyBB