Escolar Documentos
Profissional Documentos
Cultura Documentos
es
http://victorroblesweb.es/2013/12/30/crud-con-codeigniter/
Al entrar en modificar:
# models/usuarios_model.php
1/7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
class usuarios_model extends CI_Model{
public function __construct() {
parent::__construct();
$this->load->database();
}
public function ver(){
$consulta=$this->db->query("SELECT * FROM usuarios;");
return $consulta->result();
}
public function add($email,$password,$nombre,$apellido){
$consulta=$this->db->query("SELECT email FROM usuarios WHERE email LIKE '$email'");
if($consulta->num_rows()==0){
$consulta=$this->db->query("INSERT INTO usuarios
VALUES(NULL,'$email','$password','$nombre','$apellido');");
if($consulta==true){
return true;
}else{
return false;
}
}else{
return false;
}
}
public function
mod($id_usuario,$modificar="NULL",$email="NULL",$password="NULL",$nombre="NULL",$apellido="NULL")
{
if($modificar=="NULL"){
$consulta=$this->db->query("SELECT * FROM usuarios WHERE id_usuario=$id_usuario");
return $consulta->result();
}else{
$consulta=$this->db->query("
UPDATE usuarios SET email='$email', password='$password',
nombre='$nombre', apellido='$apellido' WHERE id_usuario=$id_usuario;
");
if($consulta==true){
return true;
}else{
return false;
}
}
}
public function eliminar($id_usuario){
$consulta=$this->db->query("DELETE FROM usuarios WHERE id_usuario=$id_usuario");
if($consulta==true){
return true;
}else{
return false;
}
}
}
2/7
?>
# controllers/usuarios_controller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
class usuarios_controller extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->helper("url");
$this->load->model("usuarios_model");
$this->load->library("session");
}
$usuarios["ver"]=$this->usuarios_model->ver();
$this->load->view("usuarios_view",$usuarios);
}
if($this->input->post("submit")){
$add=$this->usuarios_model->add(
$this->input->post("email"),
$this->input->post("password"),
$this->input->post("nombre"),
$this->input->post("apellido")
);
}
if($add==true){
$this->session->set_flashdata('correcto', 'Usuario aadido correctamente');
}else{
$this->session->set_flashdata('incorrecto', 'Usuario aadido correctamente');
}
redirect(base_url());
}
3/7
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
$mod=$this->usuarios_model->mod(
$id_usuario,
$this->input->post("submit"),
$this->input->post("email"),
$this->input->post("password"),
$this->input->post("nombre"),
$this->input->post("apellido")
);
if($mod==true){
$this->session->set_flashdata('correcto', 'Usuario modificado
correctamente');
}else{
$this->session->set_flashdata('incorrecto', 'Usuario modificado
correctamente');
}
redirect(base_url());
}
}else{
redirect(base_url());
}
}
# views/usuarios_view.php
4/7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="utf-8"/>
<title>CRUD con CodeIgniter</title>
</head>
<body>
<h2>Crud con CodeIgniter</h2>
<?php
if($this->session->flashdata('correcto'))
echo $this->session->flashdata('correcto');
if($this->session->flashdata('incorrecto'))
echo $this->session->flashdata('incorrecto');
?>
<table border="1">
<tr>
<form action="<?=base_url("usuarios_controller/add");?>" method="post">
<td></td>
<td>
<input type="email" name="email"/>
</td>
<td>
<input type="text" name="password"/>
</td>
<td>
<input type="text" name="nombre"/>
</td>
<td>
<input type="text" name="apellido"/>
</td>
<td>
<input type="submit" name="submit" value="Aadir" />
</td>
</form>
</tr>
<?php
foreach($ver as $fila){
?>
<tr>
<td>
<?=$fila->id_usuario;?>
</td>
<td>
<?=$fila->email;?>
</td>
<td>
<?=$fila->password;?>
</td>
<td>
<?=$fila->nombre;?>
</td>
<td>
<?=$fila->apellido;?>
</td>
<td>
<a href="<?=base_url("usuarios_controller/mod/$fila->id_usuario")?>">Modificar</a>
<a href="<?=base_url("usuarios_controller/eliminar/$fila->id_usuario")?
>">Eliminar</a>
</td>
</tr>
<?php
}
5/7
?>
</table>
</body>
</html>
# views/modificar_view.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="UTF-8" />
<title>Modificar usuarios</title>
</head>
<body>
<h2>Modificar usuario</h2>
<form action="" method="POST">
<?php foreach ($mod as $fila){ ?>
<input type="email" name="email" value="<?=$fila->email?>"/>
<input type="text" name="password" value="<?=$fila->password?>"/>
<input type="nombre" name="nombre" value="<?=$fila->nombre?>"/>
<input type="apellido" name="apellido" value="<?=$fila->apellido?
>"/>
<input type="submit" name="submit" value="Modificar"/>
<?php } ?>
</form>
<a href="<?=base_url()?>">Volver</a>
</body>
</html>
Con esto ya tendremos un CRUD funcional. Esto a sido un ejemplo sencillo y rpido, sin validar formularios, etc. Se puede hacer todava
mejor.
Artculos relacionados
6/7
7/7