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