también fue necesario hacer otras modificaciones al código, pero nada importante, por si alguien quiere comparar con el código anterior los principales cambios fueron en el controlador upload.php modificando el método do_upload y en la vista content.php. Empecemos...
Paso 1
Para simplificar las cosas vamos a reemplazar el codigo del controlador manage_products.php por el siguiente:Código de manage_products.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage_products extends CI_Controller {
function __construct() {
parent::__Construct();
$this ->load->model('Products_Model');
$this->load->library('pagination');
$this->form_validation->set_rules('marca', 'Marca', 'required|min_length[3]');
$this->form_validation->set_rules('pantalla', 'Pantalla', 'required|min_length[3]');
$this->form_validation->set_rules('ram', 'Memoria Ram', 'required|min_length[3]');
$this->form_validation->set_rules('procesador', 'Procesador', 'required|min_length[3]');
$this->form_validation->set_rules('disco_duro', 'Disco Duro', 'required|min_length[3]');
$this->form_validation->set_rules('precio', 'Precio','required|min_length[6]');
$this->form_validation->set_message('required', 'el campo %s es requerido');
$this -> form_validation -> set_error_delimiters('<ul><li>', '</li></ul>');
}
function index(){
$pagination = 6;
//$config['base_url'] = base_url().'index.php/manage_products/index';
$config['base_url'] = base_url().'manage_products/index';
$config['total_rows'] = $this->db->get('productos')->num_rows();
$config['per_page'] = $pagination;
$config['num_links'] = 20;
$config['next_link'] = 'Siguiente »';
$config['prev_link'] = '« Anterior';
$this->pagination->initialize($config);
$this->table->set_heading('Producto','Detalles','Imagen','Editar','');
$tmpl = array ( 'table_open' => '<table border="1" id="table">' );
$this->table->set_template($tmpl);
$data['title'] = 'Todos los Productos';
$data['results'] = $this->Products_Model->get_products($pagination, $this->uri->segment(3));
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/products/products');
$this->load->view('front/footer');
}
function edit(){
$id = $this->input->get('product');
$data['title'] = 'Editar Producto';
$data['results'] = $this->Products_Model->get($id);
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/products/edit_product');
$this->load->view('front/footer');
}
function add(){
$data['title'] = 'Nuevo Producto';
if ($this->form_validation->run() == FALSE){
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/products/new_product');
$this->load->view('front/footer');
}else{
$insert = $this->Products_Model->update_product();
if($insert){
$data['title'] = 'El Producto se creó correctamente.';
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/success');
$this->load->view('front/footer');
}else{
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/products/new_product');
$this->load->view('front/footer');
}
}
}
function update(){
$data['title'] = 'Editar Producto';
$data['id'] = $this->input->post('id');
if ($this->form_validation->run() == FALSE){
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/products/edit_product');
$this->load->view('front/footer');
}else{
$insert = $this->Products_Model->update_product($this->input->post('id'));
if($insert){
$data['title'] = 'El Producto se actualizó correctamente.';
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/success');
$this->load->view('front/footer');
}else{
$this->load->view('admin/header_admin',$data);
$this->load->view('admin/products/edit_product');
$this->load->view('front/footer');
}
}
}
}// fin de la clase
Paso 2
Vamos a application/models/ y reemplazmos el contenido de el modelo products_model.phpCódigo del modelo products_model.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Products_Model extends CI_Model {
function get_products($pagination, $segment) {
$this->db->order_by('marca', 'asc');
$this->db->limit($pagination, $segment);
$query = $this->db->get('productos')->result();
foreach ($query as $result) {
if ($result->valores) {
$result->valores = explode(',',$result->valores);
}
}
return $query;
}
function get_product($id) {
$query = $this->db->get_where('productos', array('id' => $id))->result();
$result = $query[0];
if ($result->valores) {
$result->valores = explode(',',$result->valores);
}
return $result;
}
function get($id){
$query = $this->db->get_where('productos', array('id' => $id))->result();
return $query;
}
function update_product($id=NULL){
$data = array(
'marca' => $this->input->post('marca'),
'pantalla' => $this->input->post('pantalla'),
'ram' => $this->input->post('ram'),
'procesador' => $this->input->post('procesador'),
'disco_duro' => $this->input->post('disco_duro'),
'precio' => $this->input->post('precio'),
'opcion' => $this->input->post('opcion'),
'valores' => $this->input->post('atributos'),
);
if($id!==NULL){
$this->db->where('id', $id);
return $this->db->update('productos', $data);
}else{
return $this->db->insert('productos', $data);
}
}
}
Paso 3
reemplazamos el código de la vista products.php, el cual encontramos en application/views/admin/productsCódigo de products.php
<?=heading($title, 2);?>
<div class="all_products">
<p align="center"><?=anchor('manage_products/add/' ,'Nuevo Producto', 'title="Agregar Producto"') ?></p>
<?php
$edit_img = '<img src="'. base_url().'assets/img/edit.png"/>';
$img = '<img src="'. base_url().'assets/img/image.png"/>';
$check = array('id'=>'check','name'=>'check','value'=>1,'checked'=>FAlSE);
$atributo_link = 'class="link"';
$atributo_img = 'title="Cambiar la Imagen del Producto"';
echo form_open('manage_products/delete');
foreach($results as $result){
$details = 'Pantalla: '.$result->pantalla.br(1).'Ram: '.$result->ram.br(1).'Procesador: '.$result->procesador.br(1).'Disco Duro: '.$result->disco_duro;
if($result->imagen ===''){
$product_img = $img;
}else{
$product_img = img(base_url().'images/'.$result->imagen);
}
$this->table->add_row('<strong>'.$result->marca.'</strong>',
$details,
anchor('manage_products/image?product='.$result->id, $product_img, $atributo_img),
anchor('manage_products/edit?product='.$result->id, $edit_img, $atributo_link ),
$delete = form_checkbox('products[]',$result->id, FALSE)
);
} // fin foreach
echo $this->table->generate();
?>
<div class="actions">
<strong>Marcar<?=form_checkbox($check)?></strong>
<strong class="delete_user"><?=form_submit('action', 'Eliminar')?></strong>
</div>
<?php echo form_close()?>
<div id="pagination"><?=$this->pagination->create_links();?></div>
<script type="text/javascript">
// *** Funciones con jQuery
$(document).ready(function(){
var products = $('#table :checkbox'); // alert(products.length);
var check = $('#check');
// **** Función para seleccionar y deseleccionar los productos
check.click(function(){
var j = 0;
var option = $('#check:checked').val();
if(option==1){
products.each(function(){
products[j].checked=1;
j++;
});
}else{
products.each(function(){
products[j].checked=0;
j++;
});
} // fin if
});
});
</script>
</div>
Paso 4
Vamos a views/admin/products/ y creamos una vista llamada edit_product.php y le colocamos el siguiente código.Vista edit_product.php
<?=heading($title,4);
$attributes = array('id' => 'form_login', 'class'=>'users');
$marca = array('name'=>'marca', 'id'=>'marca','class'=>'input','placeholder'=>'Marca',
'value'=>set_value('marca'), 'size'=> '35');
$pantalla = array('name'=>'pantalla', 'id'=>'pantalla','class'=>'input','placeholder'=>'Pantalla',
'value'=>set_value('pantalla'), 'size'=> '35');
$ram = array('name'=>'ram', 'id'=>'ram','class'=>'input','placeholder'=>'Memoria Ram',
'value'=>set_value('ram'), 'size'=> '35');
$procesador = array('name'=>'procesador', 'id'=>'procesador','class'=>'input','placeholder'=>'Procesador',
'value'=>set_value('procesador'), 'size'=> '35');
$disco_duro = array('name'=>'disco_duro', 'id'=>'disco_duro','class'=>'input','placeholder'=>'Disco Duro',
'value'=>set_value('disco_duro'), 'size'=> '35');
$precio = array('name'=>'precio', 'id'=>'precio','class'=>'input','placeholder'=>'Precio',
'value'=>set_value('precio'), 'size'=> '35');
$opcion = array('name'=>'opcion', 'id'=>'opcion','class'=>'input','placeholder'=>'Opción del atributo',
'value'=>set_value('opcion'), 'size'=> '35');
$atributos = array('name'=>'atributos', 'id'=>'atributos','class'=>'input','placeholder'=>'Atributos del producto',
'value'=>set_value('atributos'), 'size'=> '35');
$data = array('name'=>'id', 'value'=>set_value('id'));
if(validation_errors()){ ?>
<div id="error"><?=validation_errors();?></div>
<?php }else{
foreach ($results as $result){
$id = $result->id;
$marca['value'] = $result->marca;
$pantalla['value'] = $result->pantalla;
$ram['value'] = $result->ram;
$procesador['value'] = $result->procesador;
$disco_duro['value'] = $result->disco_duro;
$precio['value'] = $result->precio;
$opcion['value'] = $result->opcion;
$atributos['value'] = $result->valores;
}
}
?>
<?=form_open('manage_products/update', $attributes);?>
<div class="padding"><?=form_label('Marca');?></div>
<div class="padding"><?=form_input($marca)?></div>
<div class="padding"><?=form_label('Pantalla')?></div>
<div class="padding"><?=form_input($pantalla)?></div>
<div class="padding"><?=form_label('Memoria Ram')?></div>
<div class="padding"><?=form_input($ram)?></div>
<div class="padding"><?=form_label('Procesador')?></div>
<div class="padding"><?=form_input($procesador)?></div>
<div class="padding"><?=form_label('Disco duro')?></div>
<div class="padding"><?=form_input($disco_duro)?></div>
<div class="padding"><?=form_label('Precio')?></div>
<div class="padding"><?=form_input($precio)?></div>
<div class="padding"><?=form_label('Opción del atributo')?></div>
<div class="padding"><?=form_input($opcion)?></div>
<div class="padding"><?=form_label('Atributo(s)')?></div>
<div class="padding"><?=form_input($atributos)?></div>
<?=form_hidden('id',$id)?>
<?=form_submit(array('name' => 'submit','class'=>'submit','value' => 'Modificar'))?>
<?=form_close();?>
<div class="clear"></div>
Paso 5
Continuando en la carpeta products creamos una vista de nombre new_product.phpCódigo de la vista new_product.php
<?=heading($title,4);
$attributes = array('id' => 'form_login', 'class'=>'users');
$marca = array('name'=>'marca', 'id'=>'marca','class'=>'input','placeholder'=>'Marca',
'value'=>set_value('marca'), 'size'=> '35');
$pantalla = array('name'=>'pantalla', 'id'=>'pantalla','class'=>'input','placeholder'=>'Pantalla',
'value'=>set_value('pantalla'), 'size'=> '35');
$ram = array('name'=>'ram', 'id'=>'ram','class'=>'input','placeholder'=>'Memoria Ram',
'value'=>set_value('ram'), 'size'=> '35');
$procesador = array('name'=>'procesador', 'id'=>'procesador','class'=>'input','placeholder'=>'Procesador',
'value'=>set_value('procesador'), 'size'=> '35');
$disco_duro = array('name'=>'disco_duro', 'id'=>'disco_duro','class'=>'input','placeholder'=>'Disco Duro',
'value'=>set_value('disco_duro'), 'size'=> '35');
$precio = array('name'=>'precio', 'id'=>'precio','class'=>'input','placeholder'=>'Precio',
'value'=>set_value('precio'), 'size'=> '35');
$opcion = array('name'=>'opcion', 'id'=>'opcion','class'=>'input','placeholder'=>'Opción del atributo',
'value'=>set_value('opcion'), 'size'=> '35');
$atributos = array('name'=>'atributos', 'id'=>'atributos','class'=>'input','placeholder'=>'Atributos del producto',
'value'=>set_value('atributos'), 'size'=> '35');
$data = array('name'=>'id', 'value'=>set_value('id'));
if(validation_errors()){ ?>
<div id="error"><?=validation_errors();?></div>
<?php } ?>
<?=form_open('manage_products/add', $attributes);?>
<div class="padding"><?=form_label('Marca');?></div>
<div class="padding"><?=form_input($marca)?></div>
<div class="padding"><?=form_label('Pantalla')?></div>
<div class="padding"><?=form_input($pantalla)?></div>
<div class="padding"><?=form_label('Memoria Ram')?></div>
<div class="padding"><?=form_input($ram)?></div>
<div class="padding"><?=form_label('Procesador')?></div>
<div class="padding"><?=form_input($procesador)?></div>
<div class="padding"><?=form_label('Disco duro')?></div>
<div class="padding"><?=form_input($disco_duro)?></div>
<div class="padding"><?=form_label('Precio')?></div>
<div class="padding"><?=form_input($precio)?></div>
<div class="padding"><?=form_label('Opción del atributo - Ej. Color')?></div>
<div class="padding"><?=form_input($opcion)?></div>
<div class="padding"><?=form_label('Atributo(s) - Ej. Azul,verde')?></div>
<div class="padding"><?=form_input($atributos)?></div>
<?=form_submit(array('name' => 'submit','class'=>'submit','value' => 'Guardar'))?>
<?=form_close();?>
<div class="clear"></div>
Finalmente agregamos unas lineas de estilo al archivo style.css
.link img{width:20px; height:20px; padding-left: 20px;} .products img{width:245px; height:184px;} .products select{max-width:100px;}Descargar Ejemplo