Jump to content

codeigniter image question


witchy478

Recommended Posts

Hi

 

I'm trying to insert images and text into my database, I have found tutorials on how to insert them but the only give it in single forms. For example they give a tutorial to only insert images. So I would like to know how to combine the two of them. I have tried but it isn't working I get the text but I don't get the image file.

 

This is my controller

<?php

class Bras extends CI_Controller{

public function __construct()
{
	parent::__construct();
	$this->load->model('bras_model');
	$this->load->helper('url');
	$this->load->helper('form');
}

function index() {

	$data['title'] = "Voluptuous Decadents";
	$data['heading'] = "Bras";
	$data['query'] = $this->db->get('bras');

	$this->load->view('layout');			
	$this->load->view('bras/create', $data);

}

function bras_view(){

		$data['title'] = "Voluptuous Decadents";
	$data['heading'] = "Bras";
	$this->db->where('slug', $this->uri->segment(3));
	$data['query'] = $this->db->get('bras');

			$this->load->view('layout');			

	$this->load->view('bras/bras_view', $data);

}	

public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');

$data['title'] = 'Create a bra item';

$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'content', 'required');

if ($this->form_validation->run() === FALSE)
{
	$this->load->view('templates/header', $data);	
	$this->load->view('bras/create');
	$this->load->view('templates/footer');

}
else
{
	$this->bras_model->set_bras();
	$this->load->view('bras/success');
}
}	

function do_upload() {
        $config = array(
            'upload_path'   => './uploads/',
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => '100',
            'max_width'     => '1024',
            'max_height'    => '768',
            'encrypt_name'  => true,
        );

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

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('bras/create', $error);
        } else {
            $upload_data = $this->upload->data();
            $data_ary = array(
                
                'file'      => $upload_data['file_name'],
                
                
                
                
                
            );

            $this->load->database();
            $this->db->insert('bras', $data_ary);

            $data = array('upload_data' => $upload_data);
            $this->load->view('bras/success', $data);
        }
    }

}
?>

 

This is my model

<?php
class Bras_model extends CI_Model {

public function __construct()
{
	$this->load->database();
}

public function get_bras()
{


	$query = $this->db->get('bras');

}

public function set_bras()
{
$this->load->helper('url');

$slug = url_title($this->input->post('title'), 'dash', TRUE);

$data = array(
	'title' => $this->input->post('title'),
	'slug' => $slug,
	'content' => $this->input->post('content'),
	'file' => $this->input->post('file_name'),
	'price' => $this->input->post('price'),
);

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

}
?>	

 

this is my view

<h2>Create a news item</h2>

<?php echo validation_errors(); ?>
   
<?php echo form_open_multipart('bras/create') ?>

<label for="title">Title</label> 
<input type="input" name="title" /><br />
</form>
<?php echo form_open_multipart('bras/do_upload') ?>
<input type="file" name="userfile" size="20" /><br />
</form>
<?php echo form_open_multipart('bras/create') ?>
<label for="content">content</label>
<textarea name="content"></textarea><br />

<label for="price">Price</label>
<input type="input" name="price" /><br />

<input type="submit" name="submit" value="Create Bra item" /> 

</form>

Link to comment
Share on other sites

Your controller and views are totally messed up.  I have a link in my signature for a CI image library that can help you.  Let me expand on where you're going wrong.

 

Foremost, your sporadic use of including libraries can be eliminated by adding them to the config/autoloader.php.

 

You have three forms and one submit button.  This is not the way things are done, as the submit button will only activate the last form, which is only for your textarea and your price.

 

Your model is doing logic, and it shouldn't.  Your controllers are for that, the model is only for interacting with the database.

 

You have view loads in several incorrect locations.

 

The data accessing your database has not been secured or in any other way validated before execution.

 

Grab my library and take a peek at how I would write your code.

 

There's so much wrong with what you have going on, make some changes and come back.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.