Welcome Guest, Not a member yet? Register   Sign In
Help me about this controller ?
#1

[eluser]Unknown[/eluser]
Help me fix this, upload the image in the controller addproduct.
Source in the attachment. Help me. Thanks.
Controller page
Code:
<?php
class Product extends Controller{
    public function __construct(){
        parent::controller();
        $this->load->helper("url");    
        $this->load->helper("form");    
        $this->load->library("form_validation");
        $this->load->library("session");
        $this->load->model("Mproduct");    
    }
    public function addproduct(){
        $data['cate']=$this->Mproduct->listcate();
        $this->form_validation->set_rules("title","Title","required");
        $this->form_validation->set_rules("author","Author","required");
        $this->form_validation->set_rules("full","Full Information","required");
        $this->form_validation->set_rules("price","Price","required");
        if($this->form_validation->run()==false){
            $this->load->view("admin/addproduct_view",$data);
        }else{
            if(!$this->input->post('img')){
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']    = '800';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload("img"))
                {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view("admin/addproduct_view",$error,$data);
                    
                }else{
                    $img = array('upload_data' => $this->upload->data());
                    foreach($img as $item){
                        $img2=$item['file_name'];
                    }
                    $data=array(
                        "pro_author"  =>  $this->input->post('author'),
                        "pro_title"   =>  $this->input->post('title'),
                        "pro_full"    =>  $this->input->post('full'),
                        "pro_price"   =>  $this->input->post('price'),
                        "pro_img"     =>  base_url()."uploads/".$img2,
                        "cate_id"     =>  $this->input->post('cate'),
                    );
                    $this->Mproduct->insert_product($data);
                    redirect(base_url()."admin/product/listproduct");
                }
            }else{
                    $data=array(
                        "pro_author"  =>  $this->input->post('author'),
                        "pro_title"   =>  $this->input->post('title'),
                        "pro_full"    =>  $this->input->post('full'),
                        "pro_price"   =>  $this->input->post('price'),
                        "pro_img"     =>  "none",
                        "cate_id"     =>  $this->input->post('cate'),
                    );
                    $this->Mproduct->insert_product($data);
                    redirect(base_url()."admin/product/listproduct");
            }
        }
    }
}
?>

View page:
Code:
<?php
$user=$this->session->userdata("username");
$level=$this->session->userdata("level");
if(isset($user)&& $level==2){
$this->load->view("admin/top.php");
$this->load->view("admin/menu.php");
$this->load->view("admin/left.php");
$submit=array(
    "name"  =>  "ok",
    "value" =>  "Add cate",
);
foreach($cate as $item){
    $data[$item['cate_id']]=$item['cate_title'];
}
$title=array(
    "name"  =>  "title",
    "size"  =>  "30",
);
$author=array(
    "name"  =>  "author",
    "size"  =>  "30",
);
$full=array(
    "name"  =>  "full",
    "cols"  =>  "60",
    "rows"  =>  "30",
);
$img=array(
    "name"  =>  "img",
    "size"  =>  "30",
);
$price=array(
    "name"  =>  "price",
    "size"  =>  "30",
);

            echo form_fieldset("Add Product");
            echo "<center>".validation_errors()."</center>";
            echo $error;
            echo form_open_multipart(base_url()."admin/product/addproduct");
            echo form_label("Categorie"). form_dropdown("cate",$data). "<br />";
            echo form_label("Title"). form_input($title). "<br />";
            echo form_label("Author"). form_input($author). "<br />";
            echo form_label("Full Information"). form_textarea($full). "<br />";
            echo form_label("Image"). form_upload($img). "<br />";
            echo form_label("Price"). form_input($price). " VND<br />";
            echo form_label("&nbsp;"). form_submit($submit). "<br />";
            echo form_close();
            echo form_fieldset_close();    
$this->load->view("admin/bottom.php");
}else{
    redirect(base_url()."admin/user/login");    
}
?&gt;
#2

[eluser]jmadsen[/eluser]
Can you describe what problem you are having? Does it simply not upload, does it fail with an error, etc...




Theme © iAndrew 2016 - Forum software by © MyBB