Welcome Guest, Not a member yet? Register   Sign In
Help regarding MVC .
#1

[eluser]bidur[/eluser]
Hi all,
I am trying to upload image into database.I am successful to do so (by referring to different threads in this forum).
But my problem is that I am not able to follow the Model-View-Controller(MVC) way.
I have a working code with a controller and a view.
I am hoping that someone could help me by guiding how can I use model in my code.(probably by separating the codes into different classes)

The View goes as follows:
Code:
<?=form_open_multipart('upload/do_upload'); ?>

<input type="file" name="userfile" size="20" />

<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

The controller goes as follows:
Code:
class Upload extends Controller {
    
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }
    
    function do_upload() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
    
        if (!$this->upload->do_upload()) {
            $this->content['error'] = array('error' => $this->upload->display_errors());
            $this->load->view('upload/do_upload', $this->content);
        }    
        else {
            $data = $this->upload->data();
            $this->content['upload_data'] = $data;
            $img_name=$data['file_name'];
            $image_path="./uploads/".$img_name;
            $fh = fopen($image_path, "r");
            $data = addslashes(fread($fh, filesize($image_path)));
            fclose($fh);
            
            $this->load->database();
            $dbdata = array(
                        'id'=>'',
                        'description' => $img_name,
                        'photo'=>$data
                        );
            $this->db->insert('photo', $dbdata);
            $image_id = $this->db->insert_id();
            redirect('upload');
            
        }
    }

Thank You
#2

[eluser]nmweb[/eluser]
Code:
$this->load->database();
            $dbdata = array(
                        'id'=>'',
                        'description' => $img_name,
                        'photo'=>$data
                        );
            $this->db->insert('photo', $dbdata);


The above should be in the model. E.g model Photo, with a method insert()

It's all about keeping code DRY. Say you use the code you wrote several times throughout your application. If you want to add a field (e.g. photo_extension ) or ucfirst the description field, you would have to change the code to insert at several points. If you do it in a model and throughout your app you call the model, you only have to change the model insert method.
#3

[eluser]bidur[/eluser]
Hi nmweb,
Thank you for your help.
But I am still having in running the code as suggested above by splitting the code into required MVC.
In the model part the variables $img_name and $data are not recognized.
#4

[eluser]gtech[/eluser]
have you passed them through as parameters?
#5

[eluser]bidur[/eluser]
Thank you gtech
I have forgotten to pass parameter
thank You
#6

[eluser]bidur[/eluser]
[removed]




Theme © iAndrew 2016 - Forum software by © MyBB