do not upload when there is no file selected

#1
[eluser]ramonskie[/eluser]
hi i'am pretty new to this so hope some one can help me out here..

i'm trying to create webshop
and now i'm with the admin part to edit a prodcut

i have 1 screen where i can edit the product
but when i only want to update the price instead
the update fails because i did not use the upload image form.
how can use this edit form when i only want to update the price

view
Code:
<div class="rightmenu_admin">
    &lt;?php foreach($product as $p): ?&gt;
    <table class="">
            <tr>
              <td>
                &lt;?php echo form_open_multipart('admin/products/edit_product'); ?&gt;
                &lt;?php echo form_hidden('id', $p['id']); ?&gt;
        <h3>&lt;?php echo $p['name']; ?&gt;</h3>
        <img src="&lt;?php echo base_url(); ?&gt;assets/img/products/&lt;?php echo $p['image']; ?&gt;" alt="" />
                <br>
                &lt;input type="file" name="userfile" size="20" /&gt;
                <br>
                Price: &lt;input type="text" name="price" value="&lt;?php echo $p['price']; ?&gt;" maxlength="20" /&gt;
                <br>
                
                &lt;?php
                $textarea = array(
                     'name'        => 'description',
                     'id'          => 'description',
                     'value'       => $p['description'],
                     'maxlength'   => '100',
                     'size'        => '50',
                     'style'       => 'width:50%',
                  ); ?&gt;
                Description:
                <br>
                &lt;?php echo Form_textarea($textarea); ?&gt;
                &lt;?php echo form_submit('', 'Edit'); ?&gt;
        &lt;?php echo form_close(); ?&gt;

              <td>
           </tr>
    </table>
    &lt;?php endforeach;?&gt;

</div>



controller
Code:
&lt;?php
class Products extends Controller {

        function index()
    {
            //default index view if anything goes wron user will be brought to default template
               $data = array(
                    'right_admincontent' => 'admin/products',
                    'left_admincontent' => 'admin/leftmenu'
                     );
               $this->load->view('includes/admin_template', $data);

    }

        function show_products()
        {
            $this->load->model('admin/products_model');
            //TODO: how to show this in the menu template?
              $data = array(
                    'right_admincontent' => 'admin/products',
                    'left_admincontent' => 'admin/leftmenu'
                     );
              $data['products'] = $this->products_model->retrieve_product_items();
              $this->load->view('includes/admin_template', $data);
       }

        function show_product()
        {
            $this->load->model('admin/products_model');
            //TODO: how to show this in the menu template?
              $data = array(
                    'right_admincontent' => 'admin/edit_product',
                    'left_admincontent' => 'admin/leftmenu'
                     );
              $data['product'] = $this->products_model->retrieve_product_item();
              $this->load->view('includes/admin_template', $data);
       }

       function edit_product()
       {
                $this->load->model('admin/products_model');
               $config['upload_path'] = './assets/img/products/';
        $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())
        {
            $error = array('error' => $this->upload->display_errors());
                        // need to load the right view in frame
            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
                        $file_data = $this->upload->data();
                        $filename = $file_data['file_name'];
                        $data['product'] = $this->products_model->edit_product($filename);
                        $data = array(
                                'right_admincontent' => 'admin/edit_product',
                                'left_admincontent' => 'admin/leftmenu'
                                 );
            $this->load->view('includes/admin_template', $data);
        }
       }

}

?&gt;



model
Code:
&lt;?php
class Products_model extends Model {

        function retrieve_product_items(){
        $query = $this->db->get('products');
        return $query->result_array();
    }

        function retrieve_product_item(){
            //TODO: we need to get the form id a different way..
                $this->db->where('id', $this->input->get_post('id'));
        $query = $this->db->get('products');
        return $query->result_array();
    }

        function edit_product($filename){
           $data = array(
               'image' => $filename,
               'price' => $this->input->post('price'),
               'description' => $this->input->post('description')
                );
            $this->db->where('id', $this->input->get_post('id'));
            $this->db->update('products', $data);
            //retrive id data to return to edit product
            //TODO: clean up
            $this->db->where('id', $this->input->get_post('id'));
            $query = $this->db->get('products');
            return $query->result_array();
        }
}

?&gt;

#2
[eluser]fesweb[/eluser]
You can test to see if a file was submitted by checking $_FILES:
Code:
if(trim($_FILES['userfile']['name']) != '')
{
// attempt upload
}
else
{
// do not attempt upload
}

#3
[eluser]ramonskie[/eluser]
thanks i knew it was something as simple as this Smile

and i was wondering if this is the right way to edit the product information
or do you know a different approach that is better?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.