1

I'm trying to add a final field to my project that contains a URL of the files that have been uploaded, if anyone could point me in the write direction for extending my model ? I have included my codeIgniter Model & Controller code.

controller:

class Canvas extends CI_Controller {
   function __construct() {
    parent::__construct();
                  $this->load->helper(array('form', 'url'));

}

function index() {
            $vars = array();
            //$this->load->library('ChromePhp');
            //$this->ChromePhp->log('test');     
            //$this->ChromePhp->log($_SERVER);

            // using labels
           // foreach ($_SERVER as $key => $value) {
             //   $this->ChromePhp->log($key, $value);
          //  }
            // warnings and errors
            //$this->ChromePhp->warn('this is a warning');
            //$this->ChromePhp->error('this is an error');
            $this->load->library('FacebookConnect');
            $facebook=$this->facebookconnect->connect();
            $vars['facebook'] = $facebook;
            $user = $vars['user'] = $facebook->getUser();
            $this->load->model('Users_Model');
            // user already set up. Show thank you page
            if($user != 0  && sizeof($_POST)>0) {
                $this->do_upload();
                $this->iterate_profile ($vars['user'],false,$_POST);
                $this->load->view('canvas',$vars);
            } else {
            // user not set, show welcome message
                $this->load->view('canvas',$vars);
            }

}
    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '20048';
        $config['max_width']  = '10624';
        $config['max_height']  = '10268';
        $config['file_name']  = date("Y_m_d H:i:s");

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

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }

    function thank_you() {
            $this->load->view('thank_you',$vars);
    }

    function remove() {
            $vars = array();
            $this->load->library('FacebookConnect');
            $facebook=$this->facebookconnect->connect();
            $vars['facebook'] = $facebook;
            $vars['user'] = $facebook->getUser();
            $this->load->model('Users_Model'); 
            if($vars['user'] == 0) {
                // user not set, redirect
                redirect('/', 'refresh');
            } else {
                // user already set up. Remove
                $this->load->model('Users_Model'); 
                $this->Users_Model->remove($vars['user']);
            }

            $this->load->view('removed',$vars);
    }

    protected function iterate_profile ($user,$breadcrumb,$item) {

        foreach($item as $key => $value) {
            if(is_array($value)) {
                $this->iterate_profile($user,$key,$value);
            }
            else {
                if($breadcrumb) {
                    //echo '[' . $breadcrumb . '_' . $key . ']= ' . $value . ' <br />';
                    $key = $breadcrumb . '_' . $key;
                }

                if( ! $this->Users_Model->exists($user,$key)) {
                    // does not exist in the database, insert it
                    $this->Users_Model->add($user,$key,$value);
                } else {
                    $this->Users_Model->update($user,$key,$value);
                }
            }
        }
    } 


}

Model:

class Users_Model extends CI_Model {

protected $_name = 'users';

function add($id,$key,$value) {
    $data = array(
       'id' => $id,
       'name' => $key,
       'value' => $value
    );

    return $this->db->insert($this->_name, $data); 
}
function update($id,$key,$value) {
    $data = array(
       'value' => $value
    );

    $this->db->where(array(
       'id' => $id,
       'name' => $key
    ));
    return $this->db->update($this->_name, $data); 
}

function exists($id,$key=null) {
    if($key == null) {
        $this->db->where(array(
        'id' => $id
        ));
    } else {
        $this->db->where(array(
        'id' => $id,
        'name' => $key
        ));  
    }

    $query = $this->db->get($this->_name);
    if($query->num_rows() > 0) {
        return true;
    }
    return false;
}

function remove($id) {
    $data = array(
       'id' => $id,
    );

    return $this->db->delete($this->_name, $data); 
}

function all() {
    $query = $this->db->get($this->_name);
    $results = array();
    if($query->num_rows() > 0) {
        foreach($query->result() as $row) {
            $results[]=$row;
        }
    } 
    return $results;  
}


}

1 Answer 1

0

I don't think the question is very clear. But to me it seems you need to store the image path in the db. Otherwise you will need to perform a read directory on the upload path and then do something with the images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.